#!/bin/sh

# local copies are currently used on:
# edt raeburn.org opteron-prime
# other test systems run it directly out of AFS

PATH=$PATH:/usr/athena/bin
reportaddr=krbdev-auto@mit.edu

if test "`uname -n`" != opteron-prime && test -d /afs/athena.mit.edu/astaff/project/krbdev/testing ; then
  PATH=/afs/athena.mit.edu/astaff/project/krbdev/testing:$PATH
else
  d=`dirname $0`
  if test "$d" != ""; then
    PATH=$d:$PATH
  fi
fi

export PATH

RELEASES="current/1.15"

shell=none
for x in /bin/athena/bash /bin/bash /usr/local/bin/bash /bin/ksh /bin/sh ; do
  if test $shell = none && test -r $x ; then
    shell=$x
  fi
done

export shell

PARAMS=""
MAILSUBJ=echo
DESCR=""

# N.B.: Check disk space before changing REL= assignments!
case "`hostname`" in
small-gods.mit.edu)
  PARAMS="DO64=no/yes SHLIB=yes/no REL=$RELEASES"
#  PARAMS="DO64=no SHLIB=yes/no REL=$RELEASES"
  MAILSUBJ=echo
  ;;
salamander.mit.edu)
  PARAMS="SHLIB=yes/no REL=$RELEASES"
  MAILSUBJ=arg
  ;;
rsx-11)
# not afs?
  PARAMS="DOGCC=yes/no REL=$RELEASES SHLIB=yes"
  MAILSUBJ=arg
  ;;
science-fiction-double-feature.mit.edu)
  if test -r /etc/redhat-release ; then
    SYSTEM=redhat
  else
    LANG=C ; export LANG
    SYSTEM=debian
    PARAMS="SHLIB=yes/no REL=${RELEASES}"
  fi
  export SYSTEM
  MAILSUBJ=arg
  DESCR="$SYSTEM"
  ;;
all-in-one.mit.edu)
  PARAMS="SHLIB=yes/no REL=$RELEASES THREAD=yes/no"
  MAILSUBJ=arg
  ;;
capacitor-bank.mit.edu)
#  PARAMS="SHLIB=yes/no REL=1.3"
  PARAMS="SHLIB=yes REL=${RELEASES}"
  PATH=/usr/gcc/bin:$PATH
  ;;
rsts-11.mit.edu)
# not afs?
#  PARAMS="SHLIB=yes/no REL=1.3"
  PARAMS="SHLIB=yes REL=${RELEASES}"
  MAILSUBJ=echo
  PATH=$PATH:/usr/local/bin
  ;;
gamma-11.mit.edu)
  PARAMS="SHLIB=yes REL=${RELEASES}"
  PATH=$PATH:/usr/local/bin
  ;;
equal-rites)
  MAILSUBJ=arg
  PARAMS="SHLIB=yes/no REL=${RELEASES}"
  ;;
*)
  ;;
esac

# Use this to change the ordering of the different configurations from
# one run to another.  Apply it to the parameters to be iterated over,
# and for each one, apply it to the set of possible values.  Kind of
# silly, but if runs take too long or hang (and both *have* happened),
# we'll get results from different configurations on different days
# instead of (possibly) never testing some.

if type md5 >/dev/null 2>&1; then
    have_md5=true
else
    have_md5=false
fi

if type md5sum >/dev/null 2>&1; then
    have_md5sum=true
else
    have_md5sum=false
fi

hashof() {
    if $have_md5; then
	echo $* | md5
    elif $have_md5sum; then
	echo $* | md5sum
    else
	echo ${RANDOM-12345}
    fi
}

# Randomize the order of option combinations, so if we run out of time
# or hang during a build, we don't always fail to complete the same
# combinations.
randomize() {
    #
    if test "$2" = "" ; then
	echo $1
	return
    fi
    (
	if test "`echo $RANDOM`" = ""; then
	    d=`date`
	    for x in $*; do echo `hashof $d $x`:$x ; done
	else
	    for x in $*; do echo ${RANDOM}:$x ; done
	fi
    ) | sort | sed 's/^[^:]*://'
}

iterate() {
    # args: DESCR PARAMS
#    echo ... iterate . $1 . $2 . $3 . $4 . $5 ...
    descr=$1
    if test "$2" != ""; then
	x=$2
	# For trunk and 1.5, currently only shared builds work.
	if [ "$x" = "SHLIB=yes/no" ]; then x="SHLIB=yes"; fi
	shift
	shift
	varname=`echo $x | sed s/=.*//`
	options=`echo $x | sed -e s/.*=// -e 's,/, ,g'`
#echo initial options: $options
	options=`randomize $options`
#echo new options: $options
	for o in $options ; do
	    (eval $varname=$o ; export $varname ; iterate "$descr $varname=$o" ${1+"$@"})
	done
    else
	doit "$descr"
    fi
}

doit() {
    descr=`(for x in $1; do echo $x; done) | sort`
    descr=`echo $descr`
    runbuild "$descr" | send_mail "$descr"
#    echo run build "'$descr'" ; echo "	env: THREAD=$THREAD SHLIB=$SHLIB DO64=$DO64 REL=$REL DOGCC=$DOGCC"
}

runbuild() {
    if test $MAILSUBJ = echo ; then
	echo Subject: krb5 snapshot build `hostname` $1
	echo ""
    fi
    echo Nightly build results...
    run-build
}

send_mail() {
    if test $MAILSUBJ = arg ; then
	mail -s "krb5 snapshot build `hostname` $1" $reportaddr
    else
	mail $reportaddr
    fi
}

if test $MAILSUBJ = echo ; then
    echo Subject: nightly krb5 build cron job on `hostname`
    echo ""
fi
PARAMS=`randomize $PARAMS`
#echo PARAMS=$PARAMS
iterate "$DESCR" $PARAMS ; exit 0
