#ident	"@(#)mk::mkxcpcmd	1.1.2.1"
#
MAKE=${MAKE:-make}
CLOBBER=${CLOBBER:-"ON"}
#	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
	if i386
	then
		AS=i386as	export AS
		CC=i386cc	export CC
		LD=i386ld	export LD
		AR=i386ar	export AR
	fi
	PATH=$ROOT/xenv:/usr/ccs/bin:/usr/bin:/usr/sbin:/etc:$PATH
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"
(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
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).
#	symlink -variable to pass symbolic links to makefiles
fflag=
sflag=
iflag=
CCSTYPE=${CCSTYPE:-ELF}; export CCSTYPE
if [ x$CCSTYPE = xELF ]
then
	ldlibs=${LDLIBS:-"-dy"}
else
	ldlibs=${LDLIBS:-"-lc_s"}
fi
ldflags=
symlink=${SYMLINK:-"ln -s"}
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/xcpcmd
arguments=$*
for arg in $arguments
do(
	OBJ=
	B=`basename $arg`
#	if not a directory then remove suffix and determine the file type
	if [ "$source" = "$SRC" -a -f $LSRC/xcpcmd/$B ]
	then	source=$LSRC
		cd ${source}/xcpcmd
	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
BASE=$B
if [ -d $B ]
then	echo "cd $B"
	echo "\n======== $B"
	cd $B
	if [ -f $B.mk ]
	then	CMD="$MAKE -b -f $B.mk ARGS=\"${ARGS}\" SYMLINK='$symlink' 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 SYMLINK='$symlink' LDLIBS='$ldlibs' IFLAG=\"$iflag\" FFLAG=\"$fflag\" $yaccrm clobber"
		test "$CLOBBER" != "OFF" &&
			(echo "$CMD" ; eval $CMD)
		if [ $ret -ne 0 ]
		then	echo "**** Build of ${BASE} failed ($MAKE)"
		fi
	else	echo "**** Build of $B failed (no makefile found in directory)"
	fi
	rm -f *.o
else	echo ":mkcmd: *** no directory found for $B under $source"
fi

#
#	If build was successful install new object in appropriate bin
);done
