#ident	"@(#)mk:i386at/:mkxenv	1.4.3.11"
#ident	"$Header: $"

Cpu=$1
Root=$2
Tools=$3
Mach=$4
Work=$5
Pfx=$6

set +e

cd ${Root}/usr/src/${Work}/cmd/sgs/xenv/${Cpu}

#
#	Note below, the default parameters are relative
#	to TOOLS.
#

echo "$0: Running Cross pathedit"
(
	echo n		# Answering NO to building native CCS
	echo y		# Answering YES to building with default parameter
	echo y		# Answering YES to parameters question
) | sh ./pathedit ${Cpu} ${Root} ${Tools} ${Mach} ${Work} ${Pfx}

echo "$0: Building cross make (${Cpu}make)"
cd ${Root}/usr/src/${Work}/cmd/make
make -f make.mk YACC=yacc
cp make ${Tools}/usr/ccs/bin/${Cpu}make
make -f make.mk clobber

cd ${Root}/usr/src/${Work}/cmd/sgs

${Tools}/usr/ccs/bin/${Cpu}make -P -f sgs.mk clobber \
	CMDRULES=${Root}/usr/src/${Work}/cmd/sgs/xenv/${Cpu}/sgsrules \
	OSRULES=${TOOLS}/etc/lib.rulefile

${Tools}/usr/ccs/bin/${Cpu}make -P -f sgs.mk install \
	CMDRULES=${Root}/usr/src/${Work}/cmd/sgs/xenv/${Cpu}/sgsrules \
	OSRULES=${TOOLS}/etc/lib.rulefile


#
#	Get an install command into xenv
#
if [ -d "${Root}/usr/src/${Work}/cmd/install" ]
then
	cd ${Root}/usr/src/${Work}/cmd/install
	cp install.sh ${Tools}/bin/install
else
	cp /etc/install ${Tools}/bin
fi

#	Put the chmod here so we'll stop on error
#	(if Tools/bin/install is not there)

chmod 0755 ${Tools}/bin/install

#
#	Populate $TOOLS/etc with the OS version of rulefiles.
#	Populate $TOOLS/etc with uts.rulefile and depend.rules
#

[ -d ${Tools}/etc ] || mkdir -p ${Tools}/etc
cp ${Root}/usr/src/${Work}/build/rulefiles/* ${Tools}/etc
cp ${Root}/usr/src/${Work}/uts/uts.rulefile ${Tools}/etc
cp ${Root}/usr/src/${Work}/uts/depend.rules ${Tools}/etc

#
#	Place a crossenv front end script which points to
#	the OS CMDRULES, LIBRULES, and UTSRULES while we're at it.
#

[ -d ${Tools}/usr/ccs/bin ] || mkdir -p ${Tools}/usr/ccs/bin
cp ${Root}/usr/src/${Work}/build/xenv/i386osenv ${Tools}/usr/ccs/bin/i386osenv

chmod 755 ${Tools}/usr/ccs/bin/i386osenv

#
#	Make xenv version of Function Relocator
#

cd ${Root}/usr/src/${Work}/cmd/fur
${Tools}/usr/ccs/bin/${Cpu}osenv make -f fur.mk clobber install CMDRULES=${Tools}/etc/xcmd.rulefile

[ -d ${Tools}/usr/ccs/bin ] || mkdir -p ${Tools}/usr/ccs/bin
cp ${Root}/${Mach}/usr/ccs/bin/${Cpu}fur ${Tools}/usr/ccs/bin/${Cpu}fur

#
#	Make xenv libraries
#
echo "$0: Making Cross Environment Libraries"

# We need to build and install the libs into $TOOLS.  However, the makefiles
# are set up to install into $ROOT/$MACH.  Therefore, we set ROOT=$TOOLS and
# MACH='.' into a copy of the library rulefile and pass that in as $OSRULES

ROOT=${Tools}
export ROOT

sed -e "1,\$s/^MACH[ 	]*=.*/MACH = ./" ${Tools}/etc/lib.rulefile >  ${Tools}/etc/xenv.rulefile

cd ${Root}/usr/src/${Work}/cmd/sgs
${Tools}/usr/ccs/bin/${Cpu}env ${Cpu}make -P -f sgs.mk libs \
	CMDRULES=${Root}/usr/src/${Work}/cmd/sgs/xenv/${Cpu}/sgsrules \
	OSRULES=${Tools}/etc/xenv.rulefile

#
#	Make native Function Relocator
#	with ROOT set back to ${Root}
#

cd ${Root}/usr/src/${Work}/cmd/fur
ROOT=${Root} ${Tools}/usr/ccs/bin/${Cpu}osenv make -f fur.mk clobber install

echo "$0: Making Cross xcplibs"

cd ${Root}/usr/src/${Work}/xcplib

for l in libx lxcurses lxtermlib
do
	cd $l
	${Tools}/usr/ccs/bin/${Cpu}env ${Cpu}make -f ${l}.mk install \
		ROOT=${Tools} MACH=.
	cd ..
done

set -e

exit 0
