# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#ident	"$Header: :mkcmd 1.11 89/12/06 $@(#)mk::mkcmd	1.17"
#
PARALLEL=4 export PARALLEL
MAKE=${MAKE:-make}
umask 222		# executables 555, other files 444
#	If we are in a cross compilation environment then use cross compiler
if [ "$ROOT" != "" ]
then
	if u3b2 || u3b15
	then
		AS=m32as	export AS
		CC=m32cc	export CC
		LD=m32ld	export LD
	fi
else
	AS=as	export AS
	CC=cc	export CC
	LD=ld	export LD
fi
#	Insure that only one machine type is valid
#	Note: the redirection is needed to handle the possibility
#		of a nonexistent file in the $MACH machine list
MACH="vax pdp11 u3b2 u3b u3b15 i386"
(truecnt=0
for mach in $MACH
do
	`$mach`
	if [ $? -eq 0 ]
	then
		truecnt=`expr $truecnt + 1`
	fi
done
if [ $truecnt -ne 1 ]
then
	echo ":mkcmd: **** Only one of the following machine types can"
	echo "		be true: $MACH"
	exit 1
fi ) 2>/dev/null
#
places=${places-$ROOT/etc/places}
if [ -r $places ]
then
	. $places
else
	SRC=${SRC-$ROOT/usr/src}
	LSRC=${LSRC-$ROOT/usr/lsrc}
	BIN=${BIN-$ROOT/bin}
	UBIN=${UBIN-$ROOT/usr/bin}
	LBIN=${LBIN-$ROOT/usr/lbin}
fi
if [ ! -d $SRC/cmd ]
then
        exit 0
fi

LDFILE=${LDFILE-${SRC}/ldflags}
# Initialize default flag values:
#	fflag	- controls the FFLAG varible. It is set to either null
#		(use hardware floating point) or '-f' (use software
#		simulation floating point).
#	sflag	- shared text, turns off the -i and -n loader options.
#		Saves the intermediate .c file from a yacc or lex in
#		<filename>.x for later :mkcmd runs.
#	ldlibs	- shared library flag. It is either "-lnsl_s" (build w/the
#		shared libnsl_s.a) or "-lc_s -lnsl_s" (build w/both 
#		shared libc_s.a and libnsl_s.a). Default is both.
#	iflag	- seperate I&D space. It is either set to '-i' (when building
#		certain commands on a 16-bit machine (e.g. PDP11/70)
#		or null (for all other cases).
fflag=
sflag=
iflag=
#ldlibs=${LDLIBS:-"-lc_s -lnsl_s"}
ldlibs=
ldflags=
for opt in $*
do
	case $opt in

	-f)	fflag="-f"
		;;

	-s)	sflag=-s
		yaccrm="YACCRM=:"
		iflag=
		;;

	-*)	echo ":mkcmd; **** unknown option '$opt' - ignored"
		;;

	*)	break
		;;
	esac
	shift
done
#
#	Process remaining arguments as source that needs to be built.
#	The three interesting variables are:
#	B	- basename of each argument
#	BASE	- basename of each option with its dot suffix removed
#	OBJ	- file to be installed if the build is successful
#
cd $SRC/cmd
arguments=$*
for arg in $arguments
do(
#	Local version takes precedence over official version
	if [ -f $LSRC/cmd/$arg -o -d $LSRC/cmd/$arg ]
	then	source=$LSRC
	else	source=$SRC
	fi
	cd ${source}/cmd
	OBJ=
	B=`basename $arg`
#	Is this an aberrant file with its makefile in the $SRC/cmd directory?
#	If so then skip this file since it will handled by the makefile
	case $B in
		*.c | *.s | *.sh)
			b=`basename $B ".[cs]"`
			if [ -f $b.mk ]
			then	continue
			fi
			;;
	esac
	echo "\n======== $B"
	BASE=$B
#	if not a directory then remove suffix and determine the file type
	if [ ! -d $B ] 
	then	BASE=`echo $B | sed -e 's/\(.*\)\..*/\1/'`
		if [ -f ${BASE}.mk -o -f $LSRC/cmd/${BASE}.mk ]
		then	B=${BASE}.mk
		elif [ -f ${BASE}.rc -o -f $LSRC/cmd/${BASE}.rc ]
		then	B=${BASE}.rc
		elif [ -f ${BASE}.c  -o -f $LSRC/cmd/${BASE}.c ] 
		then	B=${BASE}.c
		elif [ -f ${BASE}.sh -o -f $LSRC/cmd/${BASE}.sh ] 
		then	B=${BASE}.sh
		elif [ -f ${BASE}.y -o -f $LSRC/cmd/${BASE}.y ]
		then	B=${BASE}.y
		elif [ -f ${BASE}.l -o -f $LSRC/cmd/${BASE}.l ] 
		then	B=${BASE}.l
		elif [ -f ${BASE}.s -o -f $LSRC/cmd/${BASE}.s ] 
		then	B=${BASE}.s
		elif [ -f ${BASE}.o -o -f $LSRC/cmd/${BASE}.o ] 
		then	B=${BASE}.o
		fi
	fi
#
	if [ "$source" = "$SRC" -a -f $LSRC/cmd/$B ]
	then	source=$LSRC
		cd ${source}/cmd
	fi
#
	if [ -r $LDFILE ]
	then	ldflags=`grep "^${BASE} " $LDFILE | sed -e "s/${BASE} //"`
		if [ -z "$fflag" ]
		then	ldflags=`echo $ldflags | sed -e 's/-f//'`
		fi
		if [ "$sflag" = "-s" ]
		then	ldflags=`echo $ldflags | sed -e 's/-i//'`
		else	if [ `expr x$ldflags : '.*-i'` != 0 ] 
			then	ldflags=`echo $ldflags | sed -e 's/-n//'`
			fi
		fi
	else	ldflags=""
	fi
#
#	Now build the command according to its file type
	case $B in

	*.mk)
		CMD="$MAKE -b -f ${BASE}.mk LDLIBS='$ldlibs' IFLAG=\"$iflag\" FFLAG=\"$fflag\" install"
		if [ "$ldflags" ]
		then	CMD="$CMD LDFLAGS='$ldflags'"
		fi
		echo "$CMD"
		if eval $CMD
		then	ret=0
		else	ret=1
		fi
		CMD="$MAKE -b -f ${BASE}.mk LDLIBS='$ldlibs' IFLAG=\"$iflag\" FFLAG=\"$fflag\" $yaccrm clobber"
		echo "$CMD"
		eval $CMD
		if [ $ret -ne 0 ]
		then	echo "**** Build of ${BASE} failed ($MAKE)"
		fi
		;;

	*.rc)	sh -x $B
		if [ $? -ne 0 ]
		then	echo "**** Build of $B failed (sh)"
		fi
		;;

	*.c)
		CMD="$CC -O $ldflags -s -o ./${BASE} $B $lib"
		echo "$CMD"
		if eval $CMD
		then	OBJ=${BASE}
		else	echo "**** Build of ${BASE} failed ($CC)"
			rm -f ./${BASE}.o ./${BASE}
		fi
		lib=
		;;

	*.sh)	echo "cp $B ${BASE}"
		if cp $B ${BASE}
		then	OBJ=${BASE}
		else	echo "**** Build of ${BASE} failed (cp)"
		fi
		;;

	*.s)	CMD="$CC -s -o ./${BASE} $B"
		echo "$CMD"
		if eval $CMD
		then	OBJ=${BASE}
		else	echo "**** Build of ${BASE} failed ($CC)"
			rm -f ./${BASE}.o ./{BASE}
		fi
		;;

	*.y)	if [ "$sflag" != "-s" ]
		then	echo "yacc $B"
			if yacc $B
			then	echo "mv y.tab.c ${BASE}.c"
				mv y.tab.c ${BASE}.c
				lib=-ly places=$places LDFILE=$LDFILE sh $SRC/:mkcmd $sflag $fflag ${BASE}.c
			else	echo "**** Build of ${BASE} failed (yacc)"
			fi
			rm -f ${BASE}.c
		else
			mv ${BASE}.x ${BASE}.c
			lib=-ly places=$places LDFILE=$LDFILE sh $SRC/:mkcmd $sflag $fflag ${BASE}.c
			mv ${BASE}.c ${BASE}.x
		fi
		;;

	*.l)	if [ "$sflag" != "-s" ]
		then	echo "lex $B"
			if lex $B
			then	echo "mv lex.yy.c ${BASE}.c"
				mv lex.yy.c ${BASE}.c
				lib=-ll places=$places LDFILE=$LDFILE sh $SRC/:mkcmd $sflag ${BASE}.c
			else	echo "**** Build of ${BASE} failed (lex)"
			fi
			rm -f ${BASE}.c
		else
			mv ${BASE}.x ${BASE}.c
			lib=-ll places=$places LDFILE=$LDFILE sh $SRC/:mkcmd $sflag ${BASE}.c
			mv ${BASE}.c ${BASE}.x
		fi
		;;

	*.x)	continue
		;;

	*.o)	rm -f $B 
		;;

	*)
		if [ -d $B ]
		then	echo "cd $B"
			cd $B
			if [ -f $B.mk ]
			then	CMD="$MAKE -b -f $B.mk ARGS=\"${ARGS}\" LDLIBS='$ldlibs' IFLAG=\"$iflag\" FFLAG=\"$fflag\" install"
				if [ "$ldflags" ]
				then	CMD="$CMD LDFLAGS='$ldflags'"
				fi
				echo "$CMD"
				if eval $CMD
				then	ret=0
				else	ret=1
				fi
				CMD="$MAKE -b -f $B.mk LDLIBS='$ldlibs' IFLAG=\"$iflag\" FFLAG=\"$fflag\" $yaccrm clobber"
				echo "$CMD"
				eval $CMD
				if [ $ret -ne 0 ]
				then	echo "**** Build of ${BASE} failed ($MAKE)"
				fi
			elif [ -f $B.rc ]
			then	sh -x $B.rc
			else	echo "**** Build of $B failed (no makefile found in directory)"
			fi
			rm -f *.o
		else	echo "**** Build of ${BASE} failed (can not determine file type)"
		fi

	esac
#
#	If build was successful install new object in appropriate bin
	if [ -n "$OBJ" ]
	then
		if [ "$source" = "$LSRC" ]
		then	echo "install -f $LBIN $OBJ"
			install -f $LBIN $OBJ
		else	echo "install -f $UBIN $OBJ"
			install -f $UBIN $OBJ
		fi
		rm -f $OBJ
	fi
) & done
wait
date
