#!/bin/sh

setup_path()
{
    # Power users can request that their environment is not modified.
    if [ "${QTOPIA_NO_ENV:=0}" = 1 ]; then
        return
    fi

    # Bail if these important variables aren't set.
    [ -n "$TOOLCHAIN" -a -n "$DEFAULT_DEVICE_PATH" -a -n "$DEVICE_BIN" ] || return

    # Default is not to use teambuilder. Device profiles must opt-in.
    USE_TB=${USE_TB:=0}

    # Turn off teambuilder if we can't locate it.
    if [ "$USE_TB" = 1 ]; then
        export TB_DIR=${TB_DIR:=/opt/teambuilder}
        if [ -d $TB_DIR ]; then
            :
        else
            USE_TB=0
        fi
    fi

    if [ "$USE_TB" = 1 ]; then
        # We need to set the PATH in this order for teambuilder
        export PATH="$DEVICE_BIN:$TB_DIR/bin:$TOOLCHAIN/bin:$PATH"

        _comp=
        TB_CC=${TB_CC:=gcc g++}
        if [ -z "$TB_SYS" ]; then
            # Remove the wrapper scripts
            for CC in $TB_CC; do
                compiler="${TB_CC_PREFIX}$CC"
                [ -z "$_comp" ] && _comp="$compiler"
                if [ -f "$DEVICE_BIN/$compiler" ]; then
                    rm "$DEVICE_BIN/$compiler"
                fi
            done
        else
            for CC in $TB_CC; do
                compiler="${TB_CC_PREFIX}$CC"
                [ -z "$_comp" ] && _comp="$compiler"
                if [ ! -f "$DEVICE_BIN/$compiler" -o "$DEFAULT_DEVICE_PATH/environment" -nt "$DEVICE_BIN/$compiler" -o "$DEVICE_CONFIG_PATH/environment" -nt "$DEVICE_BIN/$compiler" ]; then
                    # This gets called before we know how to invoke the host
                    # toolchain so we write a script that does the job the
                    # first time it's run. This is guaranteed to be after we
                    # know how to invoke the host toolchain.
                    #
                    # There would be more error checking here except that we
                    # know this script is going to be called immediately after
                    # compile.host has been written (as part of configure's
                    # compiler test).

                    #echo rebuilding "$DEVICE_BIN/$compiler"
                    cat >"$DEVICE_BIN/$compiler" <<END
#!/bin/sh
TOOLCHAIN="$TOOLCHAIN"
TB_SYS="$TB_SYS"
compiler="$compiler"
TB_DIR="$TB_DIR"
TB_STRIP_ARGS="$TB_STRIP_ARGS"
DEVICE_BIN="$DEVICE_BIN"
DEFAULT_DEVICE_PATH="$DEFAULT_DEVICE_PATH"
END
                    cat >>"$DEVICE_BIN/$compiler" <<'END'
rm "$0"
$DEVICE_BIN/compile.host\
    -DTOOLCHAIN="\"$TOOLCHAIN\""\
    -DTB_SYS="\"$TB_SYS\""\
    -DCOMPILER="\"$compiler\""\
    -DTB_DIR="\"$TB_DIR\""\
    -DTB_STRIP_ARGS="\"$TB_STRIP_ARGS\""\
    -o "$0" "$DEFAULT_DEVICE_PATH/wrapper.c" || exit 1
exec "$0" "$@"
END
                    chmod 755 "$DEVICE_BIN/$compiler"
                fi
            done
            if [ "$TEAMBUILDER_SYSTEM" = "$TB_SYS" ]; then
                unset TEAMBUILDER_SYSTEM
            fi
        fi

        _TB_CC_VER=${_TB_CC_VER:=$TB_CC_VER}
        if [ "$_TB_CC_VER" = auto ]; then
            compiler="$_comp"
            _TB_CC_VER="$($TOOLCHAIN/bin/$compiler --version 2>&1)"
            #echo _TB_CC_VER $_TB_CC_VER
            _TB_CC_VER="$(echo $_TB_CC_VER | grep $compiler)"
            #echo _TB_CC_VER $_TB_CC_VER
            _TB_CC_VER="$(echo $_TB_CC_VER | sed 's/.*\([[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\).*/\1/')"
            #echo _TB_CC_VER $_TB_CC_VER
            if echo "$_TB_CC_VER" | grep '^[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*$' >/dev/null 2>&1; then
                export _TB_CC_VER
            else
                echo "ERROR: Cannot auto-detect compiler version. Please specify TB_CC_VER explicitly."
                exit 2
            fi
        fi
        echo "$TEAMBUILDER_CC_VERSION" | grep ':' >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            export TEAMBUILDER_CC_VERSION="${TB_CC_PREFIX}*:${_TB_CC_VER};$(echo $TEAMBUILDER_CC_VERSION | sed 's/'${TB_CC_PREFIX}'[^:]*:[[digit].][[digit].]*;*//')"
        else
            export TEAMBUILDER_CC_VERSION="${TB_CC_PREFIX}*:${_TB_CC_VER};*:$(echo $TEAMBUILDER_CC_VERSION | sed 's/'${TB_CC_PREFIX}'[^:]*:[[digit].][[digit].]*;*//')"
        fi
    else
        # don't destroy the environment if $TOOLCHAIN/bin is already in the $PATH
        if echo "$PATH" | grep "$TOOLCHAIN/bin"; then
            :
        else
            export PATH="$TOOLCHAIN/bin:$PATH"
        fi
    fi
}
