#! /bin/sh
#
#ident	"@(#)build_ON.sh	1.16	94/02/22 SMI"
#
# Copyright (c) 1993 by Sun Microsystems, Inc.
#
#	called by the "nightly" script to build OS-Net source,
#	serially by default and in parallel with the -p option.
#	the -i option manipulates the path to use a binary install utility.
#
USAGE='	usage: build_ON [-i] [-p] <env_file>'
#
# also a less public -x option to invoke xmod scripts, exercising
# the excluded module build to demonstrate compliance with the
# source-binary initiative.

i_FLAG=n
p_FLAG=n
x_FLAG=n

# examine arguments
while getopts ipx FLAG
do
	case $FLAG in
	  i )	i_FLAG=y;;
	  p )	p_FLAG=y;;
	  x )	x_FLAG=y;;
	 \? )	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

#
# 	Set PATH for a build
#
#PATH="/opt/onbld/bin:/usr/sbin:/opt/SUNWspro/bin:/usr/ccs/bin:/usr/bin"
#[ "$i_FLAG" = "y" ] && PATH="/opt/onbld/bin/install:$PATH"
#[ "$p_FLAG" = "y" ] && PATH="/opt/teamware/ParallelMake/bin:$PATH"

#automount map for generic tools: teamware/onbld/ongk
TOOLPATH=/shared/ON/tools
 
#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"
else
        BLD="${TOOLPATH}/onbld/bin"
fi
if [ -d /opt/teamware/ParallelMake ]; then
        MK="/opt/teamware/ParallelMake/bin"
else
        MK="${TOOLPATH}/teamware/ParallelMake/bin"
fi
PATH="${BLD}:/usr/sbin:${SP}:/usr/ccs/bin:/usr/bin"

[ "$i_FLAG" = "y" ] && PATH="${BLD}/install:$PATH"
[ "$p_FLAG" = "y" ] && PATH="${MK}:$PATH"

TMPDIR="/tmp"
CH=

export	PATH CH TMPDIR
unset	CFLAGS LD_LIBRARY_PATH

# 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

##

TMPDIR="/tmp"
CH=

export	PATH 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=

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

export ENVLDLIBS1 ENVLDLIBS2 ENVLDLIBS3 \
	ENVCPPFLAGS1 ENVCPPFLAGS2 ENVCPPFLAGS3 \
	ENVCPPFLAGS4


echo; echo "==== starting at `date` ===="
if [ "$x_FLAG" = "y" ]
then
	cd $SRC/xmod 
	make -e all 
	if [ $? -ne 0 ]; then
		echo "*** ERROR: Bad return code from make in xmod directory."
		exit 1
	fi
	$SRC/xmod/xmod_cut
fi

cd $SRC

# main build
make -e install 
if [ $? -ne 0 ]; then
	echo "*** ERROR: Bad return code from make install."
	# a failure here could leave the effects of xmod_cut in place, so ...
	if [ "$x_FLAG" = "y" ]; then
		echo "restoring Excluded modules with xmod_add"
		$SRC/xmod/xmod_add
	fi
	exit 1
fi

if [ "$x_FLAG" = "y" ]
then
	$SRC/xmod/xmod_add 
	make -e install_xmod 
	if [ $? -ne 0 ]; then
		echo "*** ERROR: Bad return code from make install_xmod."
		exit 1
	fi
fi
echo; echo "==== stopped at `date` ===="

exit 0
