#!/bin/sh

#This script is run by 'cron' nightly to pontentially build on495-gate
#However, it should only do so if the environment changes (such as when
#a putback occurs). If somebody does a putback, the mail notification
#creates /var/tmp/gate_nightly_rebuild. This script only rebuilds
#if this file exists.

USAGE='usage: run_nightly [-r] [-f] [-m] [-e] [-i] [-n] [-p]\n
\n
	-r	Do a release build\n
	-f	Do the build even if nothing has changed\n
	-m	Do a build in the merge workspace\n
	-e	Do a build in the sun4-eol workspace\n
	-p	Do a build of the PowerPC workspace\n
	-i	Do an incremental build (no "make clobber")\n
	-n	Do not do a bringover\n
'
REBUILD=/var/tmp/gate_nightly_rebuild
ENVFILE=/builds/env.on495-gate
FORCE="n"
NFLAGS="-am"
CLOBBER="-c"
BRINGOVER="-b"
RELEASE=""
aborted=0

clean() {
	aborted=1
}

if [ $# = 0 ]; then
	echo $USAGE
	exit 1
fi


while getopts erfimnp FLAG
do
	case $FLAG in
	r)	RELEASE=".release"
		;;
	f)	touch ${REBUILD}
		chmod 666 ${REBUILD}
		;;
	e)	ENVFILE=/builds/env.on495-sun4-eol
		NFLAGS="-amp"
		;;
	p)	ENVFILE=/builds/env.on-ppc
		NFLAGS="-ampuU"
		;;
	m)	ENVFILE=/builds/env.on-ppc-merge
		#NFLAGS="-amp"
		;;
	i)	CLOBBER=""
		;;
	n)	BRINGOVER=""
		;;
	\?)	echo $USAGE
		exit 1
		;;
	esac
done
	
#automount map for generic tools: teamware/onbld/ongk
TOOLPATH=/shared/ON/tools


if [ -d /opt/onbld/bin ]; then
        BLD="/opt/onbld/bin"
else
        BLD="${TOOLPATH}/onbld/bin"
fi


if [ -f ${REBUILD} ]; then
	trap "clean" 2 15
	${BLD}/nightly ${NFLAGS} ${CLOBBER} ${BRINGOVER} \
		${ENVFILE}${RELEASE} > /dev/null
	trap "" 2 15
	if [ ${aborted} = 0 ]; then
		rm -f ${REBUILD}
	fi
fi

