#! /bin/sh
#
#ident	"@(#)nightly.sh	1.57	94/02/01 SMI"
#
# Copyright (c) 1994 by Sun Microsystems, Inc.
#
#	Build script for OS-Net source in a codemgr workspace.
#
USAGE='usage: nightly [-G|-I|-R] [-a[d]] [-bcmptuU] <env_file>

	-G	gate keeper default group of options (-abu)
	-I	integration engineer default group of options (-abimpu)
	-R	default group of options for building a release (-cimp)

	-a	create cpio archives, [-d] optionally use daily directories
	-b	bringover to the workspace to update or to create it
	-c	do "make clobber" before the build, also remove $ROOT
	-m	send mail to $STAFFER at end of build
	-p	create packages
	-t	use tar to back up the build to $TAPE
	-u	update proto_list_${MACH} in the parent workspace
	-U	update proto area in the parent

	<env_file>  file in Bourne shell syntax that sets and exports
	variables that configure the operation of this script and many of
	the scripts this one calls.  See template: /opt/onbld/etc/env.
'
#
#	-x	less public handling of xmod source for the source product
#
#	A log file will be generated under the name $LOGFILE
#	for partially completed build and log.`date '+%m%d%y'`
#	in the same directory for fully completed builds.
#

# default values for low-level FLAGS; G I R are group FLAGS
a_FLAG=n
b_FLAG=n
c_FLAG=n
m_FLAG=n
p_FLAG=n
s_FLAG=n
t_FLAG=n
u_FLAG=n
U_FLAG=n
#
CPIO_OPT=
XMOD_OPT=
#
build_ok=n
#
# examine arguments
#
MACH=`uname -p`

while getopts GIRabcdmptuUx FLAG
do
	case $FLAG in
	  G )	a_FLAG=y
		b_FLAG=y
		u_FLAG=y;;
	  I )	a_FLAG=y
		b_FLAG=y
		m_FLAG=y
		p_FLAG=y
		u_FLAG=y;;
	  R )	c_FLAG=y
		m_FLAG=y
		p_FLAG=y;;
	  a )	a_FLAG=y;;
	  b )	b_FLAG=y;;
	  c )	c_FLAG=y;;
	  d )	CPIO_OPT="-d";;
	  m )	m_FLAG=y;;
	  p )	p_FLAG=y;;
	  t )	t_FLAG=y;;
	  u )	u_FLAG=y;;
	  U )	U_FLAG=y;;
	  x )	XMOD_OPT="-x";;
	 \? )	echo "$USAGE"
		exit 1 ;;
	esac
done

# correct argument count after options
shift `expr $OPTIND - 1`

# test that the path to the environment-setting file was given
if [ $# -ne 1 ]
then
	echo "$USAGE"
	exit 1
fi
#
# 	Setup environmental variables
#
. $1

TMPDIR="/tmp"
CH=

export  CH TMPDIR
unset   CFLAGS LD_LIBRARY_PATH

[ -d $ROOT ] || mkdir -p $ROOT

# since this script assumes the build is from full source, it nullifies
# variables likely to have been set by a "ws" script; nullification
# confines the search space for headers and libraries to the proto area
# built from this immediate source.
ENVLDLIBS1=
ENVLDLIBS2=
ENVLDLIBS3=
ENVCPPFLAGS1=
ENVCPPFLAGS2=
ENVCPPFLAGS3=
ENVCPPFLAGS4=

export ENVLDLIBS3 ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 ENVCPPFLAGS4

ENVLDLIBS1="-L$ROOT/usr/lib -L$ROOT/usr/ccs/lib"
ENVCPPFLAGS1="-I$ROOT/usr/include"

export ENVLDLIBS1 ENVLDLIBS2

#PATH="/opt/onbld/bin:/opt/onbld/bin/${MACH}:/opt/SUNWspro/bin:/opt/teamware/ParallelMake/bin:/usr/ccs/bin:/usr/bin:/usr/sbin:/usr/ucb:/usr/etc:/usr/local/bin:/usr/openwin/bin:."
#export PATH

#automount map for generic tools: teamware/onbld/ongk
TOOLPATH=/shared/ON/tools

TAILPATH="/usr/ccs/bin:/usr/bin:/usr/sbin:/usr/ucb:/usr/etc:/usr/local/bin:/usr/openwin/bin:."
 
#Locally mounted compiler always has precedence 
#Impt for Sustaining work to freeze construction set by mounting locally
#
if [ -d /opt/SUNWspro/bin ]; then
        SP=/opt/SUNWspro/bin
else
        if [ "$SPRO_BIN" ]; then
                SP=${SPRO_BIN}
        else
                echo "Error: SPRO_BIN undefined in env" && exit 1
        fi
fi
[ ! -d $SP ] && echo "${SP} not found!" && exit 1

if [ -d /opt/onbld/bin ]; then
        BLD="/opt/onbld/bin:/opt/onbld/bin/${MACH}"
else
        BLD="${TOOLPATH}/onbld/bin:${TOOLPATH}/onbld/bin/${MACH}"
fi
if [ -d /opt/teamware/ParallelMake ]; then
        MK="/opt/teamware/ParallelMake/bin"
else
        MK="${TOOLPATH}/teamware/ParallelMake/bin"
fi
PATH="${BLD}:${SP}:${MK}:$TAILPATH"
export PATH


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


#
# 	Ensure no other instance of this script is running
#	LOCKNAME should be set in <env_file>
#
if [ -f /tmp/$LOCKNAME ]
then
  echo "conflicting run of nightly script attempted at `date`">> $LOGFILE.extra
  exit 0
else
  touch /tmp/$LOCKNAME
fi
#
# Create mail_msg_file
#
mail_msg_file="/tmp/mail_msg.$$"
touch $mail_msg_file
#
# 	Remove this lock on any exit
#
trap "rm -f /tmp/$LOCKNAME $mail_msg_file; exit 0" 0 1 2 3 15
#
# 	Move old LOGFILE aside; make ATLOG directory if missing.
#
if [ -f $LOGFILE ]
then
	mv -f $LOGFILE ${LOGFILE}-
else
	[ -d $ATLOG ] || mkdir -p $ATLOG
fi
#
#	Build OsNet source
#
echo "==== Nightly build script starts at `date` ====" > $LOGFILE

echo "\n==== list of environment variables ====\n" >> $LOGFILE
env >> $LOGFILE

#
# 	Decide whether to clobber
#
if [ "$c_FLAG" = "y" -a -d "$SRC" ]
then
	echo "\n==== Make clobber at `date` ====\n" >> $LOGFILE

	rm -f $SRC/clobber.out
	cd $SRC
	make -ek clobber 2>&1 | tee -a $SRC/clobber.out >> $LOGFILE
	rm -rf $ROOT
	find $SRC -name '.make.*' -exec rm -f {} \; >/dev/null 2>&1
else
	echo "\n==== No clobber at `date` ====\n" >> $LOGFILE
fi

#
# 	Decide whether to bringover to the codemgr workspace 
#
if [ "$b_FLAG" = "y" ]
then
	echo "\n==== bringover to $CODEMGR_WS at `date` ====\n" >> $LOGFILE
	PATH="$TW:$PATH"; export PATH
	# sleep on the parent workspace's lock
	while egrep -s write $PARENT_WS/Codemgr_wsdata/locks
	do
		sleep 120
	done

	echo "\n==== BRINGOVER LOG ====\n" >> $mail_msg_file
	# do update as a member of the staff rather than root
	# STAFFER should be set in <env_file>
	# later in this script, mail is optionally sent to STAFFER
	( su $STAFFER -c '$TW/bringover \
	   -c "nightly update" -p '$PARENT_WS' \
	   -w '$CODEMGR_WS' usr/src' ) 2>&1 | tee -a  $mail_msg_file >> $LOGFILE
	if [ $? -eq 1 ]
	then
        	echo "trouble with bringover, quitting at `date`." >> $LOGFILE
		exit 1
	fi
else
	echo "\n==== No bringover to $CODEMGR_WS ====\n" >> $LOGFILE
fi

#
#	Build OS-Networking source
#
echo "\n==== Building OS-Net source at `date` ====\n" >> $LOGFILE

	rm -f $SRC/install.out
	cd $SRC
	/bin/time make -e install 2>&1 | tee -a $SRC/install.out >> $LOGFILE
	grep -s make: $SRC/install.out >/dev/null 2>&1
	if [ "$?" = "0" ]; then
		build_ok=n
	else
		build_ok=y
	fi

echo "\n==== Ended OS-Net source build at `date` ====\n" >> $LOGFILE

if [ "$c_FLAG" = "y" ]; then
	rm -f $SRC/noise.ref
	if [ -f $SRC/noise.out ]; then
		mv $SRC/noise.out $SRC/noise.ref
	fi
        grep : $SRC/install.out | egrep -v '^(Start|Finish|./bld_awk)' | \
		sort >$SRC/noise.out
	if [ ! -f $SRC/noise.ref ]; then
		cp $SRC/noise.out $SRC/noise.ref
	fi
	echo "\n===== Build noise differences =====\n" >>$mail_msg_file
	diff $SRC/noise.ref $SRC/noise.out >>$mail_msg_file
fi

#
#	Create cpio archives for preintegration testing (PIT)
#
if [ "$a_FLAG" = "y" -a "$build_ok" = "y" ]
then
	echo "\n==== Creating cpio archives at `date` ====\n" >> $LOGFILE
	mkcpio $CPIO_OPT $1 >> $LOGFILE 2>&1
else
	echo "\n==== Not creating cpio archives ====\n" >> $LOGFILE
fi

#
#	Building Packages
#
if [ "$p_FLAG" = "y" -a "$build_ok" = "y" ]
then
	echo "\n==== Creating packages at `date` ====\n" >> $LOGFILE
	rm -f $SRC/pkgdefs/install.out
	if [ -d $PKGARCHIVE ]; then
		echo "Clearing out $PKGARCHIVE ..." >$SRC/pkgdefs/install.out
		echo "Clearing out $PKGARCHIVE ..." >> $LOGFILE
		rm -rf $PKGARCHIVE/*
	else
		mkdir -p $PKGARCHIVE
		echo "Created $PKGARCHIVE" >$SRC/pkgdefs/install.out
		echo "Created $PKGARCHIVE" >> $LOGFILE
	fi
	cd $SRC/pkgdefs
	make -e install 2>&1 | tee -a $SRC/pkgdefs/install.out >> $LOGFILE

else
	echo "\n==== Not creating packages ====\n" >> $LOGFILE
fi

if [ "$build_ok" = "y" ]
then
	echo "\n==== Creating protolist system file at `date` ====" >> $LOGFILE
	protolist $ROOT > $ATLOG/proto_list_${MACH}
	echo "==== protolist system file created at `date` ====\n" >> $LOGFILE
fi

if [ "$t_FLAG" = "y" -a "$build_ok" = "y" ]
then
	echo "\n==== Backing up build to tape($TAPE) at `date` ====\n" \
	    >> $LOGFILE
	mt -f $TAPE rewind
	cd $CODEMGR_WS
	tar -cvf $TAPE * >> $LOGFILE 2>&1
	cd $PKGDEFS
	tar -cvf $TAPE * >> $LOGFILE 2>&1
	echo "\n==== Backed up build to tape($TAPE) ====\n" >> $LOGFILE
else
	echo "\n==== Not backing up build to tape ====\n" >> $LOGFILE
fi

if [ "$U_FLAG" = "y" -a "$build_ok" = "y" ]
then
	echo "\n==== Copying proto area to $PARENT_ROOT ====\n" >> $LOGFILE
	rm -rf $PARENT_WS/proto/root_$MACH/*
	mkdir -p $PARENT_ROOT
	cd $ROOT
	tar cf - . | ( cd $PARENT_ROOT;  umask 0; tar xpf - )
fi

echo "==== Nightly build completed at `date` ====" >> $LOGFILE

LLOG="$ATLOG/log.`date '+%m%d'`"
rm -rf $ATLOG/log.??`date '+%d'`
if [ -f $LLOG -o -d $LLOG ]
then
	LLOG=$LLOG.$$
fi

mkdir $LLOG
if [ "$m_FLAG" = "y" ]
then
	mv $mail_msg_file /tmp/t.$$
	echo "\n===== BUILD ERRORS =====\n" >> $mail_msg_file
	grep "make:" $SRC/install.out >> $mail_msg_file
	if [ "$p_FLAG" = "y" -a "$build_OK" = "y" ]
	then
	    echo "\n===== PKG BUILD ERRORS =====\n" >> $mail_msg_file
	    egrep "^ERROR|^WARNING" $SRC/pkgdefs/install.out | \
	        grep -v PSTAMP | grep -v v8plus >> $mail_msg_file
	fi
	cat /tmp/t.$$ >> $mail_msg_file
	rm -f /tmp/t.$$
fi

if [ "$m_FLAG" = "y" ]
then
	# If there is a reference proto list and the build was good,
	# compare the build's proto list with the reference to see changes
	# in proto areas.  Use the current exception list.
	if [ \( -f $REF_PROTO_LIST \) -a \( "$build_ok" = "y" \) ]
	then
	    echo "\n==== protocmp with $REF_PROTO_LIST ====\n" >> $mail_msg_file
	    protocmp -e $SRC/pkgdefs/etc/exception_list_$MACH \
		$REF_PROTO_LIST $ATLOG/proto_list_${MACH} >> $mail_msg_file
	fi
	# If the build was good, compare the build's proto list with current
	# package definitions to audit the quality of package definitions
	# and makefile install targets.  Use the current exception list.
	if [ "$build_ok" = "y" ]
	then
	    echo "\n==== protocmp with $SRC/pkgdefs ====\n" >> $mail_msg_file
	    protocmp -e $SRC/pkgdefs/etc/exception_list_$MACH \
		$SRC/pkgdefs $ATLOG/proto_list_${MACH} >> $mail_msg_file
	fi
	# Mail list of errors and audit results to the designated staff member
	/usr/bin/mailx -s "Nightly Build Completed." ${STAFFER} < $mail_msg_file
fi

if [ "$u_FLAG" = "y"  -a "$build_ok" = "y" ]
then
	( su $STAFFER -c \
	 cp' '$ATLOG/proto_list_${MACH}' '$PARENT_WS/usr/src/proto_list_${MACH})
fi

rm -f $mail_msg_file
mv $LOGFILE $LLOG
[ "$build_ok" = "y" ] && mv $ATLOG/proto_list_${MACH} $LLOG
