#!/bin/sh
##
## build www homepage access counter
## Aug-26-1999
## it's broken again. geez how many times I screwed this up??
##  --mm, Sep-17-1999

argc=$#
me=`basename $0`

common_libs="mutils bigplus sll combine"
common_utils="mkstrip extdgts mwhich"

db_libs="sdbm ldbm"
db_utils="dumpdb editdb rgbtxt2db"

#config_options=${1+"$@"}
probe_db="yes"
probe_ttf="yes"

bindir="./bin"
rm="/bin/rm -f"
ls="/bin/ls -lt"

if test $# -lt 1; then

cat<<EOM
========================================================================
NOTE: this script is nothing but a nice wrapper around ./configure.
If you know what "configure" is, use that instead to have more control.
========================================================================

    Usage: $me [options]

    where the options include:
       --all                        compile with default settings

       --with-cgi-bin-dir[=DIR]     designated directory for CGI programs,
                                    default is /usr/local/apache/cgi-bin

       --prefix=dir                 counter base directory, default is
                                    /usr/local/etc/Counter

       --with-db[=DIR]              include Berkeyley db 2.x or 3.x support
       --with-db1[=DIR]             include Berkeyley DB 1.x support
       --with-gdbm[=DIR]            include GNU gdbm support
       --with-ndbm[=DIR]            include ndbm support
       --with-sdbm                  include sdbm support. sdbm database
                                    Library comes with the counter.

       --with-ttf[=DIR]             include TrueType font support

       --without-database           don't include database support
       --without-ttf                don't include TrueType font support

       --clean                      remove object and binary files
       --pristine                   clean real good
       --install                    Install the counter

    Note: If you compile with --all (default setting), your system will be 
          probed for databases and TTF fonts. If Berkeley db or gdbm not 
          found, the supplied sdbm database will be used.
EOM
    exit 1
fi


while test $# -gt 0; do
    case "$1" in
        --all)
        all="yes"
        break;
        ;;
        --with-cgi-bin-dir*)
        cgi_bin_dir=`echo $1 | awk -F= '{print $2}'`
        if test ! -d $cgi_bin_dir; then
            echo "
            Warning: CGI directory: $cgi_bin_dir does not exist
            "        
        fi
        config_options="$config_options $1"
        shift
        ;;

        --prefix*)
        config_options="$config_options $1"
        prefix="$1"
        shift;
        ;;

        --with-db*)
        config_options="$config_options $1"
        prefix="$1"
        shift
        ;;

        --with-gdbm*)
        config_options="$config_options $1"
        prefix="$1"
        shift
        ;;

        --with-ndbm*)
        config_options="$config_options $1"
        prefix="$1"
        shift
        ;;

        --with-sdbm)
        config_options="$config_options $1"
        prefix="$1"
        shift
        ;;

        --with-ttf*)
        config_options="$config_options $1"
        prefix="$1"
        shift
        ;;

        --without-database)
        config_options="$config_options $1"
        probe_db="no"
        shift
        ;;

        --without-ttf)
        config_options="$config_options $1"
        without_ttf="$1"
        probe_ttf="no"
        shift
        ;;

        --pristine)
            $rm bin/names.txt
            $rm bin/Count.cgi bin/mkstrip bin/extdgts bin/editdb bin/dumpdb 
            $rm bin/count_admin.cgi bin/rgbtxt2db bin/count_admin_help.cgi
            $rm bin/dumpdb.pl 
            $rm bin/mwhich
            $rm config.cache config.log config.status

            (cd src; make clean)
            $rm ./src/Makefile

            for i in $common_libs; do
                (cd ./libs/$i; make clean)
                $rm ./libs/$i/Makefile
            done
            for i in $db_libs; do
                (cd ./libs/$i; make clean)
                $rm ./libs/$i/Makefile
            done
            for i in $common_utils; do
                (cd ./utils/$i; make clean)
                $rm ./utils/$i/Makefile
            done
            for i in $db_utils; do
                (cd ./utils/$i; make clean)
                $rm ./utils/$i/Makefile
            done

            (cd ./utils/mwhich; make clean)

            exit
        ;;

        --install)
            (sh ./Count-install)
            shift
            exit
        ;;

        --clean)
            $rm bin/names.txt
            (cd src; make clean)

            for i in $common_libs; do
                (cd ./libs/$i; make clean)
            done

            for i in $db_libs; do
                (cd ./libs/$i; make clean)
            done

            for i in $common_utils; do
                (cd ./utils/$i; make clean)
            done

            for i in $db_utils; do
                (cd ./utils/$i; make clean)
            done

            (cd ./utils/mwhich; make clean)
            shift

            exit
        ;;

        -*)
            echo "unknown option $1" >&2
            shift
            exit 1
            ;;

        *)
        break
        ;;
    esac
done



$rm config.cache

# temporariy send stdout and stderr to /dev/null, first save them
    exec 8<&1
    exec 9<&2

    exec 1>/dev/null 2>&1
        (cd src; make clean)

        for i in $common_libs; do
            (cd ./libs/$i; make clean)
        done

        for i in $db_libs; do
            (cd ./libs/$i; make clean)
        done

        for i in $common_utils; do
            (cd ./utils/$i; make clean)
        done

        for i in $db_utils; do
            (cd ./utils/$i; make clean)
        done
# restore stdout
    exec 1<&8
    exec 2<&9

if test x"$all" = x"yes"; then
    config_options='';
fi

#echo "config options=$config_options"

./configure $config_options
status=$?

if test $status -ne 0; then
    exit
fi

p=10
echo "Pausing for $p seconds ..."
sleep 10

##
## make common libraries
for lib in $common_libs; do
    dir="./libs/$lib"
    echo "========== Entering directory: $dir "
    sleep 1
    (cd $dir; make)
done


if test "$probe_db" = "yes"; then
    for lib in $db_libs; do
        dir="./libs/$lib"
        echo "========== Entering directory: $dir "
        sleep 1
        (cd $dir; make)
    done

    echo " ========= making database utilities "
    sleep 1
    for u in $db_utils; do
        dir="./utils/$u"
        echo "Entering directory: $dir"
        (cd $dir; make)
    done

# which in some systems are screwed up. We'll use our own
    (cd ./utils/mwhich; make)
    echo " ========= fixing cgi programs"
    ./fixcgi.sh
fi


##
## make the counter binary
dir="./src"
cat<<EOF
#########################################################################
#        making Counter, entering directory: $dir 
#########################################################################
EOF
sleep 2
(cd $dir; make)

dir="./utils"
echo "
========= making common utilities
"
sleep 1
for u in $common_utils; do
    dir="./utils/$u"
    echo "========= Entering directory: $dir"
    (cd $dir; make)
done

echo "
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Binaries built and copied to ./bin
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$ls $bindir
"
$ls $bindir
