#! /bin/sh
#
# Stole this from the common SGI mmake. Hack it up to only do PR. Typical
# usage is
#
#	mmake -cuH
#
# This is a stripped version of SGI internal graphics build script mmake.
# When defining new commands, make efforts to avoid collision with current
# command flags so future integration would be easy.
#
# Key Example of usage
#
#	mmake -Iu	build the header files and p_tupdate all files
#	mmake -H	builds everything
#	mmake -c	clobbers everything
#	mmake -a	runs the nightly regression tests (will be done after
#			all other build activity has completed, if any).
#
# List of things you should do on your system prior to running this script:
#
#       1. Reserve a workarea underneath your home account that's large enough
#	   to hold the PR sources & executables (this script, mmake, will set
#	   $WORKAREA to $HOME/mdevroot.
#
#	2. nfs mount the source tree (ie madman:/mdev)
#
#	   Note: this is not necessary if you put the following lines in your
#	         $WORKAREA/.workarea file:
#
#	   workarea.sm_location: /mdev
#	   workarea.sm_machine: madman
#          workarea.sm_transport_method: network
#
#	3. Setup an account on the srctree host machine (e.g. madman); ptools 
#	   will use rsh to access the machine hosting the source tree.
#
#	4. Get all of the standard system libraries and header files installed
#	   underneath your $WORKAREA.  There are two ways you can do this:
#
#	Method One:
#
#          Make sure your local machine has a full complement of compilers
#	   and build tools.  The PR build environment does not use a workarea
#	   based TOOLROOT, so do not set this environment variable or attempt
#	   to set up a TOOLROOT in your source tree; it's needless complexity
#	   at this stage of development.  The Irix subsystems you must 
#	   install include the following:
#
#	   compiler_dev  (be sure to get the internal headers too)
#	   compiler_eoe
#	   gl_dev	 (GL header files & libimage.a image file support)
#	   x_dev	 (X11 header files & libraries)
#
#	   Symlink everything in your /usr/include directory to your
#	   $WORKAREA/usr/include with the following commands:
#
#	   ln -s /usr/include/* $WORKAREA/usr/include
#	   rm $WORKAREA/usr/include/make     (break the symlink...)
#	   mkdir $WORKAREA/usr/include/make  (...so we can install headers)
#	   ln -s /usr/include/make/* $WORKAREA/usr/include/make
#
#	   Symlink everything in your /usr/lib directory to your 
#	   $WORKAREA/usr/lib with the following commands:
#
#	   ln -s /usr/lib/* $WORKAREA/usr/lib
#
#	Method Two:
#
#	   Install the build isms needed to build PR software directly into 
#          your $WORKAREA (this will install all the necessary include files 
#	   and libraries into your build arena rooted beneath $WORKAREA):
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/build_root -r $ROOT
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/cmplrs_root -r $ROOT
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/gfx_root -r $ROOT
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/irix_root -r $ROOT
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/man_root -r $ROOT
#
# inst -a -x -f /hosts/babylon.wpd/usr/dist/sherwood/LATEST.5.2MR/root/x_root -r $ROOT
#
#
#	5. nfs mount the ecad based tools on your build machine; you can
#	   use automount if you create the following symlink as root on
#	   your machine (for example) and run the automount daemon
#	   (do a chkconfig automount on and reboot if it's not already on):
#
#	   ln -s /hosts/mileena.engr/ecad /ecad	
#	   (this assumes the ecad tools are on a machine called "mileena.engr")
#
#	6. For info on how to setup a nightly build, see the file
#	   "README.nightly".  The script 'mnightly' will set the necessary 
#	   environment variables, then invoke mmake to clobber, update & build,
#	   then sends you (via e-mail) a filtered report of what actually got 
#	   done during the build.
#
# IMPORTANT NOTES:
#	
# Options (ones we support are marked with PR)
#
#	-0	IP20 build
#
#	-1	IP21 build
#
#	-2	MAGNUM/IP12 build
#
#	-3	IP22 build
#
#	-4	IP4 build
#
#	-5	IP5/IP7/IP13/IP15 build
#
#	-6	IP6 build
#
#	-7	R4000/IP17 build
#
#	-8	4D100 build
#
#	-9	R4000/IP19 build
#
# PR	-a	Run PR nightly regressions (used to install new IRIX4 ISMs) 
#		(these will be run after all other build activity completes,
#		 if any).
#
#	-b	specify the inst ROOT for IRIX4 ISMs (default is $ROOT) 
#
# PR	-c	clobber
#
#	-e	build EXPRESS graphics
#
#	-g	build CLOVER1 graphics
#
#	-h	"build irix gfx x" ISM directory identifier	eg. -h P26
#		to specify ISMs at /hosts/babylon.engr/usr/dist/5.1/P26
#
# 	-i	install new "build irix gfx x" ISMs (default is LATEST)
#
#	-j	"cmplrs" ISM directory identifier		eg. -j P25
#		to specify ISMs at /hosts/babylon.engr/usr/dist/5.1/P25
#
#	-k	install new "cmplrs" ISMs (default is LATEST)
#
#	-l	(ell) build LIGHT graphics
#
#	-n	build Newport graphics
#
#	-p	build PI graphics
#
#	-q	RAGANROK ISM directory identifier eg. -q 8028
#
#	-r	install new RAGANROK ISMs (default is LATEST)
#
#	-s	build SERVER graphics
#
#	-t	build GT graphics
#
# PR	-u	update the tree
#
#	-x	build GTX graphics
#
#	-v	build VGX graphics
#
#	-w	build SKYWRITER version of VGX graphics
#
#	-z	build VENICE graphics
#
#	-A	specify $IRIX4ROOT				eg. -A $ROOT/4
#
#	-B	build GLdebug
#
#	-C	build X clients
#
# PR	-F	build diags - Currently only VENICE/MIRAGE supported
#
#	-G	build GLs
#
# PR	-H	build simulator related stuff (libsimgl, tests libs, & sim)
#			=> venice stuff is currently incomplete
#
# PR	-I	build symbolic linked headers and libraries
#		WAS - build headers
#
#	-J	abort when about to start building (useful for doing tree clobbers)
#
#	-K	build kernels
#
# PR	-L	turn on library builds.
#		WAS - turn on libgl.a build, off dy default (now)
#
# PR	-M	real machine where src code is		eg. -M jake
#
#	-N	build libglnohw.a
#
#	-O	build OpenGLs
#
#	-P	toolroot				eg. -P ~/trees/cctoolroot
#
#	-Q	build GTST
#
# PR	-R	$ROOT					eg. -R ~/jake
#
#	-S	build libgl_s using a 4.0 ROOT & TOOLROOT as specified in the
#		environment variable IRIX4ROOT 
#
# PR	-T	where the root of the srctree is on	eg. -T jake
#		src code machine.
#
# PR	-U	build ucode
#
# PR	-W	specifiy $WORKAREA			eg. -W ~/tree/shrwd
#
#	-V	build corresponding video bits.
#
#	-X	build xserver
#	
#	-Y	build X libraries (default off)
#
#	-Z	specify automount path to ISMs		eg. -Z /hosts/myhost/dist/5.1/
#
#
# Note
#	There are interdepenancies between WORKAREA ROOT TOOLROOT.  The
#	script tried to do the right thing.
#
# Examples
#
#	mmake
#		does nothing
#
#	cd $HOME
#
#	mmake -cuzGIKU -W $HOME/trees/sherwood 
#		- p_tupdates virtually everyting you need for VENICE dev.
#		- builds VENICE ucode, gl, and kernel for IP7
#
#	mmake -cuzGXIKU -W $HOME/trees/sherwood 
#		- All that the previous one does and builds the X server
#
#	mmake -05K -W $HOME/trees/sherwood 
#		- makes IP20 and IP5 kernels 
#

echo "\n"`basename $0`" $@ started - `date`\n"

#
# Initial enviroment settings
#
P_TUPDATE="p_tupdate -v"
P_CHECK="p_check -s"
P_PURGE="p_purge -yiu"
MAKE="make -k"

DIAGBUILD=FALSE
ABORTBUILD=FALSE
SKELETON=FALSE

SRCMACH="madman"
SRCTREE="mdev"
WORKAREA="$HOME/mdevroot"

UPDATE=FALSE
CLOBBER=FALSE
REGRESSION=FALSE

INCBUILD=FALSE
BUILD=FALSE

#########################################################################
#
# Parse input
# 
########################################################################
#
# mmake from sherwood has - while getopts 0123456789ab:cegh:ij:klnpq:rstuxvwzA:BCFGHIJKLMN:OP:QR:ST:UVW:XYZ: c

while getopts acuHIJMP:R:T:W: c
do
    case $c in
	a)	REGRESSION=TRUE ;;
	c)	CLOBBER=TRUE ;;
	u)	UPDATE=TRUE ;;
	H)	BUILD=TRUE ;;
	I)	INCBUILD=TRUE ;;
	J)	ABORTBUILD=TRUE ;;
	M)	SRCMACH=$OPTARG ;;
	P)	TOOLROOT=$OPTARG ;;
	R)	ROOT=$OPTARG ;;
	T)	SRCTREE=$OPTARG ;;
	W)	WORKAREA=$OPTARG ;;
	\?)	echo $USAGE; exit 2 ;;
    esac
done
shift `expr $OPTIND - 1`

#
# Turn on trace
set -x

# Setup misc environment stuff
#
TMPDIR=/usr/tmp

#
# Test for missing enviroment stuff
#
if test -z "$WORKAREA"
then
    if test -z "$ROOT"
    then
	WORKAREA=$ROOT
    else
	echo "$WORKAREA not set, use -W"
	exit 2
    fi
fi

if test -z "$ROOT"; then ROOT=$WORKAREA; fi
if test -z "$TOOLROOT"; then TOOLROOT=/; fi

# Aliases
#
PR=$WORKAREA/PR
PATH=.:/usr/local/bin/ptools:$ROOT/usr/bsd:$ROOT/usr/sbin:$ROOT/bin:/usr/bsd:/bin:/usr/bin:/usr/sbin:/etc:/usr/etc:/lib

# force no -cckr since so many things flame with it turned on
unset SGI_CC

echo "WORKAREA = $WORKAREA"
echo "TOOLROOT = $TOOLROOT"
echo "ROOT = $ROOT"

# Exports
#
export ROOT WORKAREA PATH TMPDIR TOOLROOT 

#########################################################################
#
# Build Tree Skeleton
#
#########################################################################

SKELETON=TRUE
if test $SKELETON = TRUE
then
    if [ ! -d $ROOT ]; then mkdir -p $ROOT; fi
    if [ ! -d $TOOLROOT ]; then mkdir -p $TOOLROOT; fi
    if [ ! -d $WORKAREA ]; then mkdir -p $WORKAREA; fi

    cd $WORKAREA; 

    if [ ! -f $WORKAREA/.workarea ] 
    then 
	touch .workarea 
	echo "workarea.sm_location: /$SRCTREE" >>.workarea 
	echo "workarea.sm_machine: $SRCMACH" >>.workarea

	CLOBBER=FALSE;
    fi

    if [ ! -f $WORKAREA/census ] 
    then 
	touch census
    fi

    for dir in  $WORKAREA		\
		$WORKAREA/usr/include	\
		$WORKAREA/usr/lib	\
		$PR/doc
    do
	if [ ! -d $dir ];  then mkdir -p $dir; fi
    done

fi

#########################################################################
#
# Clobber stuff
#
#########################################################################

if test $CLOBBER = TRUE
then

    #
    # For now, C simulators, ucode,  host libraries
    #

    for dir in  $PR/include		\
		$PR/libultra		\
		$PR/tools		\
		$PR/iosim		\
		$PR/debugger		\
		$PR/rspasm		\
		$PR/rspsim		\
		$PR/rdpsim		\
		$PR/rdpsim/test		\
		$PR/rdpsim/test/rdp	\
		$PR/man			\
		$PR/rspcode		\
		$PR/apps		\
		$PR/apps.released
    do
	cd $dir; make clobber; make clean
    done
fi


#########################################################################
#
# tupdate stage
#
#########################################################################

if test $UPDATE = TRUE
then
    # Grab new mmake in case people have it symlinked
    #
    cd $PR/doc; $P_TUPDATE; $P_CHECK | $P_PURGE 

    # Grab new defs & rules
    cd $PR; $P_TUPDATE PRdefs PRrules

    if test $INCBUILD = TRUE
    then
	# header files
	#
	cd $WORKAREA/usr/include; ln -s /usr/include/* .

	# libraries
	#
	cd $WORKAREA/usr/lib; ln -s /usr/lib/* .
    fi

    #
    # C simulators, ucode, host libraries, apps, iosim environment
    #

    for dir in  $PR/include		\
		$PR/libultra		\
		$PR/tools		\
		$PR/iosim		\
		$PR/debugger		\
		$PR/rspasm		\
		$PR/rspsim		\
		$PR/hw			\
                $PR/hw2                 \
                $PR/rdpsim		\
		$PR/rspcode		\
		$PR/doc			\
		$PR/man			\
		$PR/apps		\
		$PR/apps.released
    do
	cd $dir; $P_TUPDATE; $P_CHECK | $P_PURGE;
    done

fi

#########################################################################
#
# build stage
#
#########################################################################

if test $ABORTBUILD = TRUE
then
    exit 0;
fi

INSTOPTS=-t
export INSTOPTS

if test $BUILD = TRUE
then

	cd $PR; make headers
	cd $PR; make exports

    #
    # C simulators, ucode,  host libraries, apps
    #

    for dir in  $PR/include		\
    		$PR/libultra		\
		$PR/tools		\
		$PR/debugger		\
		$PR/rspasm		\
		$PR/rspsim		\
		$PR/rdpsim		\
		$PR/rdpsim/test/rdp	\
		$PR/rspcode		\
		$PR/iosim		\
		$PR/man			\
		$PR/apps		\
		$PR/apps.released
    do
	cd $dir; make -k install
    done

fi

#########################################################################
#
# regression stage
#
#########################################################################

if test $REGRESSION = TRUE
then


#
# Build the iosim based simulator for the verilog mspan debugging environment,
# then build the nightly regression targets in apps/rdpatt, apps/rdpcov, 
# apps/rdpew, apps/rdptex, and finally in the RDP simulator directory.
#

RSP_UCODE=../../rspcode/graphics
export RSP_UCODE

    #
    # Build 'simv.ipc', and put the goodies in rdpsim/test/rdp.
    #
    cd $PR/hw/chip/sim; make ipc SIMVDIR="$PR/rdpsim/test/rdp"

    #
    # Build the nightly_regression targets in the apps directories, then
    # use the rdram files generated there by making the nightly_regression
    # target in PR/rdpsim/test/rdp.
    #
    for dir in  $PR/apps/rdpatt		\
		$PR/apps/rdpcov		\
		$PR/apps/rdpew		\
		$PR/apps/rdptex		\
		$PR/apps/rdpms		\
		$PR/rdpsim/test/rdp
    do
	cd $dir; make nightly_regression
    done
fi

#########################################################################

echo `basename $0`" $@ ended - `date`\n"
