#!/usr/lib/buildtool/bt_sh
# $Id: sh_head.in,v 1.29 2004/05/08 20:05:19 jmmv Exp $
# Common header for shell scripts, including shared functions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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.
#

ProgName=${0##*/}
LogName=

ConfigFile=
DefsFile=
DocsFile=
LogicFile=

# -------------------------------------------------------------------------

#
# btcmn_err message
#   Show error message on stderr and exit with error status.
#
btcmn_err() {
    local spaces="$(echo ${ProgName} | sed -e 's/./ /g')"

    echo "${ProgName}: $1" 1>&2
    shift
    while [ $# -gt 0 ]; do
        echo "${spaces}  $1" 1>&2
        shift
    done
    exit 1
}

# -------------------------------------------------------------------------

#
# btcmn_warn message
#   Show warning message on stderr.
#
btcmn_warn() {
    local spaces="$(echo ${ProgName} | sed -e 's/./ /g')"

    echo "${ProgName}: $1" 1>&2
    shift
    while [ $# -gt 0 ]; do
        echo "${spaces}  $1" 1>&2
        shift
    done
}

# -------------------------------------------------------------------------

#
# btcmn_log_start name
#    Open the given log file and add a header to it for the current
#    session.
#
btcmn_log_start() {
    LogName=$1
    [ -f ${LogName} ] && echo >> ${LogName}
    echo "-------------------------------------------------------------------------" >> ${LogName} # NOLINT
    echo "LOG STARTED FOR MODULE ${ProgName} ON $(date)" >> ${LogName}
    echo >> ${LogName}
}

# -------------------------------------------------------------------------

#
# btcmn_log_name
#    Get the current log name.
#
btcmn_log_name() {
    echo ${LogName}
}

# -------------------------------------------------------------------------

#
# btcmn_log_msg text
#    Append the given message to the log file.
#
btcmn_log_msg() {
    echo $* >> ${LogName}
}

# -------------------------------------------------------------------------

#
# btcmn_req_project
#   Check if we are executing Buildtool inside a buildtoolized project.
#   If so, set BT_TOPDIR to its top directory, set all *File variables
#   pointing to the right script, check if the current Buildtool version
#   is enough to work with this project and automatically source the defs
#   block.  The function does not return if the check fails.
#
btcmn_req_project() {
    local major minor oldpwd

    config() {
        btcmn_err "no config() function defined; cannot continue."
    }

    defs() {
        btcmn_err "no defs() function defined; cannot continue."
    }

    docs() {
        btcmn_err "no docs() function defined; cannot continue."
    }

    oldpwd=$(pwd)
    BT_TOPDIR=
    while [ -z "${BT_TOPDIR}" -a $(pwd) != / ]; do
        if [ -f Generic.bt -o -f Defs.bt ]; then
            BT_TOPDIR=$(pwd)
        else
            cd ..
        fi
    done
    [ -z "${BT_TOPDIR}" ] &&
        btcmn_err \
            "this module needs to be run from inside a Buildtool project"

    if [ -f Config.bt ]; then
        ConfigFile=${BT_TOPDIR}/Config.bt
    else
        ConfigFile=${BT_TOPDIR}/Generic.bt
    fi

    if [ -f Defs.bt ]; then
        DefsFile=${BT_TOPDIR}/Defs.bt
    else
        DefsFile=${BT_TOPDIR}/Generic.bt
    fi

    if [ -f Docs.bt ]; then
        DocsFile=${BT_TOPDIR}/Docs.bt
    else
        DocsFile=${BT_TOPDIR}/Generic.bt
    fi

    if [ -f Logic.bt ]; then
        LogicFile=${BT_TOPDIR}/Logic.bt
    else
        LogicFile=${BT_TOPDIR}/Generic.bt
    fi

    cd ${oldpwd}

    [ -f Logic.bt ] && LogicFile=$(pwd)/Logic.bt

    BT_REQUIRE=
    . ${DefsFile}
    defs
    if [ -z "${BT_REQUIRE}" ]; then
        btcmn_err "broken package; does not define BT_REQUIRE"
    fi

    major=$(echo ${BT_REQUIRE} | cut -d . -f 1)
    minor=$(echo ${BT_REQUIRE} | cut -d . -f 2)

    [ ${major} -ne 0 -a ${major} -ne 0 ] && \
        btcmn_err "this package requires the Buildtool ${major}.x branch"
    [ ${minor} -gt 16 ] && \
        btcmn_err "this package requires Buildtool ${BT_REQUIRE}"
}

# -------------------------------------------------------------------------

#
# btcmn_req_config
#   Check for the presence of the config script and source it if found.
#   Does not return if the file is not found.
#
btcmn_req_config() {
    [ ! -f ${ConfigFile} ] && btcmn_err "cannot open ${ConfigFile}"
    config_init() { return 0; }
    config() { return 0; }
    . ${ConfigFile}
}

# -------------------------------------------------------------------------

#
# btcmn_req_defs
#   Check for the presence of the defs script and source it if found.
#   Does not return if the file is not found.
#
btcmn_req_defs() {
    [ ! -f ${DefsFile} ] && btcmn_err "cannot open ${DefsFile}"
    defs() { return 0; }
    . ${DefsFile}
}

# -------------------------------------------------------------------------

#
# btcmn_req_docs
#   Check for the presence of the docs script and source it if found.
#   Does not return if the file is not found.
#
btcmn_req_docs() {
    [ ! -f ${DocsFile} ] && btcmn_err "cannot open ${DocsFile}"
    docs() { return 0; }
    . ${DocsFile}
}

# -------------------------------------------------------------------------

#
# btcmn_req_logic
#   Check for the presence of the logic script and source it if found.
#   Does not return if the file is not found.
#
btcmn_req_logic() {
    [ ! -f ${LogicFile} ] && btcmn_err "cannot open ${LogicFile}"
    logic() { return 0; }
    . ${LogicFile}
}

# -------------------------------------------------------------------------

#
# btcmn_req_runtime
#   Check if we were executed by the main buildtool wrapper program.
#
btcmn_req_runtime() {
    if [ ${__BUILDTOOL:-no} != yes ]; then
        btcmn_err "this program must be run through Buildtool"
    fi
}

# -------------------------------------------------------------------------

#
# btcmn_run_module module [args]
#   Execute the given module passing all extra arguments to it.
#
btcmn_run_module() {
    local module="$1"; shift

    case ${module} in
        config)
            btcmn_req_project
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_config ${BT_PKG_CONFIG_FLAGS} "$@"
            ;;
        dist)
            btcmn_req_project
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_dist ${BT_PKG_DIST_FLAGS} "$@"
            ;;
        doc)
            btcmn_req_project
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_doc ${BT_PKG_DOC_FLAGS} "$@"
            ;;
        lint)
            btcmn_req_project
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_lint ${BT_PKG_LINT_FLAGS} "$@"
            ;;
        logic)
            btcmn_req_project
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_logic ${BT_PKG_LOGIC_FLAGS} "$@"
            ;;
        pkgflags)
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_pkgflags "$@"
            ;;
        swcgen)
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_swcgen "$@"
            ;;
        wizard)
            __BUILDTOOL=yes /usr/lib/buildtool/bt_sh ${BT_SH_FLAGS} \
                /usr/lib/buildtool/bt_wizard "$@"
            ;;
        *)
            btcmn_err "no such module \`${module}' (internal error)"
            ;;
    esac
}

# -------------------------------------------------------------------------

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: base.in,v 1.30 2004/05/13 18:24:36 jmmv Exp $
# Base functions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_TARGET_DEFS="BT_DEPENDS BT_FIX_PATHS BT_SUBDIR BT_TARGET_FILE BT_TYPE"
BT_TARGETS=
BT_TYPES=

bt_init() {
    local t

    for t in ${BT_AUTOTYPES}; do
        bt_load_type ${t}
    done

    for t in ${BT_STAGES}; do
        BT_TARGET_DEFS+=BT_NO_$(echo ${t} | swcase -u)
    done
}

bt_load_type() {
    local type="${1}" tmp

    for tmp in ${BT_TYPES}; do
        [ ${tmp} = ${type} ] && return
    done

    [ -f "/usr/share/buildtool/bt_logic/${type}.subr" ] ||
        btcmn_err "cannot load \`${type}' type specification"

    . /usr/share/buildtool/bt_logic/${type}.subr
    bt_register_type ${type}
}

bt_register_type() {
    local stage type="${1}"

    BT_TYPES+=${type}

    isfunc ${type}_init && ${type}_init
    isfunc ${type}_defs || eval "${type}_defs() { true; }"
    isfunc ${type}_check || eval "${type}_check() { false; }"
    for stage in ${BT_STAGES}; do
        isfunc ${type}_${stage} || eval "${type}_${stage}() { true; }"
    done
}

bt_is_oodate() {
    local stage="${1}" phony="${2}" target="${3}"
    local dep res

    res=1
    for dep in ${BT_DEPENDS}; do
        bt_do_target ${stage} ${phony} ${dep} ${BT_TARGET_FILE} && res=0
    done

    if [ ${phony} = yes ] || [ ! -f ${BT_TARGET_FILE} ]; then
        res=0
    fi

    return ${res}
}

bt_fix_paths() {
    local f new values

    [ ${BT_TOPDIR} = ${BT_WORKDIR} ] && return 0

    set -- ${BT_FIX_PATHS}
    while [ $# -gt 0 ]; do
        eval values=\"\$$1\"
        new=
        for f in ${values}; do
            if [ -f ${f} ]; then
                new+=${f}
            elif [ -f ${BT_SRCDIR}/${f} ]; then
                new+=${BT_SRCDIR}/${f}
            else
                new+=${f}
            fi
        done
        eval $1=\'${new}\'
        shift
    done
}

bt_enter_obj() {
    BT_SRCDIR=$(pwd)
    BT_OBJDIR=${BT_WORKDIR}/${BT_SRCDIR##${BT_TOPDIR}}
    if [ ! -d ${BT_OBJDIR} ]; then
        mkdir -p ${BT_OBJDIR}
        btcmn_warn "created object directory ${BT_OBJDIR}"
    fi
    cd ${BT_OBJDIR}
}

bt_leave_obj() {
    cd ${BT_SRCDIR}
}

bt_target() {
    while [ ${#} -gt 0 ]; do
        BT_TARGETS+=${1}
        shift
    done
}

bt_try_auto() {
    local stage="${1}" phony="${2}" target="${3}" dependent="${4}"

    if [ -d ${BT_SRCDIR}/${target} ]; then
        btcmn_warn "entering directory \`${target}' for \`${stage}'"
        ( cd ${BT_SRCDIR}/${target} && /usr/lib/buildtool/bt_logic \
          --destdir=${BT_DESTDIR} -w ${BT_WORKDIR} -s ${stage} )
            #btcmn_err "\`${stage}' failed in \`${target}' directory"
        res=$?
        btcmn_warn "leaving directory \`${target}' for \`${stage}'"
        return ${res}
    elif echo ${target} | grep / >/dev/null 2>&1; then
        oodate ${dependent} ${target}
        return
    fi

    local type
    for type in ${BT_AUTOTYPES}; do
        if ${type}_check ${target}; then
            eval "target_${target}() { BT_TYPE='${type}'; }"
            return
        fi
    done

    if [ ! -f ${BT_SRCDIR}/${target} ]; then
        btcmn_err "don't know how to \`${stage}' target \`${target}'"
    else
        oodate ${dependent} ${target}
    fi
}

# Returns 0 if some target was remade; 1 otherwise.
bt_do_target() {
    local stage="${1}" phony="${2}" target="${3}" dependent="${4}"
    local oldpwd= res t tmp var

    if ! isfunc target_${target}; then
        bt_try_auto $*
        res=$?
        isfunc target_${target} || return ${res}
    fi

    for var in ${BT_TARGET_DEFS}; do
        local ${var}
        eval ${var}=\"\"
    done
    for var in ${BT_TARGET_VARS}; do
        local ${var}
    done
    BT_TARGET_FILE="${target}"
    target_${target}

    eval tmp=\"\${BT_NO_$(echo ${stage} | swcase -u)}\"
    [ "${tmp}" = yes ] && return 1

    : ${BT_TYPE:=null}

    if [ -n "${BT_SUBDIR}" ]; then
        btcmn_warn "entering directory \`${BT_SUBDIR}' for \`${stage}'"
        olddir=$(pwd)
        cd ${BT_SUBDIR}
        bt_enter_obj
    fi

    bt_load_type ${BT_TYPE}
    ${BT_TYPE}_defs ${target}

    bt_fix_paths

    res=1
    if bt_is_oodate ${stage} ${phony} ${target}; then
        isfunc target_${target}_pre_${stage} && target_${target}_pre_${stage}
        if isfunc target_${target}_${stage}; then
            target_${target}_${stage}
        else
            ${BT_TYPE}_${stage}
        fi
        isfunc target_${target}_post_${stage} && target_${target}_post_${stage}
        res=0
    fi

    if [ -n "${BT_SUBDIR}" ]; then
        btcmn_warn "leaving directory \`${BT_SUBDIR}' for \`${stage}'"
        bt_leave_obj
        cd ${olddir}
    fi

    return ${res}
}

bt_run_hooks() {
    local var="${1}"
    local f funcs

    eval funcs=\"\$${var}\"
    for f in ${funcs}; do
        ${f}
    done
}

bt_source_depends() {
    local dep obj src

    for src in ${*}; do
        if [ ${BT_LIB_MKPIC} = yes ]; then
            obj=$(echo ${src} | sed -e 's|\.c$|.po|' -e 's|\.cpp$|.po|' \
                  -e 's|\.cc$|.po|')
        else
            obj=$(echo ${src} | sed -e 's|\.c$|.o|' -e 's|\.cpp$|.o|' \
                  -e 's|\.cc$|.o|')
        fi
        dep=$(echo ${src} | sed -e 's|\.c$|.dep|' -e 's|\.cpp$|.dep|' \
              -e 's|\.cc$|.dep|')
        eval "target_${dep}() { BT_TYPE=depend; BT_SOURCES='${src}'; }"
        eval "target_${obj}() { BT_TYPE=convert; }"
        BT_DEPENDS+="${dep} ${obj}"
        BT_OBJECTS+=${obj}
    done

    BT_FIX_PATHS+=BT_SOURCES
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: cmds.in,v 1.9 2004/05/14 13:27:20 jmmv Exp $
# Functions to indirectly call system commands.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_die() {
    btcmn_err "process stopped; command exited with error status \`$?'"
}

bt_exec() {
    [ -n "${LogFile}" ] && echo "[exec] $*" >> ${LogFile}
    echo "[exec] $*"
    $* || bt_die
}

bt_remove() {
    local f files=

    for f in $*; do
        [ -f "${f}" ] && files+=${f}
    done

    if [ -n "${files}" ]; then
        [ -n "${LogFile}" ] && echo "[remove] ${files}" >> ${LogFile}
        echo "[remove] ${files}"
        rm -f ${files}
    fi
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: compile.in,v 1.39 2004/02/03 22:59:10 jmmv Exp $
# C/C++ code compilation functions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_TARGET_VARS+="BT_FLAGS_CPP BT_FLAGS_CC BT_FLAGS_CXX"

btown_compile_parse_args() {
    local arg=
    local res=
    local obj= src=
    local flags_D= flags_I= flags_O= flags_W= flags_other= flags_unknown=

    [ -n "${LogFile}" ] && echo "[compile: original call] $*" >> ${LogFile}

    while [ $# -gt 0 ]; do
        arg=$(echo ${1} | sed -e 's|"|\\"|g')
        case ${arg} in
            -D*)
                flags_D+=${arg}
                ;;

            -I*)
                arg=$(echo ${arg} | sed -e s/-I//)
                [ -d ${arg} ] && flags_I="${flags_I} -I$(cd ${arg} && pwd)"
                ;;

            -L*|-l*|-Wl*)
                ;;

            -O*)
                flags_O+=${arg}
                ;;

            -W*)
                flags_W+=${arg}
                ;;

            -c)
                ;;

            -g)
                flags_other+=${arg}
                ;;

            -o)
                obj=${2}; shift
                ;;

            *)
                if echo ${arg} | grep '^-' >/dev/null; then
                    btcmn_warn "compile: passing unhandled option \`${arg}'"
                    flags_unknown+=${arg}
                else
                    src=${arg}
                fi
                ;;
        esac
        shift
    done

    if echo ${obj} | grep '.po$' >/dev/null; then
        flags_other+=${BT_LIB_PIC_COMPILE_PREARGS}
    fi

    res="${flags_D} ${flags_I} ${flags_W} ${flags_O} ${flags_other} ${flags_unknown} -o ${obj} -c ${src}" # NOLINT
    [ -n "${LogFile}" ] && \
        echo "[compile: call rewrote as] ${res}" >> ${LogFile}
    echo ${res}
}

bt_compile_c() {
    local cmd

    [ -z ${BT_PROG_CC} ] && btcmn_err \
        "compile: no C compiler available (BT_PROG_CC is not set)" \
        "         add a call to bt_check_env_c in your config script"

    cmd="${BT_PROG_CC} $(btown_compile_parse_args $*)"
    echo "[compile] ${cmd}"
    ${cmd} || bt_die
}

bt_compile_cxx() {
    local cmd

    [ -z ${BT_PROG_CXX} ] && btcmn_err \
        "compile: no C++ compiler available (BT_PROG_CXX is not set)" \
        "         add a call to bt_check_env_c in your config script"

    cmd="${BT_PROG_CXX} $(btown_compile_parse_args $*)"
    echo "[compile] ${cmd}"
    ${cmd} || bt_die
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: docs.in,v 1.9 2004/05/09 19:24:28 jmmv Exp $
# Functions to handle package documents.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_DOCS=
BT_INSTALL_DOCS=yes

bt_doc() {
    BT_DOCS+=${1}
}

bt_deinstall_docs() {
    local f

    for f in ${BT_DOCS}; do
        bt_remove ${BT_DIR_DOC}/${f}
    done
    rmdir -p ${BT_DIR_DOC} >/dev/null 2>&1 || true
}

bt_install_docs() {
    local f

    [ -n "${BT_DOCS}" ] && bt_install_dir ${BT_DIR_DOC}
    for f in ${BT_DOCS}; do
        bt_install_data ${f} ${BT_DIR_DOC}
    done
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: install.in,v 1.23 2004/05/13 18:24:36 jmmv Exp $
# Generic install functions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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.
#

btown_install_parse_args() {
    local group mode owner

    group="${1}"; mode="${2}"; owner="${3}"; shift 3

    while [ $# -gt 0 ]; do
        case ${1} in
            -g)
                group=${2}; shift
                ;;
            -m)
                mode=${2}; shift
                ;;
            -o)
                owner=${2}; shift
                ;;
            *)
                if echo ${1} | grep '^-' >/dev/null; then
                    btcmn_err "install: unknown option \`${1}'"
                else
                    break
                fi
                ;;
        esac
        shift
    done

    echo ${group} ${mode} ${owner} $*
}

btown_install_files() {
    local dir f msg srcs target

    msg=${1}; shift

    if [ $# -eq 2 ]; then
        if [ -d ${BT_DESTDIR}${2} ]; then
            target=${BT_DESTDIR}${2}/${1##*/}
        else
            target=${BT_DESTDIR}${2}
        fi
        echo "[install] installing ${msg} file ${target}"
        cp -f ${1} ${target} || bt_die
        chmod ${mode} ${target} || bt_die
        if [ ${owner} != DEFAULT ]; then
            chown ${owner} ${target} || bt_die
        fi
        if [ ${group} != DEFAULT ]; then
            chgrp ${group} ${target} || bt_die
        fi
    else
        srcs=
        while [ $# -gt 1 ]; do
            srcs+=${1}
            shift
        done
        dir="${BT_DESTDIR}${1}"

        [ ! -d "${dir}" ] && \
            btcmn_err "install: last argument must be a directory"

        for f in ${srcs}; do
            target=${dir}/${f##*/}
            echo "[install] installing ${msg} file ${target}"
            cp -f ${f} ${target} || bt_die
            chmod ${mode} ${target} || bt_die
            if [ ${owner} != DEFAULT ]; then
                chown ${owner} ${target} || bt_die
            fi
            if [ ${group} != DEFAULT ]; then
                chgrp ${group} ${target} || bt_die
            fi
        done
    fi
}

bt_install_bin() {
    local group mode owner

    set -- $(btown_install_parse_args ${BT_BIN_GROUP} ${BT_BIN_MODE} \
        ${BT_BIN_OWNER} $*)

    group="${1}"; mode="${2}"; owner="${3}"; shift 3

    btown_install_files binary $*
}

bt_install_data() {
    local group mode owner

    set -- $(btown_install_parse_args ${BT_DATA_GROUP} ${BT_DATA_MODE} \
        ${BT_DATA_OWNER} $*)

    group="${1}"; mode="${2}"; owner="${3}"; shift 3

    btown_install_files data $*
}

bt_install_dir() {
    local dir group mode owner

    set -- $(btown_install_parse_args ${BT_DIR_GROUP} ${BT_DIR_MODE} \
        ${BT_DIR_OWNER} $*)

    group="${1}"; mode="${2}"; owner="${3}"; shift 3

    while [ $# -gt 0 ]; do
        dir=${BT_DESTDIR}${1}
        if [ ! -d ${dir} ]; then
            echo "[install] creating missing directory ${dir}"
            mkdir -p ${dir} || bt_die
            chmod ${mode} ${dir} || bt_die
            if [ ${owner} != DEFAULT ]; then
                chown ${owner} ${dir} || bt_die
            fi
            if [ ${group} != DEFAULT ]; then
                chgrp ${group} ${dir} || bt_die
            fi
        fi
        shift
    done
}

bt_install_symlink() {
    bt_exec ln -fs $* || bt_die
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: link.in,v 1.49 2004/03/24 18:36:01 jmmv Exp $
# C/C++ object linking functions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_TARGET_VARS+="BT_FLAGS_LD BT_LIBS"

btown_link_parse_args() {
    local arg=
    local res=
    local archives= libs= objs= targets=
    local dirs_libs= dirs_rpath=
    local flags_L= flags_other= flags_rpath= flags_unknown=
    local shared=no

    [ -n "${LogFile}" ] && echo "[link: original call] $*" >> ${LogFile}

    while [ $# -gt 0 ]; do
        arg=$(echo ${1} | sed -e 's|"|\\"|g')
        case ${arg} in
            -L*)
                arg=$(echo ${arg} | sed -e s/-L//)
                if [ -d ${arg} ]; then
                    arg=$(cd ${arg} && pwd)
                    flags_L+=-L${arg}
                    dirs_libs+=${arg}
                    [ ${arg##${BT_WORKDIR}} = ${arg} ] && dirs_rpath+=${arg}
                fi
                ;;

            -Wl,-r*|-Wl,-R*|-rpath=*)
                arg=$(echo ${arg} | sed -e s/-Wl,-r// -e s/-Wl,-R// \
                    -e s/-rpath=//)
                [ -d ${arg} ] && arg=$(cd ${arg} && pwd)
                dirs_rpath+=${arg}
                ;;

            -export-dynamic)
                flags_other+=${BT_LIB_FLAG_EXPORTDYNAMIC}
                ;;

            -l*)
                if [ ${BT_LIB_MKPIC} = yes ]; then
                    libs+=${arg}
                else
                    local d found=no
                    arg=$(echo ${arg} | sed -e s/-l//)
                    for d in ${dirs_libs}; do
                        if [ -f ${d}/lib${arg}.a ]; then
                            archives+=${d}/lib${arg}.a
                            found=yes
                            break
                        fi
                    done
                    [ ${found} = no ] && libs="${libs} ${arg}"
                fi
                ;;

            -o)
                target=${2}; shift
                ;;

            -shared)
                shared=yes
                ;;

            -soname=*)
                arg=$(echo ${arg} | sed -e 's/-soname=//')
                if [ ${BT_LIB_PIC_SONAME} = yes ]; then
                    flags_other="-Wl,-soname=${arg}"
                fi
                shared=yes
                ;;

            *)
                if echo ${arg} | grep '^-' >/dev/null; then
                    btcmn_warn "link: passing unhandled option \`${arg}'"
                    flags_unknown+=${arg}
                elif echo ${arg} | grep '.a$' >/dev/null; then
                    archives+=${arg}
                else
                    objects+=${arg}
                fi
                ;;
        esac
        shift
    done

    if [ ${BT_FEATURE_RPATH} = yes ]; then
        local d
        for d in ${BT_DIR_LIB} ${dirs_rpath}; do
            flags_rpath+=${BT_LINK_FLAG_RPATH}${d}
        done
    fi

    if [ ${shared} = yes ]; then
        flags_other="-shared ${flags_other}"
    fi

    res="${flags_L} ${flags_rpath} ${flags_other} ${flags_unknown} -o ${target} ${objects} ${archives} ${libs}" # NOLINT
    [ -n "${LogFile}" ] && echo "[link: call rewrote as] ${res}" >> ${LogFile}
    echo ${res}
}

bt_link_c() {
    local cmd

    [ -z ${BT_PROG_CC} ] && btcmn_err \
        "link: no C linker available (BT_PROG_CC is not set)" \
        "      add a call to bt_check_env_c in your config script"

    cmd="${BT_PROG_CC} $(btown_link_parse_args $*)"
    echo "[link] ${cmd}"
    ${cmd} || bt_die
}

bt_link_cxx() {
    local cmd

    [ -z ${BT_PROG_CXX} ] && btcmn_err \
        "link: no C++ linker available (BT_PROG_CXX is not set)" \
        "      add a call to bt_check_env_c in your config script"

    cmd="${BT_PROG_CXX} $(btown_link_parse_args $*)"
    echo "[link] ${cmd}"
    ${cmd} || bt_die
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: stages.in,v 1.18 2004/06/07 17:29:26 jmmv Exp $
# Standard stage definitions.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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_STAGES="build clean cleandir deinstall install test"

stage_build() {
    local res

    bt_run_hooks BT_PRE_BUILD
    res=1
    while [ ${#} -gt 0 ]; do
        bt_do_target build no ${1} && res=0
        shift
    done
    bt_run_hooks BT_POST_BUILD

    return ${res}
}

stage_clean() {
    bt_run_hooks BT_PRE_CLEAN
    while [ ${#} -gt 0 ]; do
        bt_do_target clean yes ${1}
        shift
    done
    bt_run_hooks BT_POST_CLEAN

    return 0
}

stage_cleandir() {
    bt_run_hooks BT_PRE_CLEANDIR
    while [ ${#} -gt 0 ]; do
        bt_do_target clean yes ${1}
        bt_do_target cleandir yes ${1}
        rmdir ${BT_OBJDIR} 2>/dev/null
        shift
    done
    bt_run_hooks BT_POST_CLEANDIR

    rmdir ${BT_WORKDIR} 2>/dev/null

    return 0
}

stage_deinstall() {
    bt_run_hooks BT_PRE_DEINSTALL
    while [ ${#} -gt 0 ]; do
        bt_do_target deinstall yes ${1}
        shift
    done
    bt_run_hooks BT_POST_DEINSTALL

    return 0
}

stage_install() {
    bt_run_hooks BT_PRE_INSTALL
    while [ ${#} -gt 0 ]; do
        bt_do_target install yes ${1}
        shift
    done
    bt_run_hooks BT_POST_INSTALL

    return 0
}

stage_test() {
    bt_run_hooks BT_PRE_TEST
    while [ ${#} -gt 0 ]; do
        bt_do_target test yes ${1}
        shift
    done
    bt_run_hooks BT_POST_TEST

    return 0
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: frontend.in,v 1.44 2004/05/14 15:13:38 jmmv Exp $
# bt_logic's frontend.
#
# buildtool
# Copyright (c) 2003, 2004 Julio M. Merino Vidal.
#
# 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() {
    local exitstatus=${1}
    local detailed=${2}

    [ ${detailed} = yes ] && echo "buildtool version 0.16" 1>&2

    echo "usage: ${ProgName} [extra opts] {-s ,--stage=}name" \
        "[target1 ... targetN]" 1>&2

    if [ ${detailed} = yes ]; then
        cat 1>&2 <<EOF

Available options:
    {-d |--destdir=}dir     Set installation destination (only during install).
    {-f |--file=}file       Set script name.
    {-h|--help}             Show this help message.
    --ignore-sw-config      Ignore system wide configuration file.
    {-l |--logfile=}file    Set log file name and enable logging.
    {-s |--stage=}name      Specifiy stage to be executed.
    {-w |--workdir=}dir     Set work directory.

See buildtool(1) for more information.
EOF
    fi

    exit ${exitstatus}
}

main() {
    local load_sw_config

    btcmn_req_runtime

    load_sw_config=yes
    scrfile=
    LogFile=
    Stage=

    BT_DESTDIR=
    BT_WORKDIR=

    while [ $# -gt 0 ]; do
        [ -z "$(echo ${1} | grep ^-)" ] && break

        case "${1}" in
            -d)
                BT_DESTDIR="${2}"; shift
                ;;
            --destdir=*)
                BT_DESTDIR=$(echo ${1} | sed -e 's|--destdir=||')
                ;;
            -f)
                if echo ${2} | grep / >/dev/null; then
                    scrfile="./${2}"
                else
                    scrfile="${2}"
                fi
                shift
                ;;
            --file=*)
                arg=$(echo ${1} | sed -e 's|--file=||')
                if echo ${arg} | grep / >/dev/null; then
                    scrfile="./${arg}"
                else
                    scrfile="${arg}"
                fi
                ;;
            --ignore-sw-config)
                load_sw_config=no
                ;;
            -h|--help)
                usage 0 yes
                ;;
            -l)
                LogFile="${2}"; shift
                ;;
            --logfile=*)
                LogFile=$(echo ${1} | sed -e 's|--logfile=||')
                ;;
            -s)
                Stage="${2}"; shift
                ;;
            --stage=*)
                Stage=$(echo ${1} | sed -e 's|--stage=||')
                ;;
            -w)
                BT_WORKDIR="${2}"; shift
                ;;
            --workdir=*)
                BT_WORKDIR=$(echo ${1} | sed -e 's|--workdir=||')
                ;;
            --)
                shift; break
                ;;
            *)
                btcmn_warn "unknown option \`${1}'"
                usage 1 no
                ;;
        esac
        shift
    done

    [ ${Stage} != install -a -n "${BT_DESTDIR}" ] && \
        btcmn_err "-d (--destdir) can only be used during the \`install' stage"

    btcmn_req_project

    [ -n "${scrfile}" ] && LogicFile="${scrfile}"
    if [ -z "${BT_WORKDIR}" ]; then
        if [ -d "${BT_TOPDIR}/work.bt" -a \
             -f "${BT_TOPDIR}/work.bt/bt_config.env" ]
        then
            BT_WORKDIR=${BT_TOPDIR}/work.bt
        else
            BT_WORKDIR=${BT_TOPDIR}
        fi
    fi

    # Load system configuration
    if [ ${load_sw_config} = yes -a -f "/etc/buildtool/bt_logic.conf" ]; then
        echo "bt_logic: loading system-wide configuration"
        . "/etc/buildtool/bt_logic.conf"
    fi

    if [ ! -f "${BT_WORKDIR}/bt_config.env" ]; then
        btcmn_err "the package is not configured (use \`buildtool config')" \
            "(or maybe you forgot to specify the work directory?)"
    fi
    . ${BT_WORKDIR}/bt_config.env
    [ -f ${BT_WORKDIR}/bt_logic.env ] && . ${BT_WORKDIR}/bt_logic.env

    bt_enter_obj

    bt_init

    btcmn_req_docs
    btcmn_req_logic

    [ ${BT_FEATURE_DEVELOPER} = yes -a -z "${LogFile}" ] && \
        LogFile=${BT_OBJDIR}/.bt_logic.log

    docs
    logic

    _BT_CMDTARGETS="$*"
    if [ $# -gt 0 ]; then
        BT_TARGETS=
        bt_target $*
    fi

    local res
    if ! isfunc stage_${Stage}; then
        btcmn_warn "unknown stage \`${Stage}'"
        res=1
    else
        if [ -n "${LogFile}" ]; then
            [ -f "${LogFile}" ] && echo >> ${LogFile}
            echo "---------------------------------------------------------------------------" >> ${LogFile} # NOLINT
            echo "bt_logic session started at $(date)" >> ${LogFile}
            echo >> ${LogFile}
        fi
        eval stage_${Stage} ${BT_TARGETS}
        res=$?
    fi

    return ${res}
}

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
#
# $Id: sh_tail.in,v 1.3 2003/09/23 16:40:23 jmmv Exp $
# Common footer for shell scripts.
#
# buildtool
# Copyright (c) 2003 Julio M. Merino Vidal.
#
# 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.
#

main "$@"

# Local Variables: ***
# mode: shell-script ***
# End: ***
# vim: syntax=sh
