#!/bin/sh
reconfig=true
clean=false
check=true
wipe=false
host=unset
pack=false
xtra=
nosrc=unset
src=src
top=krb5
make=unset
config_opts="--enable-xdm --enable-shared --with-tk"
while test $# != 0; do
    case "$1" in
	--config)
	    reconfig=true ;;
	noconfig | --noconfig | --no-config)
	    reconfig=false ;;
	--wipe)
	    wipe=true ;;
	--no-wipe)
	    wipe=false ;;
	--clean)
	    clean=true ;;
	--no-clean)
	    clean=false ;;
	--check)
	    check=true ;;
	--no-check)
	    check=false ;;
	--pack)
	    pack=true ;;
	--no-pack)
	    pack=false ;;
	--no-src)
	    nosrc=true ;;
	--top)
	    top="$2"
	    shift ;;
	--top=*)
	    top=`echo x$1 | sed s/^x--top=//` ;;
	--make)
	    make="$2"
	    shift ;;
	--make=*)
	    make=`echo x$1 | sed s/^x--make=//` ;;
	-*)
	    xtra="$xtra '$1'" ;;
	*)
	    if test "$host" = unset ; then
		host=$1
	    else
		echo "Host specified multiple times?  $host , $1" 1>&2
		exit 1
	    fi ;;
    esac
    shift
done

if test "$nosrc" = true ; then
    src=$top
fi
case "$top" in
    /*)
	topdotdot=""
	;;
    *)
	topdotdot=../
	;;
esac
case "$src" in
    /*)
	srcdotdot=""
	;;
    *)
	srcdotdot=../
	;;
esac
if [ ! -d $top ]; then
    echo "run this script above the krb5 directory"
    echo "Check out the sources (for example, run"
    echo "      cvs -d cvs-ma.cygnus.com:/local/cvsfiles co krb5"
    echo "or whatever you use to check out the sources)"
    exit 1
fi
if test "$host" = unset ; then
    host=`$top/config/config.guess`
    echo Using \"$host\" for build directory.
fi
if test "$make" = unset ; then
    case "$host" in
	*-*-netbsd*) make=gmake ;;
	*) make=make ;;
    esac
fi
if test -r $top/util/release-info ; then
    . $top/util/release-info
    dfl_prefix=/usr/cygnus/${RELEASE_TAG}
    case "$xtra" in
	*-prefix*) ;;
	*)	echo Using $dfl_prefix as default installation prefix. ;;
    esac
else
    echo "Can't find $top/util/release-info."	1>&2
    exit 1
fi
if [ ! -d $src ]; then
	echo Creating \"$src\" symlink tree...
	mkdir $src || exit 1
	(cd $src && $topdotdot$top/util/lndir $topdotdot$top && rm Makefile) >lndir.log 2>&1
	if test $? != 0 ; then
		echo lndir failed! 1>&2
		exit 1
	fi
	rm lndir.log
	if [ "$src" != "$top" ] ; then
	    rm -f $src/util/autoconf/configure
	fi
	echo Building autoconf...
	(cd $src/util/autoconf && $topdotdot/../../$top/util/autoconf/configure && $make MAKEINFO=:) || {
		echo autoconf/configure/make failed! 1>&2
		exit 1
	}
	
fi

support_dir=none
for d in progressive devo ; do
    if test -d $d ; then
	support_dir=$d
	break
    fi
done
if test ! -d src/support/. ; then
    if test $support_dir != none ; then
	rm -f $src/support
	ln -s $topdotdot$top/../$support_dir $src/support
    fi
fi

if [ ! -x $src/configure ]; then
	echo Rebuilding configure scripts...
	(cd $src && util/reconf) || {
		echo reconf failed! 1>&2
		exit 1
	}
fi

test -d $host || mkdir $host || exit 1

set -e
cd $host
if $wipe ; then
    test -d .delete_me || mkdir .delete_me || exit 1
    # make sure "*" matches at least one thing
    echo x >deleteMe.$$
    mv -f * .delete_me || exit 1
    rm -rf .delete_me &
else true ; fi

(
time= #time
set -e
date
echo ""
echo "environment variables:"
echo ""
printenv
echo ""
echo ""
if $reconfig || [ ! -r Makefile ]; then
	reconfig=true
fi
if $reconfig; then 
	echo "configuring..."
	date
	echo ""
	echo       $srcdotdot$src/configure $config_opts --prefix=/usr/cygnus/${RELEASE_TAG} $xtra
	eval $time $srcdotdot$src/configure $config_opts --prefix=/usr/cygnus/${RELEASE_TAG} $xtra
else
	true
fi
echo ""
if $clean ; then
    echo "$make clean"
    date
    echo ""
    $time $make clean
    echo ""
else true ; fi
echo "building..."
echo ""
date
echo "$make all"
$time $make all
echo ""
if $check ; then
    echo "checking..."
    date
    echo ""
    echo "$make check"
    $time $make check
    echo ""
fi
if $pack ; then
    echo "packing tar file..."
    date
    echo ""
    $time $make tgz-bin
else true; fi
date
) 2>&1 | tee all-log-`date +%y%m%d%H%M`
