#!/bin/csh
#
# Last Edited: Jan 10 15:48:07 PST 1996
#
# make_ides_for_mfg
#
# Calls _make_ides_for_mfg which does all the real work.
#
# A script to do nightly builds of ide executables for manufacturing.
#
# Usage: make_ides_for_mfg [-nopurge] [-noupdate] [-noclobber] [-gold] 
#	[-mail] [-root <path>] [-workarea <path>] 
#	[-toolroot <path>] [-product <string>]
#
#	All arguments are optional:
#	-nopurge	-- don't p_purge files. Default is to purge.
#	-noupdate	-- don't p_tupdate files. Default is to update.
#	-noclobber	-- don't clobber files. Default is to clobber.
#	-gold		-- copies shell.ide to /usr/local/boot/gold.ide
#	-mail		-- send out email with build results. Default is no mail
#	-root		
#	-workarea
#	-toolroot 	-- set root, workarea, or toolroot. Default is to use
#			   the values in the shell which calls this script.
#
#
# Set some env vars
#

#
# Get arguments if any
#
unalias rm

setenv INSTOPTS -t
setenv LOGFILE `date '+%y%m%d_%H%M'`
setenv ERRFILE $LOGFILE.err
setenv CLOBBER 1
setenv UPDATE 1
setenv PURGE 1
setenv SEND_MAIL 0
setenv GOLD	0
setenv PROM 	1

while ($#argv)
	switch ($argv[1])
		case -noclobber:
			setenv CLOBBER 0
			breaksw
		case -noupdate:
			setenv UPDATE 0
			breaksw
		case -nopurge:
			setenv PURGE 0
			breaksw
		case -workarea:
			setenv WORKAREA $argv[2]
			breaksw
		case -toolroot:
			setenv TOOLROOT $argv[2]
			breaksw
		case -root:
			setenv ROOT $argv[2]
			breaksw
		case -mail:
			setenv SEND_MAIL 1
			breaksw
		case -gold:
			setenv GOLD 1
			breaksw
		case -product:
			setenv PRODUCT $argv[2]
			breaksw
		case -noprom:
			setenv PROM 0
			breaksw
	endsw
	shift
end

setenv LOGDIR $WORKAREA/stand/arcs/ide/buildlog/$PRODUCT

if !(-e $LOGDIR) then
	mkdir -p $LOGDIR
endif

if (-e $LOGDIR/$LOGFILE) then
	cp $LOGDIR/$LOGFILE $LOGDIR/$LOGFILE.old
	rm $LOGDIR/$LOGFILE
endif
touch $LOGDIR/$LOGFILE

if (-e $LOGDIR/$ERRFILE) then
	cp $LOGDIR/$ERRFILE $LOGDIR/$ERRFILE.old
	rm $LOGDIR/$ERRFILE
endif

# capture all the output
tail -f $LOGDIR/$LOGFILE &

./_make_ides_for_mfg $PURGE $UPDATE $CLOBBER $LOGFILE $WORKAREA $ROOT $TOOLROOT >>& $LOGDIR/$LOGFILE

if ($status) then # _make_ides_for_mfg exited with error
	echo >>& $LOGDIR/$LOGFILE
	echo "_make_ides_for_mfg exited with error" >>& $LOGDIR/$LOGFILE
endif

sleep 3 # wait for everything to be done

echo >>& $LOGDIR/$LOGFILE
echo "Script completed at `date`" >>& $LOGDIR/$LOGFILE
echo >>& $LOGDIR/$LOGFILE

killall tail
sleep 2
#
# Check for errors
#
echo | tee -a $LOGDIR/$LOGFILE 
echo "Checking log file..." | tee -a $LOGDIR/$LOGFILE
echo | tee -a $LOGDIR/$LOGFILE
egrep -n "Error code|Error in reading|Permission denied|No such file|Usage|_make_ides_for_mfg exited|Command not found|File exists" $LOGDIR/$LOGFILE >>! $LOGDIR/$ERRFILE

if ($status == 0) then # found errors
   banner "Errors" | tee -a $LOGDIR/$LOGFILE
   echo | tee -a $LOGDIR/$LOGFILE; echo | tee -a $LOGDIR/$LOGFILE
   echo "Log file in $LOGDIR/$LOGFILE" | tee -a $LOGDIR/$LOGFILE
   echo "Error file in $LOGDIR/$ERRFILE" | tee -a $LOGDIR/$LOGFILE
   if ($SEND_MAIL == 0) then 
	echo "ide build had errors. See $LOGDIR/$LOGFILE"
   else
	echo "ide build had errors. See $LOGDIR/$LOGFILE" | Mail -s "ide build had errors. See $LOGDIR/$LOGFILE" shou@engr dilip@engr sridharv@engr heintz@mfg keiming@engr 
   endif
else
   banner "Ran OK" | tee -a $LOGDIR/$LOGFILE
   if ($GOLD == 1) then 
	cp $WORKAREA/stand/arcs/ide/$PRODUCT.O/shell.ide /usr/local/boot/gold.ide
   endif
   echo "Log file in $LOGDIR/$LOGFILE" | tee -a $LOGDIR/$LOGFILE
   if ($SEND_MAIL == 0) then 
	echo "ide build okay. Log in $LOGDIR/$LOGFILE"
   else
	echo "ide build okay. Log in $LOGDIR/$LOGFILE" | Mail -s "ide build okay. Log $LOGDIR/$LOGFILE" shou@engr dilip@engr sridharv@engr heintz@mfg keiming@engr
   endif
   if (-e $LOGDIR/$ERRFILE) then
		rm $LOGDIR/$ERRFILE
   endif
#
# Check for sizes of mfg ides'
#
#   cd $WORKAREA/stand/arcs/ide/$PRODUCT.O
#   echo "Checking for sizes. The text column values must be < 1441792"
#   size -B *.ide.elf

#   echo "All values in this column must be < 3407872 (data + bss)"  
#   size -B *.ide.elf | awk '{print $2 + $3, $6}'
endif

echo "Done" | tee -a $LOGDIR/$LOGFILE
