#!/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 ] [ -q ] [ -h] [-- makeopts]
#      -b: build only
#      -c: configure only (full re/configure)
#      -C: configure with developer options turned on
#      -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
	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'
#pat='fh_dref'

##############################################################################
# initialize variables (build command, config command, variables)
bld_cmd=""
bld_flags=""
cnf_cmd=""
cnf_flags=""
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"
	vars="$vars CFLAGS=\"-O\""
	expvars="$expvars CFLAGS"
	vars="$vars AM_CFLAGS=\"-Wall -Werror\""
#	vars="$vars AM_CFLAGS=\"-Werror\""
	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 ] [ -q ] [ -h] [-- makeopts]
     -b: build only
     -c: configure only (full re/configure)
     -C: configure with developer options turned on
     -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 tmp.$$ config.cache
    fi
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
    echo $cnf_cmd $cnf_flags
    $cnf_cmd $cnf_flags || exit 1
fi
##############################################################################
# if need to [re]build
if test -n "$bld_cmd"; then
    echo $bld_cmd $bld_flags
    $bld_cmd $bld_flags || exit 1
fi

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