#!/bin/sh
# Build all of the am-utils package in a directory A.<cpu-company-system>
# Used by am-utils users.
# Erez Zadok <ezk@cs.columbia.edu>
#
# Usage: buildall [-b] [-c] [-C] [-D] [-K] [-q] [-h] [-- makeopts]
#      -b: build only
#      -c: configure only (full re/configure)
#      -C: configure with developer options turned on
#      -D: configure with very strict developer options turned on
#      -K: run mkconf to update *.in files (developers only)
#      -q: quick configure only (run config.guess)
#      -h: print usage
#      makeopts: options to pass to make (must be last and after a --)
# You may pass variables: CFLAGS for build, MAKE for your make program
# and AM_CFLAGS for additional build flags.
#set -x

##############################################################################
# first test if we are in the right directory to run this script
if test -f ./aux/config.guess ; then
    host_alias=`./aux/config.guess`
else
    cd .. # maybe we are inside the A.* directory
fi
if test -f ./aux/config.guess ; then
    host_alias=`./aux/config.guess`
else
    cd .. # maybe we are inside a subdirectory of the the A.* directory
fi
if test -f ./aux/config.guess ; then
    host_alias=`./aux/config.guess`
else
	echo "$0: must run from the source or the A. directory."
	echo "$0: cannot find ./aux/config.guess"
	exit 1
fi

##############################################################################
# pattern of lines to remove from config.cache (for developers only)
# Example: if you change TRY_COMPILE_NFS, redo these:
#pat='fhandle|nfs_fh|nfs_args|struct_nfs|opt'
#pat='style_|mount_trap|mtype|transport|dref'
#pat='mntent|mnttab'
#pat='nfs_args|fh_len|irix|proto|vers'
#pat='3|proto|tcp|ver|nfs_prot|mtype'
#pat='ldap|lber'
#pat='bison|yacc'

##############################################################################
# initialize variables (build command, config command, variables)
bld_cmd=""
bld_flags=""
cnf_cmd=""
cnf_flags=""
mkcnf_cmd=""
vars=""
expvars=""
default=yes

##############################################################################
# check if CFLAGS or AM_CFLAGS was passed
test -z "$CFLAGS" || vars="$vars CFLAGS=\"${CFLAGS}\""
test -z "$CFLAGS" || expvars="$expvars CFLAGS"
test -z "$AM_CFLAGS" || vars="$vars AM_CFLAGS=\"${AM_CFLAGS}\""

##############################################################################
# iterate over all options, and set the command to run with any variables
while [ $# != 0 ]; do
case "$1" in
    -b )
	bld_cmd="${MAKE:-make}"
	default=no
	shift
    ;;

    -c )
	cnf_cmd="../configure --srcdir=.."
	default=no
	shift
    ;;

    -C )
#	cnf_cmd="../configure --srcdir=.. --enable-debug=yes --enable-cppflags=-I/home/ezk/hesiod/include --enable-libs=-L/home/ezk/hesiod/lib"
	cnf_cmd="../configure --srcdir=.. --prefix=/usr/local/AMD --enable-debug=yes"
	vars="$vars AM_CFLAGS=\"-Werror\""
	AM_CFLAGS="-Werror"
	expvars="$expvars AM_CFLAGS"
	default=no
	shift
    ;;

    -C2 )
	cnf_cmd="../configure --srcdir=.. --enable-debug=no"
	vars="$vars AM_CFLAGS=\"-Werror\""
	AM_CFLAGS="-Werror"
	expvars="$expvars AM_CFLAGS"
	default=no
	shift
    ;;

    -D )
	cnf_cmd="../configure --srcdir=.. --enable-debug=yes --enable-cppflags=-I/home/ezk/hesiod/include --enable-libs=-L/home/ezk/hesiod/lib"
	cnf_cmd="../configure --srcdir=.. --prefix=/usr/local/AMD --enable-debug=yes --enable-shared --enable-cppflags=-I/u/zing/ezk/ldap/include --enable-libs=-L/u/zing/ezk/ldap/lib"
	cnf_cmd="../configure --srcdir=.. --prefix=/usr/local/AMD --enable-debug=yes"
	cnf_cmd="../configure --srcdir=.. --prefix=/usr/local/AMD --enable-debug=yes --enable-shared"

	vars="$vars AM_CFLAGS=\"-Wall -Werror\""
	AM_CFLAGS="-Werror -Wall"
	expvars="$expvars AM_CFLAGS"
	default=no
	shift
    ;;

    -K )
	mkcnf_cmd="../aux/mkconf"
	default=no
	shift
    ;;

    -q )
	cnf_cmd="./config.status"
	default=no
	shift
    ;;

    -- )
	shift
	bld_flags="$*"
	break	# from while loop
    ;;

    -h | * )
cat <<EOF
Usage: buildall [-b] [-c] [-C] [-D] [-K] [-q] [-h] [-- makeopts]
     -b: build only
     -c: configure only (full re/configure)
     -C: configure with developer options turned on
     -D: configure with very strict developer options turned on
     -K: run mkconf to update *.in files (developers only)
     -q: quick configure only (run config.guess)
     -h: print usage
     makeopts: options to pass to make (must be last and after a --)
You may pass variables: CFLAGS for build, MAKE for your make program
and AM_CFLAGS for additional build flags.
EOF
    exit 1
    ;;

esac
done

# if AM_CFLAGS was set before, then add it to the configure option
if test -n "${AM_CFLAGS}"; then
    cnf_flags="--enable-am-cflags=${AM_CFLAGS}"
fi

# check if no options were given, and set to defaults
if test "$default" = "yes"; then
    bld_cmd="${MAKE:-make}"
    cnf_cmd="../configure --srcdir=.."
fi

##############################################################################
# make build directory if needed
if test -d ./A.${host_alias} ; then
	:
else
	mkdir ./A.${host_alias}
fi
echo "Configuring/building am-utils in directory ./A.${host_alias} ..." 
echo cd ./A.${host_alias}
cd ./A.${host_alias} || exit 1

##############################################################################
# this is for developers only (remove config.cache entries)
if test -n "$pat"; then
    if test -f config.cache; then
	egrep $pat config.cache | while read i; do echo '	'$i;done
	egrep -v $pat config.cache > tmp.$$ && \
		mv config.cache config.cache.old && mv tmp.$$ config.cache
    fi
fi

##############################################################################
# Some system's /bin/sh has limits/bugs which prevent it from being used
# with configure
case "${host_alias}" in
    hppa1.1-hp-hpux9.01 )
	if test -f /bin/bash; then
	    cnf_cmd="/bin/bash $cnf_cmd"
	elif test -f /bin/ksh; then
	    cnf_cmd="/bin/ksh $cnf_cmd"
	fi	    
	;;
esac

##############################################################################
# see if need to run mkconf
if test -n "$mkcnf_cmd"; then
    echo $mkcnf_cmd
    $mkcnf_cmd || exit 1
fi

##############################################################################
# see if need to [re]configure
if test -n "$cnf_cmd"; then
    if test -n "$vars"; then
	echo $vars
	eval $vars
	echo export $expvars
	export $expvars
    fi
    if test -z "$cnf_flags"; then
	echo $cnf_cmd
	$cnf_cmd || exit 1
    else
	echo $cnf_cmd "$cnf_flags"
	$cnf_cmd "$cnf_flags" || exit 1
    fi
fi

##############################################################################
# if need to [re]build
if test -n "$bld_cmd"; then
    echo $bld_cmd $bld_flags
    $bld_cmd $bld_flags || exit 1
fi

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