#!/bin/sh

umask 022

echo ""

if [ -d /bin/athena ]; then
    PATH=$PATH:/bin/athena
fi

date=gdate
MAKE=make
config_options=
BUILDOPTS=

echo=bsd
doinstall=true
cname="(configname)"
cripple_random=false
TCL=yes
if test "${SHLIB-yes}" = no ; then L=static ; else L=shared ; fi
if test "${THREAD-no}" = yes ; then T=-thread ; else T= ; fi
cripple_random=true
case `hostname` in
  small-gods.mit.edu)
    # get working runtest, make and printenv
    #PATH=/u1/kr/gnu/bin:/usr/sfw/bin:$PATH:/usr/ccs/bin:/usr/ucb
    PATH=/usr/local/bin:/usr/sfw/bin:$PATH:/usr/ccs/bin:/usr/ucb
    # the "cc" script in /usr/athena/bin needs this
    ATHENA_SYS_COMPAT=sun4x_58:sun4x_57:sun4x_56:sun4x_55:sun4m_54:sun4m_53:sun4m_412
    export ATHENA_SYS_COMPAT
    #
    if test "${DO64-no}" = yes ; then
	# For Solaris 9 we needed -D_XOPEN_SOURCE=500 -D__EXTENSIONS__, but
	# Solaris 10 doesn't like that option combined with the native
	# default of C99, apparently.  Dropping the -D options means we
	# don't get to see the getsockname prototype with a reasonable
	# pointer type (void* instead of socklen_t*), but our defaults
	# still do the right thing.
#	config_options="CC=\"cc -xarch=v9 -D_XOPEN_SOURCE=500 -D__EXTENSIONS__\""
	config_options="CC=\"cc -m64\" CXX=\"CC -m64\""

	PATH=/var/SUNWspro/bin:$PATH
	STUDIO12TOP=/var/SUNWspro ; export STUDIO12TOP
# v12
#	config_options="CC=\"cc -m64\" CXX=\"CC -m64\""
	LD_LIBRARY_PATH=/opt/SUNWspro/lib:$LD_LIBRARY_PATH
	export LD_LIBRARY_PATH

	TCL=/mit/krbdev/arch/sun4x_58/v9
	xdir=/var/krbsnap/autobuild64-$L
	cname=solaris-sparcv9-$L
    else
	config_options="CC=\"gcc -pipe\""
	TCL=/mit/krbdev/arch/sun4x_58
	xdir=/var/krbsnap/autobuild-$L
	cname=solaris-sparc32-gcc-$L
    fi
    if [ ${BASH-no} != no ]; then
#	echo is bash
	echo=bsd
    else
#	echo is not bash
	echo=sys5
    fi
#    doinstall=false
#    LD_PRELOAD=libumem.so
    UMEM_DEBUG=default,verbose
    export LD_PRELOAD UMEM_DEBUG
    ;;
  salamander.mit.edu)
    PATH=/usr/pkg/bin:$PATH
    TCL=/usr/pkg
    xdir=/home/krbsnap/autobuild-$L
    date=date
    ulimit -t 600
    ;;
  capacitor-bank.mit.edu)
    xdir=/var/krbsnap/autobuilder-krb5-$L
#    PATH=/usr/local/bin:$PATH
    config_options="$config_options"
    TCL=/mit/krbdev/arch/sgi_65
    if test "${SHLIB-no}" = no ; then
        cname=mips32-irix6.5.7
    else
	cname=mips32-irix6.5.7-shared
    fi
    ;;
  rsx-11)
    # /usr/vac - compiler tools
    # /usr/local - gcc and friends
    # /home/raeburn/tools - expect, dejagnu
    PATH=$PATH:/usr/vac/bin:/usr/local/bin:/home/raeburn/tools/bin
    TCL=/home/raeburn/tools
    echo=sys5
    if test "${DOGCC-no}" = no ; then
	xdir=/home/krbsnap/autobuilder-krb5
        config_options="$config_options CC=cc"
	cname=powerpc-aix4.3.3-cc
    else
	xdir=/home/krbsnap/autobuilder-krb5-gcc
	config_options="$config_options CC=gcc-3.4"
	cname=powerpc-aix4.3.3-gcc
    fi
    date=date
    ;;
  all-in-one.mit.edu)
    xdir=/var/krbsnap/autobuilder-krb5-$L
    date=date
    cname=i386-linux-$L
    test "$REL" != 1.3 && BUILDOPTS="-j 9"
    TCL=/usr
    ;;
  equal-rites)
    xdir=/home/krbsnap/autobuilder-krb5-$L
    date=date
    cname=x86_64-linux-$L
    # BUILDOPTS="-j 3"
    ;;
  rsts-11.mit.edu)
    xdir=/usr/users/krbsnap/autobuilder-krb5-$L
    PATH=/usr/local/bin:$PATH
    date=date
#    doinstall=false
    cname=alpha-osf5.1A
    TCL=/usr/local
    test "$shell" = /bin/ksh && echo=sys5
    ;;
  science-fiction-double-feature.mit.edu)
    xdir=$HOME/autobuilder-krb5-$SYSTEM
    PATH=/usr/local/bin:$PATH
    # sfdf has two cpus, let's make use of them
#    BUILDOPTS="-j 3"
    date=date
    cname=ia64-linux
    if test "$SYSTEM" = debian ; then
        config_options="$config_options CC=gcc-3.3"
	xdir=${xdir}-$L
    fi
    cripple_random=true
    ;;
  venix.mit.edu)
    xdir=$HOME/autobuild-$L
    date=date
    cname=alpha-netbsd2.0BETA
    TCL=/usr/pkg
    cripple_random=true
    ;;
  edt)
    xdir=/home/krbsnap/autobuild-$L
    date=date
    cname=alpha-linux-$L
    test "$REL" != 1.3 && BUILDOPTS="-j 3"
    cripple_random=true
    ;;
  gamma-11.mit.edu | Ken-Raeburns-Computer.local)
    xdir=/Users/krbsnap/autobuild
    PATH=$PATH:/sw/bin
    date=date
    cname=powerpc-darwin
    test "$REL" != 1.3 && BUILTOPTS="-j 2"
    TCL=/sw
    ;;
  *)
    xdir=/tmp/autobuilder-krb5
    ;;
esac

case $cname in
  *netbsd*)  MALLOC_OPTIONS=AJRV ; export MALLOC_OPTIONS ;;
  *darwin*)
#	MallocScribble=. ; export MallocScribble
#	MallocPreScribble=. ; export MallocPreScribble
	MallocBadFreeAbort=1 ; export MallocBadFreeAbort
	;;
esac

# wget lives in gnu
# runtest lives in gnu (but is broken), krbdev
if [ -x /bin/athena/attach ]; then
    /bin/athena/attach -q -n gnu krbdev
    PATH=/mit/krbdev/arch/@sys/bin:$PATH:/mit/gnu/arch/@sys/bin
fi
export PATH


config_options="$config_options --with-tcl=$TCL"
today=`$date +%Y%m%d.%H%M`
release=${REL-current}
if test $release = current ; then
  workprefix=work
  ftpname=krb5-current.tar.gz
else
  workprefix=work-${REL}
  ftpname=krb5-${REL}-current.tar.gz
fi
tdir=$xdir/${workprefix}-$today

echo ----
echo To grab logs, run:
echo perl /mit/krbdev/testing/grab-logs `hostname` $xdir "$cname"
echo ----
echo ""
echo Branch: $release
echo ""
#echo echo is $echo type
case $echo in
  bsd)
    echo_nnl() {
	echo -n $*
    }
    ;;
  sys5)
    echo_nnl() {
	echo $*\\c
	true 2
    }
    ;;
esac

zreport() {
    (echo autobuild status: $cname krb5-$release ; cat $tdir/status ; echo `hostname` $xdir) | \
	zwrite -q -d -c krb5 -i autobuild
}

diskspace_and_report() {
    if test -d $tdir; then
	cd $tdir
	du -ks . | awk '{print $1}' > tree-size
	df -k . > free-space
    fi
    $ztrap && zreport
}

if test -x /usr/athena/bin/zwrite || test -x /usr/bin/zwrite ; then
  ztrap=true
else
  ztrap=false
fi
trap "diskspace_and_report ; trap '' 0 ; exit 1" 0

case "${SHLIB-no}" in
no)	libopts="--disable-shared --enable-static" ;;
yes)	libopts="--disable-static --enable-shared" ;;
both)	libopts="--enable-static --enable-shared" ;;
esac
config_options="$libopts $config_options"

case "${THREAD-no}" in
yes)	config_options="$config_options --enable-thread-support" ;;
*)	;;
esac

#rm -rf $tdir
if test ! -d $xdir ; then
    mkdir -p $xdir
    if [ $? != 0 ]; then
	echo cannot make autobuilder directory
	exit 1
    fi
fi
cd $xdir
if test ! -d .delete_me ; then
    mkdir .delete_me
fi
touch ${workprefix}-${today}-x
mv ${workprefix}-20[0-9][0-9]* .delete_me
(rm -rf .delete_me >/dev/null 2>&1 &)
# Give the rm a little time to get going and free up some space.
sleep 10
mkdir $tdir
cd $tdir
if [ $? != 0 ]; then
    echo cannot cd to temporary build directory $tdir
    exit 1
fi

save_env() {
    (
      echo Config options $config_options.
      echo Make program is specified as $MAKE.
      echo Script shell is $shell.
      for prog in $MAKE cc gcc as ld ranlib echo ps runtest rlogin telnet ; do
#	echo Looking for $prog ...
	type $prog
      done
      echo Working in $tdir.
      if test "`uname -s`" = Linux ; then
	  echo ""
	  test -r /etc/redhat-release && cat /etc/redhat-release
	  test -r /etc/debian_version && echo Debian version `cat /etc/debian_version`
      fi
      echo ""
      echo GCC version info:
      gcc -v
      echo ""
      echo Environment variables:
      echo ""
      printenv
      echo ""
      echo Attached filesystems:
      echo ""
      attach || mount
      echo ""
      echo Kerberos tickets:
      echo ""
      klist
      echo ""
      echo AFS tokens:
      echo ""
      tokens
      echo ""
      echo "Process limits:"
      echo ""
      ulimit -a
      echo ""
      echo "Disk space:"
      echo ""
      df -k
      df -k .
      echo ""
    ) > $tdir/env-info 2>&1
}

download() {
    echo downloading > $tdir/status
    wget --passive-ftp -nv -t 10 ftp://ftp:autobuilder@aeneas.mit.edu/pub/kerberos/dist/vaporware-r-us/$ftpname
    if [ $? != 0 ]; then
	echo ""
	echo error fetching snapshot
	echo download error > $tdir/status
	exit 1
    fi
}

unpack() {
    echo unpacking > $tdir/status
    gunzip < $ftpname > krb5.tar && tar xf krb5.tar && rm krb5.tar
    if [ $? != 0 ]; then
	echo ""
	echo error unpacking snapshot
	echo unpacking error > $tdir/status
	exit 1
    fi
    grep krb5_brand krb5-*/src/lib/krb5/krb/brand.c | sed -e 's/.*KRB5_BRAND: *//' -e 's/";$//' > $tdir/version
    echo Version: `cat $tdir/version`
}

maybe_cripple_random() {
    if $cripple_random; then
	echo Crippling /dev/random reader for testing...
	if test -r lib/crypto/prng.c ; then
	    prng=lib/crypto/prng.c
	elif test -r lib/crypto/krb/prng.c ; then
	    prng=lib/crypto/krb/prng.c
	else
	    # ??
	    prng=lib/crypto/krb/prng.c
	fi
	sed -e 's,"/dev/random","/dev/urandom",g' \
	    < $prng > $prng.new
#	diff -u lib/crypto/prng.c lib/crypto/prng.c.new
	mv -f $prng.new $prng
    fi
}

maybe_reconf() {
    if [ ! -r configure ]; then
	echo running autoconf > $tdir/status
	echo_nnl autoconf... ''
	if [ $? != 0 ]; then
	    echo ""
	    echo cannot chdir into unpacked snapshot
	    echo cannot chdir into unpacked snapshot > $tdir/status
	    exit 1
	fi
	# autoconf from path?
	util/reconf		> $tdir/reconf-log	2>&1
	if [ $? != 0 ]; then
	    echo ""
	    echo error running autoconf
	    echo autoconf failed > $tdir/status
	    exit 1
	fi
    fi
}

do_configure() {
    echo configuring > $tdir/status
    eval "./configure -C --prefix=$tdir/Install $config_options" \
				> $tdir/config-log	2>&1
    if [ $? != 0 ]; then
	echo ""
	echo error running configure
	echo configure failed > $tdir/status
	echo output in $tdir/config-log
	echo _______________
	tail $tdir/config-log
	exit 1
    fi
}

do_build() {
    echo building > $tdir/status
    $MAKE $BUILDOPTS		> $tdir/build-log	2>&1
    if [ $? != 0 ]; then
	echo ""
	echo error building krb5 snapshot
	echo build failed > $tdir/status
	echo output in $tdir/build-log
	echo _______________
	tail -30 $tdir/build-log
	exit 1
    fi
}

do_install() {
    echo installing > $tdir/status
    $MAKE install \
	INSTALL_SETUID='$(INSTALL_PROGRAM)' \
	INSTALL_STRIP= \
				> $tdir/install-log	2>&1
    if [ $? != 0 ]; then
	echo ""
	echo error installing krb5 snapshot
	echo install failed > $tdir/status
	echo output in $tdir/install-log
	echo _______________
	tail $tdir/install-log
	exit 1
    fi
}

do_check() {
    echo checking > $tdir/status
    # turn on verbosity in OV tests
    VERBOSE_TEST=yes ; export VERBOSE_TEST
    $MAKE -k check RUNTESTFLAGS="PORTBASE=23100" > $tdir/check-log	2>&1
    if [ $? != 0 ]; then
	echo ""
	echo error running krb5 tests
	echo output in $tdir/check-log
	echo _______________
	tail -30 $tdir/check-log
	echo check failed > $tdir/status
	echo ""
	echo "Rechecking attached filesystems:"
	echo ""
	attach || mount
	exit 1
    fi
}

go() {
    echo Starting build on `hostname` at `date`.
    echo Config options $config_options.
#    for prog in cc gcc as ld ranlib echo rlogin telnet $MAKE ; do
#	echo Looking for $prog ...
#	type $prog
#    done
    echo Make program is specified as $MAKE.
    echo Working in $tdir.
    echo starting > $tdir/status
    #echo Saving environment info...
    save_env
    echo_nnl Download... ''
    download
    echo_nnl unpack... ''
    unpack
    cd krb5-*/src
    maybe_cripple_random
    maybe_reconf
    echo_nnl configure... ''
    # prime cache
    #ac_cv_prog_RUNTEST=
    #export ac_cv_prog_RUNTEST
    # configure and build
    do_configure
    # native make?
    echo_nnl build... ''
    do_build
    warnings=0
    if egrep -i 'warning:' $tdir/build-log >/dev/null 2>&1 ; then
#	echo "" ; echo Some warnings produced, see build-log.
	echo_nnl "(warnings!)... "
	warnings=1
    fi
#    echo doinstall=${doinstall}
    if eval ${doinstall} ; then
	echo_nnl install... ''
	do_install
    fi
    echo_nnl check... ''
    do_check
    echo ""
    echo All phases appear successful.

    # Let's check for some warnings we want to address.
    # gcc warning about missing prototype issues:
    if test "$REL" = "current"; then
      egrep "warning: no previous prototype for|warning: implicit declaration of function" $tdir/build-log
    fi

    echo success > $tdir/status
    trap "" 0
    diskspace_and_report
    exit 0
}

# Catch any error output not elsewhere redirected.
exec 2>&1
# Run all the interesting stuff via functions, so the shell will have the
# end of file or an exit command already loaded, just in case we edit the
# script while it's being run on some machine.
go ; exit 1
