#!/bin/sh
#
# copyright 2012 Petr Cerny <pcerny@suse.cz>
#
# published under the GNU General Public License, version 2
#

if test "x$1" = "x-?"; then
    cat <<EOH
ccw - configurable compiler wrapper

use envioronment variable CCW_CONF or first argument to select
configuration file to use. If the configuration file is executable it is
executed with the same parameters as ccw. If it is not it is stripped of
all linec beginning with the hash ('#') character and fed to shell's
eval functionality.

The configuration using 'eval' generally looks like:

    'command options "\$@" options'

which puts the original arguments in between some special ones.

If the desired configuration script is not found directly, an attempt is
made to find it in system wide configuration located in

    /usr/share/ccw

EOH
    exit 1
fi

if test -z "$CCW_CONF" ; then
    CCW_CONF=$1
    shift
fi

if test ! -f "$CCW_CONF" ; then
    if test ! -f "/usr/share/ccw/$CCW_CONF" ; then
        echo "configuration file '$CCW_CONF' does not exist (not even in /usr/share/ccw)." >&2
        exit 2
    else
        CCW_CONF="/usr/share/ccw/$CCW_CONF"
        echo "using system-wide configuration '$CCW_CONF'." >&2
    fi
fi

if test -x "$CCW_CONF" ; then
    exec $CCW_CONF "$@"
else
    eval $( grep -v "^#" $CCW_CONF )
fi

