#!/usr/local/bin/bash
# $Id: compile.in,v 1.14 2003/02/05 11:27:02 jmmv Exp $
# bt_logic's compile module.
#
# buildtool
# Copyright (c) 2003, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

PreArgs=""
PostArgs=""
SrcFile=""
ObjFile=""

bt_logic_compile() {
    Type=$1; shift; FakeCmd=$1; shift

    while [ $# -gt 0 ]; do
        arg="$1"; shift
        
        case $arg in
            -I*)
                # Remember include directories, resolving them to absolute
                # paths.

                argdir=`echo $arg | sed -e s/-I//`
                dir=`cd $argdir && pwd`
                PreArgs="$PreArgs -I$dir"
                continue
                ;;

            -L*|-l*|-Wl*)
                # Discard any linker flags; we are only compiling.

                continue
                ;;

            -W*|-O*)
                # Warnings and optimizations go first, no specific reason.

                PreArgs="$PreArgs $arg"
                continue
                ;;

            -c*)
                # Discard -c.  We set it later manually at the right place.

                continue
                ;;

            -g)
                # Remember debugging options.

                PreArgs="$arg $PreArgs"
                continue
                ;;

            -o*)
                # Remember any object file found.

                ObjFile=`echo $arg | sed -e s/-o//`
                if [ -z "$ObjFile" ]; then
                    ObjFile="$1"; shift
                fi
                continue
                ;;

            *)
                # Unknown flags are stored and thrown then to the compiler.
                # Source files are remembered.

                if [ -n "`echo $arg | grep -- ^-`" ]; then
                    PreArgs="$PreArgs $arg"
                else
                    SrcFile="$arg"
                fi
                continue
                ;;
        esac
    done
    PostArgs="$PostArgs -c $SrcFile"

    bt_logic_compile_$Type
}

bt_logic_compile_file() {
    if [ "$FakeCmd" = "cc" ]; then
        runcmd ${BT_PROG_CC} $PreArgs -o $ObjFile $PostArgs
    else
        runcmd ${BT_PROG_CXX} $PreArgs -o $ObjFile $PostArgs
    fi
}

bt_logic_compile_prog() {
    echo "[compile-prog] $SrcFile -> $ObjFile"
    bt_logic_compile_file
}

bt_logic_compile_lib() {
    if [ "$BT_LOGIC_MKSTATIC" = "yes" ]; then
        echo "[compile-lib, static] $SrcFile -> $ObjFile"
        bt_logic_compile_file
    fi
    
    if [ "$BT_LOGIC_MKPIC" = "yes" ]; then
        PreArgs="$PreArgs $BT_LIB_PIC_COMPILE_PREARGS"

        oldobjfile="$ObjFile"
        ObjFile=`echo $ObjFile | sed -e "s/\.o$/.po/g"`
        echo "[compile-lib, pic] $SrcFile -> $ObjFile"
        bt_logic_compile_file
        ObjFile="$oldobjfile"
    fi
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/usr/local/bin/bash
# $Id: depend.in,v 1.4 2003/02/04 11:24:02 jmmv Exp $
# bt_logic's depend module.
#
# buildtool
# Copyright (c) 2003, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

bt_logic_depend() {
    _append="no"
    _flags=""
    _files=""

    while [ $# -gt 0 ]; do
        arg="$1"; shift
        case $arg in
            -I*)
                _argdir=`echo $arg | sed -e s/-I//`
                _dir=`cd $_argdir && pwd`
                _flags="$_flags -I$_dir"
                ;;
            -a)
                _append="yes"
                ;;
            -*)
                _flags="$_flags $arg"
                ;;
            *)
                _files="$_files $arg"
                ;;
        esac
    done

    echo "[depend] $_files"

    _compiler=${BT_PROG_CXX:=$BT_PROG_CC}

    if [ "$_append" = "no" ]; then
        runcmd "rm -f .depend"
    fi
    echo "$_compiler -M $_flags $_files >> .depend" >> $Log
    $_compiler -M $_flags $_files >> .depend
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/usr/local/bin/bash
# $Id: install.in,v 1.7 2003/02/05 17:29:14 jmmv Exp $
# bt_logic's install module.
#
# buildtool
# Copyright (c) 2003, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

InstallMode=""
InstallOwner=""
InstallGroup=""
InstallArgs=""

bt_logic_install() {
    _type="$1"; shift

    while [ $# -gt 0 ]; do
        _arg="$1"; shift
        case $_arg in
            -g)
                InstallGroup="$1"; shift
                ;;
            -m)
                InstallMode="$1"; shift
                ;;
            -o)
                InstallOwner="$1"; shift
                ;;
            *)
                InstallArgs="$InstallArgs $_arg"
                ;;
        esac
    done

    bt_logic_install_${_type}
}

setmodes() {
    chmod $1 $4
    if [ "$2" != "NULL" ]; then chown $2 $4; fi
    if [ "$3" != "NULL" ]; then chgrp $3 $4; fi
}

bt_logic_install_bin() {
    _mode=${BT_BIN_MODE:=${InstallMode:=0755}}
    _owner=${BT_BIN_OWN:=${InstallOwner:=NULL}}
    _group=${BT_BIN_GRP:=${InstallGroup:=NULL}}

    bt_logic_install_files bin $_mode $_owner $_group
}

bt_logic_install_data() {
    _mode=${BT_DATA_MODE:=${InstallMode:=0644}}
    _owner=${BT_DATA_OWN:=${InstallOwner:=NULL}}
    _group=${BT_DATA_GRP:=${InstallGroup:=NULL}}

    bt_logic_install_files data $_mode $_owner $_group
}

bt_logic_install_dir() {
    _mode=${BT_DIR_MODE:=${InstallMode:=0755}}
    _owner=${BT_DIR_OWN:=${InstallOwner:=NULL}}
    _group=${BT_DIR_GRP:=${InstallGroup:=NULL}}
    for _d in $InstallArgs; do
        echo "[install-dir, $_mode] $_d"
        if [ ! -d "$_d" ]; then
            mkdir -p $_d
            setmodes $_mode $_owner $_group $_d
        fi
    done
}

bt_logic_install_files() {
    _type="$1"
    _mode="$2"
    _owner="$3"
    _group="$4"

    set -- $InstallArgs
    if [ $# -eq 2 ]; then
        echo "[install-$_type, $_mode] $1 -> $2"
        cp $1 $2
        if [ -d "$2" ]; then
            setmodes $_mode $_owner $_group $2/`basename $1`
        else
            setmodes $_mode $_owner $_group $2
        fi
    else
        _files=""
        while [ $# -gt 1 ]; do
            _files="$_files $1"
            shift
        done
        _targdir="$1"
        if [ ! -d "$_targdir" ]; then
            err "$_targdir missing or not a directory"
        fi

        echo "[install-$_type, $_mode] $_files -> $_targdir"
        for _f in $_files; do
            cp $_f $_targdir
            setmodes $_mode $_owner $_group "$_targdir/`basename $_f`"
        done
    fi
}

bt_logic_install_lib() {
    _mode=${BT_DATA_MODE:=${InstallMode:=0644}}
    _owner=${BT_DATA_OWN:=${InstallOwner:=NULL}}
    _group=${BT_DATA_GRP:=${InstallGroup:=NULL}}

    set -- $InstallArgs
    _libfile="$1"
    _targdir="$2"

    . ./$_libfile

    if [ "$btl_static" = "yes" ]; then
        echo "[install-lib, $_mode, static] $btl_static_name -> $_targdir"
        cp $btl_static_name $_targdir/$btl_static_name
        setmodes $_mode $_owner $_group $_targdir/$btl_static_name
        if [ -n "$BT_PROG_RANLIB" ]; then
            ranlib $_targdir/$btl_static_name
        fi
    fi

    if [ "$btl_pic" = "yes" ]; then
        echo "[install-lib, $_mode, pic] $btl_pic_name $btl_pic_links -> $_targdir"
        cp $btl_pic_name $_targdir/$btl_pic_name
        setmodes $_mode $_owner $_group $_targdir/$btl_pic_name
        for _l in $btl_pic_links; do
            rm -f $_targdir/$_l
            ( cd $_targdir && ln -s $btl_pic_name $_l )
        done
    fi
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/usr/local/bin/bash
# $Id: link.in,v 1.21 2003/02/05 11:27:02 jmmv Exp $
# bt_logic's link module.
#
# buildtool
# Copyright (c) 2003, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Arguments passed just after command's name.
PreArgs=""

# Arguments passed at the end of the call.
PostArgs=""

# Object files.
ObjFiles=""

# List of directories passed through -L.
LibDirs=""

# The target file.
TargetFile=""

# Version of generated library.
LibVer=""

# Main function
bt_logic_link() {
    Type=$1; shift; FakeCmd=$1; shift

    while [ $# -gt 0 ]; do
        arg="$1"; shift
        case $arg in
            -I*)
                # -I flags are useless when linking.

                continue
                ;;

            -L*)
                # We want full paths for -L flags.  Also, we set rpath's for
                # all of them iff the directories are not inside our source
                # tree.

                argdir=`echo $arg | sed -e s/-L//`
                dir=`cd $argdir && pwd`
                PreArgs="$PreArgs -L$dir"
                if [ "$BT_FEATURE_RPATH" = "yes" ]; then
                    if [ -z "`echo $dir | grep ^$BT_TOPDIR`" ]; then
                        PreArgs="$PreArgs ${BT_LINK_FLAG_RPATH}$dir"
                    fi
                fi
                LibDirs="$LibDirs $dir"
                continue
                ;;

            -Wl,-r*|-Wl,-R*|-rpath=*)
                # We want absolute paths for rpath directories, and only allow
                # them if rpath is supported.

                argdir=`echo $arg | sed -e s/-Wl,-r// -e s/-Wl,-R// \
                                        -e s/-rpath=//`
                if [ -z "`echo $argdir | grep ^\/`" ]; then
                    dir=`cd $argdir && pwd`
                else
                    dir="$argdir"
                fi
                if [ "$BT_FEATURE_RPATH" = "yes" ]; then
                    PreArgs="$PreArgs ${BT_LINK_FLAG_RPATH}$dir"
                fi
                LibDirs="$LibDirs $dir"
                continue
                ;;

            -Wl*)
                # Any other linker flags are discarded... this may bring
                # problems...

                continue
                ;;

            -c)
                # Abort linking.  -c is not a valid flag.

                err "-c not allowed in link mode"
                ;;

            -export-dynamic)
                # -export-dynamic is converted to whatever the platform
                # supports.

                PreArgs="$PreArgs $BT_LINK_FLAG_EXPORTDYNAMIC"
                continue
                ;;

            -g)
                # Does nothing in GNU ld, but is kept for compatibility.

                PreArgs="$arg $PreArgs"
                continue
                ;;

            -l*)
                # If using static libraries, we have to resolve -l flags to
                # static archives.  Default libraries set by the linker are
                # not parsed here (like -lc).
                # If using dynamic libraries, just remember the flag.

                if [ "$BT_LOGIC_MKPIC" = "no" ]; then
                    lib="`echo $arg | sed -e s/-l//`"
                    found="no"
                    for d in $LibDirs; do
                        if [ -f "$d/lib${lib}.a" ]; then
                            PostArgs="$PostArgs $d/lib${lib}.a"
                            found="yes"
                            break
                        fi
                    done
                    if [ "$found" = "no" ]; then
                        err "cannot find static library for $arg"
                    fi
                else
                    PostArgs="$PostArgs $arg"
                fi
                continue
                ;;

            -o*)
                # Remember target file name.

                TargetFile=`echo $arg | sed -e s/-o//`
                if [ -z "$TargetFile" ]; then
                    TargetFile="$1"; shift
                fi
                continue
                ;;

            -version-info)
                # -version-info is a non-standard flag, only recognized by us
                # to determine the version we need to set.  Remember it for
                # further processing.

                LibVer="$1"; shift
                ;;

            *)
                # Unknown flags are a very sensible thing.  Emit a warning
                # about them.
                # If we find static archives or object files, remember them.

                if [ -n "`echo $arg | grep -- ^-`" ]; then
                    echo "bt_logic: unknown argument $arg"
                elif [ -n "`echo $arg | grep \.a$`" ]; then
                    PostArgs="$PostArgs $arg"
                else
                    ObjFiles="$ObjFiles $arg"
                fi
                continue
                ;;
        esac
    done

    if [ "$BT_FEATURE_RPATH" = "yes" ]; then
        PreArgs="$PreArgs ${BT_LINK_FLAG_RPATH}${BT_DIR_LIB}"
    fi

    # Generate bt_run
    rm -f $RunScript
    echo "#!${BT_PROG_SH:=/bin/sh}" >> $RunScript
    echo "# Created by bt_logic on `date`" >> $RunScript
    echo >> $RunScript
    echo "LD_LIBRARY_PATH=" >> $RunScript
    for d in $LibDirs; do
        echo "LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:$d\"" >> $RunScript
    done
    echo "LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:${BT_DIR_LIB}\"" >> $RunScript
    echo "export LD_LIBRARY_PATH" >> $RunScript
    echo 'exec $*' >> $RunScript
    chmod +x $RunScript

    bt_logic_link_$Type
}

# Link a program.
bt_logic_link_prog() {
    echo "[link-prog] $ObjFiles -> $TargetFile"
    if [ "$FakeCmd" = "cc" ]; then
        runcmd ${BT_PROG_CC} $PreArgs -o $TargetFile $ObjFiles $PostArgs
    else
        runcmd ${BT_PROG_CXX} $PreArgs -o $TargetFile $ObjFiles $PostArgs
    fi
}

# Link a library.
bt_logic_link_lib() {
    # Strip out the .btl suffix.
    TargetFile="`echo $TargetFile | sed -e s/\\\.btl//`"

    rm -f $TargetFile.btl
    cat > $TargetFile.btl <<EOF
# Buildtool library control file: $TargetFile.btl
# Created by bt_logic on `date`
#

btl_name=$TargetFile
btl_version=$LibVer

btl_static=$BT_LOGIC_MKSTATIC
btl_pic=$BT_LOGIC_MKPIC

EOF

    if [ "$BT_LOGIC_MKSTATIC" = "yes" ]; then
        bt_logic_link_lib_static
    fi

    if [ "$BT_LOGIC_MKPIC" = "yes" ]; then
        bt_logic_link_lib_pic
    fi

    touch $TargetFile.btl
}

bt_logic_link_lib_static() {
    libname="$TargetFile.a"
    echo "[link-lib, static] $ObjFiles -> $libname"
    echo "btl_static_name=\"$libname\"" >> $TargetFile.btl

    # Use ar to generate the archive file.
    runcmd $BT_PROG_AR r $libname $ObjFiles

    # Use ranlib if necessary on the generated archive file.
    if [ -n "${BT_PROG_RANLIB}" ]; then
        runcmd $BT_PROG_RANLIB $libname
    fi
}

bt_logic_link_lib_pic() {
    libname=""
    case $BT_LIB_PIC_NAMING in
        Darwin)
            libname="$TargetFile.$LibVer.A.dylib"
            ;;
        GNU)
            libname="$TargetFile.so.$LibVer"
            libname_major="$TargetFile.so.`echo $LibVer | cut -d '.' -f 1`"
            libname_short="$TargetFile.so"
            if [ "$BT_LIB_PIC_SONAME" = "yes" ]; then
                PreArgs="$PreArgs -soname $libname_major"
            fi
            ;;
        *)
            err "unkown naming scheme for libraries"
            ;;
    esac
    echo "btl_pic_name=\"$libname\"" >> $TargetFile.btl
    echo "btl_pic_links=\"$libname_major $libname_short\"" >> $TargetFile.btl

    # Strip any -Wl, strings that may still remain, as we are calling
    # the linker directly.
    PreArgs="`echo $PreArgs $BT_LIB_PIC_LINK_PREARGS | sed -e 's/-Wl,//g'`"
    PostArgs="`echo $PostArgs $BT_LIB_PIC_LINK_POSTARGS | sed -e 's/-Wl,//g'`"

    # We want pic objects, so translate .o to .po.
    objs="`echo $ObjFiles | sed -e 's/\.o/.po/g'`"

    echo "[link-lib, pic] $objs -> $libname"

    runcmd ${BT_PROG_LD} $PreArgs -o $libname $PostArgs $objs

    rm -f $libname_major && ln -s $libname $libname_major
    rm -f $libname_short && ln -s $libname $libname_short
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
#!/usr/local/bin/bash
# $Id: frontend.in,v 1.8 2003/02/03 22:33:25 jmmv Exp $
# bt_logic's frontend.
#
# buildtool
# Copyright (c) 2003, Julio Merino.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the author nor the names of contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

usage() {
    echo "usage: bt_logic action type command arguments"
    echo ""
    echo "action: compile, link"
    echo "type: prog, lib"
    echo "command: cc, c++"
    exit 1
}

err() {
    echo "bt_logic: $*"
    exit 1
}

runcmd() {
    echo "$*" >> $Log
    $*
    ret=$?; if [ $ret -ne 0 ]; then exit $ret; fi
}

if [ x"$__BUILDTOOL" != x"yes" ]; then
    err "bt_logic: this program must be run through buildtool"
fi

if [ x"$__BUILDTOOL_OWNINSTALL" != x"yes" ]; then
    if [ -z "$BT_TOPDIR" ]; then
        err "BT_TOPDIR is unset; cannot continue"
    fi

    if [ ! -f "${BT_TOPDIR}/bt_config.env" ]; then
        err "bt_generate_configenv required in config file"
    fi

    . ${BT_TOPDIR}/buildtool.d/defs
    . ${BT_TOPDIR}/bt_config.env
fi

Log="./bt_logic.log"
RunScript="./bt_run"
Action="$1"
shift

if [ -f "$Log" ]; then
    echo >> $Log
fi

echo "ACTION: $Action" >> $Log
echo "COMMAND: $*" >> $Log

case $Action in
    compile-lib)
        bt_logic_compile lib $*
        ;;
    compile-prog)
        bt_logic_compile prog $*
        ;;
    depend)
        bt_logic_depend $*
        ;;
    install-bin|install-data|install-dir|install-lib)
        _what="`echo $Action | cut -d '-' -f 2`"
        bt_logic_install $_what $*
        ;;
    link-lib)
        bt_logic_link lib $*
        ;;
    link-prog)
        bt_logic_link prog $*
        ;;
    *)
        err "unknown action"
        ;;
esac
# NOTREACHED

# Local Variables: ***
# mode: shell-script ***
# End: ***
