#! /bin/sh

# Run this program to setup the build environment.

# $Progeny$

# This program is in the public domain.

# Too bad we don't have something like sysexits.h for POSIX sh...
EX_USAGE=64

usage ()
{
    cat <<EOF
usage: $0
       $0 (aclocal|autoheader|autoconf) ...

Setup the build environment.  If arguments are given, only those actions
are performed.
EOF
}

do_aclocal ()
{
    echo "cat $@ > aclocal.m4"
    cat "$@" > aclocal.m4
    return $?
}

do_autoheader ()
{
    echo "autoheader && touch config.h.in"
    autoheader && touch config.h.in
    return $?
}

do_autoconf ()
{
    echo "autoconf"
    autoconf
    return $?
}

###############################################################################

buildtools=buildtools
aclibs="${buildtools}/check.m4"
aclibs="${aclibs} ${buildtools}/config-script.m4"
aclibs="${aclibs} ${buildtools}/libtool.m4"
aclibs="${aclibs} ${buildtools}/show-config.m4"
aclibs="${aclibs} ${buildtools}/java/ac_check_class.m4"
aclibs="${aclibs} ${buildtools}/java/ac_check_classpath.m4"
aclibs="${aclibs} ${buildtools}/java/ac_check_junit.m4"
aclibs="${aclibs} ${buildtools}/java/ac_check_rqrd_class.m4"
aclibs="${aclibs} ${buildtools}/java/ac_java_options.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_jar.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_java.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_java_cc.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_java_works.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_javac.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_javac_works.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_javadoc.m4"
aclibs="${aclibs} ${buildtools}/java/ac_prog_javah.m4"
aclibs="${aclibs} ${buildtools}/java/ac_try_compile_java.m4"
aclibs="${aclibs} ${buildtools}/java/ac_try_run_javac.m4"
aclibs="${aclibs} ${buildtools}/python/ac_py_check_lib.m4"
aclibs="${aclibs} ${buildtools}/python/ac_prog_python.m4"
aclibs="${aclibs} ${buildtools}/python/ac_prog_python_works.m4"
aclibs="${aclibs} ${buildtools}/python/ac_py_lib_path.m4"

if [ -f aclibs ]; then
    aclibs=$(sed -e 's/#.*//' aclibs)
fi

if [ $# -eq 0 ]; then
    set aclocal autoheader autoconf
fi

while [ $# -gt 0 ]; do
    case $1 in
    aclocal)
        do_aclocal ${aclibs}
        ;;
    autoheader)
        do_autoheader
        ;;
    autoconf)
        do_autoconf
        ;;
    *)
        usage
        exit ${EX_USAGE}
        ;;
    esac

    result=$?
    if [ ${result} -ne 0 ]; then
        echo "$0: $1 failed."
        exit ${result}
    fi

    shift
done

exit 0
