#sccs	"@(#)tools:mkenv	1.2"

# :mkenv - do iterations of environment builds
# Depends on the routines mksgs and cmp_lib.

date
cd $SRC

if [ ! "$1" ] 
then
	ITERS="1 2 3"
else
	ITERS="$*"
fi

if [ ! -d $ROOT/lib.back ] 
then
	mkdir $ROOT/lib.back
else
	rm -rf $ROOT/lib.back
	mkdir $ROOT/lib.back
fi

i=0 # Zeroth iteration of build, ie save libs.
if [ ! -d $ROOT/lib.back/$i ] 
then
	mkdir $ROOT/lib.back/$i
fi
if [ ! -d $ROOT/lib.back/$i/lib ] 
then
	mkdir $ROOT/lib.back/$i/lib
fi
if [ ! -d $ROOT/lib.back/$i/bin ] 
then
	mkdir $ROOT/lib.back/$i/bin
fi

#____________________________________________________________________________
#  Save the original set of sgs tools and libraries.

cp /lib/lib[cm].a /lib/shlib* /usr/lib/libtam.a $ROOT/lib.back/$i/lib
cd /bin
cp ar make cc ld as file strip size dump nm $ROOT/lib.back/$i/bin

cd /lib
cp cpp ccom optim $ROOT/lib.back/$i/lib
echo "original sgs tools saved in $ROOT/lib.back/$i"
cd $SRC

#____________________________________________________________________________
#  Begin process of making and saving the new binaries.

cd $SRC/HEADERS; /bin/make
for i in $ITERS
do
	echo $i > $SRC/iteration
	if [ ! -d $ROOT/lib.back/$i ] 
	then
		mkdir $ROOT/lib.back/$i
	fi
	if [ ! -d $ROOT/lib.back/$i/lib ] 
	then
		mkdir $ROOT/lib.back/$i/lib
	fi

	cd $SRC/LIB
	/bin/make -i clobber includes all install || ( echo "SGS BUILD FAILED, iteration $i."; exit 9 )
	#make -i all install || ( echo "SGS BUILD FAILED, iteration $i."; exit 9 )
	if [ $? = 9 ]
	then
		exit 9
	fi
	cp /lib/lib[cm].a /lib/shlib* /usr/lib/libtam.a $ROOT/lib.back/$i/lib

	cd $SRC
	#sh -x /usr/local/bin/mksgs all || ( echo "SGS BUILD FAILED, iteration $i."; exit 9 )
	sh -x /usr/local/bin/mksgs clobber includes all || ( echo "SGS BUILD FAILED, iteration $i."; exit 9 )
	if [ $? = 9 ]
	then
		exit 9
	fi

	rm -f $SRC/iteration
done

#____________________________________________________________________________
# Now begin comparing the final binaries.

cd $ROOT/lib.back

for I in libtam.a libc.a libm.a
do
	/usr/local/bin/cmp_lib 2/lib/$I 3/lib/$I 
	if [ $? != 0 ]
	then
		echo "$I did not compare."
	else
		echo "$I compare SUCCESSFUL."
	fi
done
diff 2/lib/shlib.ifile 3/lib/shlib.ifile > shlib.$$
if [ -s shlib.$$ ]
then
	echo "shlib.ifile did not compare."
else
	echo "shlib.ifile compare SUCCESSFUL."
fi
rm -f shlib.$$
for I in shlib cpp ccom optim
do
	cmp -l 2/lib/$I 3/lib/$I | awk '{ print $1 }' | grep [012349] > cmp.$$
	if [ -s cmp.$$ ]
	then
		echo "$I did not compare."
	else
		echo "$I compare SUCCESSFUL."
	fi
	rm -f cmp.$$
done
for I in ar make cc ld as file strip size dump nm
do
	cmp -l 2/bin/$I 3/bin/$I | awk '{ print $1 }' | grep [012349] > cmp.$$
	if [ -s cmp.$$ ]
	then
		echo "$I did not compare."
	else
		echo "$I compare SUCCESSFUL."
	fi
	rm -f cmp.$$
done

# Clean up old makes - currently running one should be saved
rm -f /bin/oldmake*

echo END OF SGS TOOLSET BUILD!!!!
date
