#!/bin/sh -f
do_nothing() {
    return 0;
}

device () {
    df -k $1 | grep -v '^Filesystem' | awk '{print $1;}'
}

inumber () {
    ls -id "$1" | awk '{print $1;}'
}

#
# This function returns:
#    0 if the files are identical,
#  100 if either of the parameters are the empty string.
#  101 if either of the parameters does not name a directory
#  102 if both parameters name different directories.
#
files_are_equal() {
    local inode1 inode2 dev1 dev2
    if [ -z "$1" ] || [ -z "$2" ] ; then
        return 100;
    fi
    #
    # For the purposes of this script, if either directory
    # does not exist, then they are not equal.
    #
    if [ ! -d "$1" ] || [ ! -d "$2" ] ; then
        return 101
    fi
    inode1=`inumber $1`
    inode2=`inumber $2`
    dev1=`device $1`
    dev2=`device $2`
    [ "$dev1" = "$dev2" ] && [ "$inode1" = "$inode2" ] && return 0
    return 102
}

rm="/bin/rm -rf"
DONE=NO
DO_CONFIGURE=NO
DO_BUILD=NO
DO_CLEAN=NO
DO_GENERATE=NO
BUILD_DIRECTORY=build
MAKE_HISTORY_FILE=make.hst
CONFIGURE_OPTIONS=
BUILD_IS_DOT=NO
while [ ! -z "$1" ] && [ "$DONE" != YES ] ; do
    case "$1" in
    --help)
        echo 'usage: chores.3dfx [options]'
        echo '    Handle some 3dfx specific configuration chores'
        echo 'Options:'
        echo '    --help               This note.'
        echo '    --clean              Delete some files in the source'
        echo '                         directory.  Since we do not generally'
        echo '                         run make in the source directory,'
        echo '                         the "make clean" rules are not so'
        echo '                         useful there.  The files deleted'
        echo '                         are:'
        echo '                             configure,'
        echo '                             makefile.autoconf.{in},'
        echo '                             aclocal.m4'
        echo '                             *~, #*#, xx, yy, zz'
        echo '                         Note that you need to put local macros'
        echo '                         in acinclude.m4, not aclocal.m4.  The'
        echo '                         former is included in the latter.'
        echo '    --debug-build        Add configuration option for a'
        echo '                         debug build.'
        echo '    --texus2             Add configuration option to use'
        echo '                         the texus2 library.  (Do not use this'
        echo '                         unless you know what you are doing.)'
        echo '    --architecture=arch  Configure for the given architecture.'
        echo '                         Equivalent to giving'
        echo '                             --enable-build-architecture=arch'
        echo '                         to "configure".'
        echo '    --generate:          Generate configure script and'
        echo '                         makefile.autoconf.in files.'
        echo '                         This is essentially just:'
        echo '                           aclocal; automake; autoconf'
        echo '    --build-dir=dir'
        echo '                         Set the build directory to dir.'
        echo '    --configure="configure options"'
        echo '                         Configure in the build directory'
        echo '                         with the given options.'
        echo '                         NOTE: this will delete and recreate'
        echo '                               your build directory if it'
        echo '                               exists.'
        echo '    --build[=dirname]    Build in the given build directory.'
        echo '                           o The default directory name is'
        echo '                             named "build".'
        echo '                           o The directory does not need to'
        echo '                             be a subdirectory of the source'
        echo '                             directory.'
        echo '                         This option is equivalent to specifying'
        echo '                         --build-directory=dirname --build'
        echo ''
        echo 'Note that the operations are done in logical order, not'
        echo 'in the order that the operations are specified on the'
        echo 'command line.'
        echo ''
        exit 100
        ;;
    --debug-build)
        CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS --enable-fx-debug"
        shift
        ;;
    --texus2)
        CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS --enable-fx-texlib=texus2"
        shift
        ;;
    --generate)
        DO_GENERATE=YES
        shift
        ;;
    --arch*=*)
        CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS --enable-build-architecture=`echo $1 | sed s/--arch[^=]*=//`"
        shift
        ;;
    --configure)
        DO_CONFIGURE=YES
        shift
        ;;
    --configure=*)
        DO_CONFIGURE=YES
        CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS `echo $1 | sed s/--configure=//`"
        shift
        ;;
    --build-dir*=*)
        BUILD_DIRECTORY=`echo $1 | sed 's/--build-dir[^=]*=//'`
        shift
        ;;
    --build=*)
        DO_BUILD=YES
        BUILD_DIRECTORY=`echo $1 | sed s/--build=//`
        shift
        ;;
    --build)
        DO_BUILD=YES
        shift
        ;;
    --clean)
        DO_CLEAN=YES
        shift
        ;;
    --make-hst=*)
        MAKE_HISTORY_FILE=`echo "$1" | sed s/--make-hst=//'`
        shift
        ;;
    *)
        echo "chores.3dfx: Unknown command line parameter $1"
        exit 100
        ;;
    esac
done
if [ "$DO_CLEAN" = YES ] ; then
    #
    # Remove detritus.
    #
    echo -n 'Removing detritus...'
    find . '(' -name config.cache -o -name config.status -o -name build.3dfx -o -name config.h -o -name stamp-h -name config.log -o -name aclocal.m4 -o -name configure -o -name '*~' -o -name '#*#' -o -name makefile.autoconf.in -o -name xx -o -name yy -o -name zz ')' -a -exec $rm {} \;
    echo 'Done'
fi
if [ "$DO_GENERATE" = YES ] ; then
    #
    # Regenerate everything.
    #
    echo -n 'Regenerating everything...'
    echo -n 'Aclocal...'
    if aclocal ; then
        do_nothing
    else
        echo 'Failed!!'
        exit 100
    fi
    echo -n 'Automake...'
    if automake ; then
        do_nothing
    else
        echo 'Failed!!'
        exit 100
    fi
    echo -n 'Autoconf...'
    if autoconf ; then
        do_nothing
    else
        echo 'Failed!!'
        exit 100
    fi
    echo 'Done.'
fi
#
# See if the build directory is the current directory.
#
if files_are_equal "${PWD}" "${BUILD_DIRECTORY}" ; then
    BUILD_IS_DOT=YES
fi
if [ "$DO_CONFIGURE" = YES ] ; then
    if [ "$BUILD_IS_DOT" = NO ] ; then
        #
        # Get rid of the build directory.
        #
        echo -n 'Getting rid of the old build directory...'
        /bin/rm -rf $BUILD_DIRECTORY
        echo 'Done.'
    else
        echo 'Old build directory is current directory.'
    fi
    if [ ! -d "$BUILD_DIRECTORY" ] ; then
        #
        # Now, make a build directory, and configure in it.
        #
        echo -n "Making build directory $BUILD_DIRECTORY..."
        mkdir -p $BUILD_DIRECTORY
        echo 'Done'
    fi
    #
    # Configure.
    #
    echo -n "Configuring..."
    SRCDIR="${PWD}"
    (cd $BUILD_DIRECTORY; ${SRCDIR}/configure --quiet $CONFIGURE_OPTIONS)
    echo 'Done.'
fi
if [ "$DO_BUILD" = YES ] ; then
    #
    # Build in it.
    #
    cd $BUILD_DIRECTORY
    ./build.3dfx all | tee $MAKE_HISTORY_FILE
fi
