#!/usr/local/bin/bash
# $Id: buildtool.in,v 1.5 2003/01/23 12:20:28 jmmv Exp $
# buildtool's frontend, wrapper for other utitilies.
#
# buildtool
# Copyright (c) 2002, 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.
#

ProgName="`basename $0`"

usage() {
cat <<EOF
buildtool version 0.10
usage: $ProgName [-h] target [target_args]

Copyright (c) 2002, 2003. Julio Merino
This program is licensed under the terms of the BSD license.

Valid targets:
    config          Auto-configure the package.
    dist            Generate a distribution file (i.e., tar.gz).
    doc             Read package's documentation.
    lint            Validate the package according to recommendations.
    make            Build targets (i.e., build, install).
    maketemplate    Create template files in the current directory.
    siteinfo        Get site specific details.

See buildtool(1) for more information.
EOF
}

copytemplate() {
    local src="$1" dest="$2"

    if [ -f "$dest" ]; then
        echo "file exists $dest"
        return 1
    fi

    echo "copying template $dest"
    cp /usr/local/share/buildtool-0/templates/$src $dest
}

maketemplate() {
    echo "$ProgName: creating template files"
    mkdir -p buildtool.d
    mkdir -p bin
    copytemplate "README.bt" "README.bt"
    copytemplate "defs" "buildtool.d/defs"
    copytemplate "config" "buildtool.d/config"
    copytemplate "config_init" "buildtool.d/config_init"
    copytemplate "docs" "buildtool.d/docs"
    copytemplate "mk_top" "Makefile.bt"
    copytemplate "mk_bin" "bin/Makefile.bt"
}

siteinfo() {
    case "$1" in
        dirlicenses)
            echo "/usr/local/share/buildtool-0/licenses"
            ;;
        dirmk)
            echo "/usr/local/share/buildtool-0/mk"
            ;;
        dirsubr)
            echo "/usr/local/share/buildtool-0/bt_config"
            ;;
        *)
            echo "$ProgName: unknown siteinfo argument"
            echo "Valid arguments include: dirlicenses dirmk dirsubr"
            exit 1
            ;;
    esac
}

check_ctldir() {
    if [ ! -d "./buildtool.d" ]; then
        echo "$ProgName: cannot find buildtool.d control directory"
        exit 1
    fi
    if [ ! -f "./buildtool.d/defs" ]; then
        echo "$ProgName: cannot find buildtool.d/defs"
        exit 1
    fi

    . ./buildtool.d/defs
    if [ -n "$BT_REQUIRE" ]; then
        _major="`echo $BT_REQUIRE | cut -d '.' -f 1`"
        if [ $_major -ne 0 -a $_major -ne 0 ]; then
            echo "$ProgName: this package requires Buildtool $_major.x"
            exit 1
        fi
        _minor="`echo $BT_REQUIRE | cut -d '.' -f 2`"
        if [ $_minor -gt 10 ]; then
            echo "$ProgName: this package requires Buildtool $BT_REQUIRE"
            exit 1
        fi
    fi
}

args=`POSIXLY_CORRECT=yes /usr/bin/getopt ih $*`
if [ $? != 0 ]; then
    echo "Use $ProgName -h to get more information."
    exit 1
fi
set -- $args
while [ $# -gt 0 ]; do
    case "$1" in
        -i)
            IgnoreEnv="yes"
            ;;
        -h)
            usage
            exit 0
            ;;
        --)
            shift; break
            ;;
    esac
    shift
done

if [ -z "$1" ]; then
    echo "$ProgName: requires an argument"
    echo "type \`\`$ProgName -h'' for more information"
    exit 1
fi

Target="$1"
shift
Args="$*"

case "$Target" in
    config)
        check_ctldir
        __BUILDTOOL="yes" /usr/local/bin/bash -c "/usr/local/libexec/buildtool-0/bt_config $Args"
        ;;
    dist)
        check_ctldir
        __BUILDTOOL="yes" /usr/local/bin/bash -c "/usr/local/libexec/buildtool-0/bt_dist $Args"
        ;;
    doc)
        check_ctldir
        __BUILDTOOL="yes" /usr/local/bin/bash -c "/usr/local/libexec/buildtool-0/bt_doc $Args"
        ;;
    lint)
        check_ctldir
        __BUILDTOOL="yes" /usr/local/bin/bash -c "/usr/local/libexec/buildtool-0/bt_lint $Args"
        ;;
    make)
        __BUILDTOOL="yes" /usr/local/libexec/buildtool-0/bt_make -m/usr/local/share/buildtool-0/mk BT_DIR_MODS=/usr/local/libexec/buildtool-0 $Args
        ;;
    maketemplate)
        maketemplate
        ;;
    mkdep)
        __BUILDTOOL="yes" /usr/local/libexec/buildtool-0/bt_mkdep $Args
        ;;
    siteinfo)
        siteinfo $*
        ;;
    *)
        echo "$ProgName: unknown target"
        ;;
esac

exit 0

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