#! /bin/sh
#
#ident	"@(#)nightly.sh	1.59	95/05/31 SMI"
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved.
#
#	Build script for OS-Net source in a codemgr workspace.
#
USAGE='usage: nightly [-G|-I|-R] [-a[d]] [-bcimMpstuU] <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
	-i	use faster binary install utility in /opt/onbld/bin/install
	-m	send mail to $STAFFER at end of build
	-M	build _msg target and the SUNW0on package for L10n
	-p	create packages
	-s 	do a serial build instead of a parallel build
	-t	use tar to back up the build to $TAPE
	-u	update proto_list_${MACH} in the parent workspace
	-U	update headers and libraries (protolibs) 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/ongk/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
M_FLAG=n
p_FLAG=n
s_FLAG=n
t_FLAG=n
u_FLAG=n
U_FLAG=n
#
CPIO_OPT=
INS_OPT=
XMOD_OPT=
#
BAD_BLD=n
#
# examine arguments
#
while getopts GIRabcdimMpstuUx FLAG
do
	case $FLAG in
	  G )	a_FLAG=y
		b_FLAG=y
		u_FLAG=y;;
	  I )	a_FLAG=y
		b_FLAG=y
		INS_OPT="-i"
		m_FLAG=y
		p_FLAG=y
		u_FLAG=y;;
	  R )	c_FLAG=y
		INS_OPT="-i"
		m_FLAG=y
		p_FLAG=y;;
	  a )	a_FLAG=y;;
	  b )	b_FLAG=y;;
	  c )	c_FLAG=y;;
	  d )	CPIO_OPT="-d";;
	  i )	INS_OPT="-i";;
	  m )	m_FLAG=y;;
	  M )	M_FLAG=y;;
	  p )	p_FLAG=y;;
	  s )	s_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

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

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

export TW
PATH="${GK}:$PATH"; export PATH
#
# 	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; echo "==== list of environment variables ===="; env; echo) >> $LOGFILE

#
# 	Decide whether to bringover to the codemgr workspace 
#
if [ "$b_FLAG" = "y" ]
then
	(echo; \
	echo "==== bringover to $CODEMGR_WS at `date` ===="; echo) >> $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; echo "==== BRINGOVER LOG ===="; echo) >> $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; echo "==== No bringover to $CODEMGR_WS ===="; echo) >> $LOGFILE
fi

#
# 	Decide whether to clobber
#
if [ "$c_FLAG" = "y" ]
then
	(echo; echo "==== Make clobber at `date` ===="; echo) >> $LOGFILE
	nightly_clobber $1
else
	(echo; echo "==== No clobber at `date` ===="; echo) >> $LOGFILE
fi

#
#	Build OS-Networking source
#
(echo; echo "==== Building OS-Net source at `date` ===="; echo) >> $LOGFILE

if [ "$s_FLAG" = "y" ]
then
	echo "Starting serial build" >> $LOGFILE
	/bin/time build_ON $INS_OPT $XMOD_OPT $1 >> $LOGFILE 2>&1
	if [ $? -ne 0 ]
	then
		# if a bad build, do not do archives, packages, tape, nor
		# update of proto_list in parent
		BAD_BLD=y
		a_FLAG=n
		p_FLAG=n
		t_FLAG=n
		u_FLAG=n
		U_FLAG=n
	fi
else
	echo "Starting parallel build" >> $LOGFILE
	/bin/time build_ON -p $INS_OPT $XMOD_OPT $1 >> $LOGFILE 2>&1
	if [ $? -ne 0 ]
	then
		BAD_BLD=y
		a_FLAG=n
		p_FLAG=n
		t_FLAG=n
		u_FLAG=n
		U_FLAG=n
	fi
fi
(echo; echo "==== Ended OS-Net source build at `date` ===="; echo) >> $LOGFILE

#
#	Create cpio archives for preintegration testing (PIT)
#
if [ "$a_FLAG" = "y" ]
then
	(echo; \
	echo "==== Creating cpio archives at `date` ===="; echo) >> $LOGFILE
	mkbfu.596 "$ROOT" "$CPIODIR" >> $LOGFILE 2>&1
else
	(echo; echo "==== Not creating cpio archives ===="; echo) >> $LOGFILE
fi

#
#	Building Packages
#
if [ "$p_FLAG" = "y" ]
then
	(echo; echo "==== Creating packages at `date` ===="; echo) >> $LOGFILE
	pm $1 >> $LOGFILE 2>&1
else
	(echo; echo "==== Not creating packages ===="; echo) >> $LOGFILE
fi

if [ \( "$M_FLAG" = "y" \) -a \( "$BAD_BLD" = "n" \) ]
then
	(echo; \
	echo "==== Building L10n messages at `date` ===="; echo) >> $LOGFILE
	mkmsg $1 >> $LOGFILE 2>&1
fi

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

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

if [ "$U_FLAG" = "y" ]
then
	if [ "x$PARENT_ROOT" = "x" ]
	then
	    echo "==== PARENT_ROOT variable not set; protolibs not copied ===="\
		>> $LOGFILE
	else
	    (echo; echo "==== Copying protolibs to $PARENT_ROOT ===="; echo) \
		>> $LOGFILE
	    ( su $STAFFER -c \
	    '${GK}/cp_protolibs '$ROOT' '$PARENT_ROOT ) >> $LOGFILE 2>&1
	fi
fi

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

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

mkdir $LLOG
if [ "$m_FLAG" = "y" ]
then
	mv $mail_msg_file /tmp/t.$$
	(echo; echo "===== BUILD ERRORS ====="; echo) >> $mail_msg_file
	grep "make:" $LOGFILE >> $mail_msg_file
	if [ "$p_FLAG" = "y" ]
	then
	    (echo; echo "===== PKG BUILD ERRORS ====="; echo) >> $mail_msg_file
	    egrep "^ERROR|^WARNING" $LOGFILE | grep -v PSTAMP >> $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 \( "$BAD_BLD" = "n" \) ]
	then
	    (echo; echo "==== protocmp with $REF_PROTO_LIST ===="; \
		echo) >> $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 [ "$BAD_BLD" = "n" ]
	then
	    (echo; echo "==== protocmp with $SRC/pkgdefs ===="; \
		echo) >> $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
	# use sendmail (not mailx) to add Precedence: junk to header

        touch /tmp/final_mail.$$
        echo "Subject: Nightly Build Completed." >> /tmp/final_mail.$$
        echo "Precedence: junk" >> /tmp/final_mail.$$
        cat $mail_msg_file >> /tmp/final_mail.$$
	/usr/lib/sendmail $STAFFER < /tmp/final_mail.$$
        
fi

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

rm -f $mail_msg_file /tmp/final_mail.$$
mv $LOGFILE $LLOG
[ "$BAD_BLD" = "n" ] && mv $ATLOG/proto_list_${MACH} $LLOG
