
#ident	"@(#)adm::mk	1.5scp"
#
#	Shell script for rebuilding the UNIX System
#
#-----------------------------------------------------------#

#  Check that the correct (ctp) compiler is installed

if ns32000 ; then
	CC_SIZE=`ls -l /bin/cc 2>/dev/null | awk '{ print $5 }'`
	PCC_SIZE=`ls -l /bin/cc.pcc 2>/dev/null | awk '{ print $5 }'`
	CTP_SIZE=`ls -l /bin/cc.ctp 2>/dev/null | awk '{ print $5 }'`
	if [ "$CC_SIZE" = "$PCC_SIZE" ]; then
		echo "\n\007The pcc compiler is installed as /bin/cc." > /dev/tty
		echo "The ctp compiler should be installed.\n" > /dev/tty
		exit 1
	elif [ "$CC_SIZE" != "$CTP_SIZE" ]; then
		echo "\n\007Check the file /bin/cc." > /dev/tty
		echo "It doesn't match /bin/cc.pcc or /bin/cc.ctp." > /dev/tty
		exit 1
	fi
fi

#-----------------------------------------------------------#

#  Set CROSS variable
if [ ! -f /bin/CROSS ]; then
	CROSS=FALSE; export CROSS
elif CROSS ; then
	CROSS=TRUE; export CROSS
else
	CROSS=FALSE; export CROSS
fi

#-----------------------------------------------------------#

#  Set PATH

if [ "$ROOT" = "" ]
then
	PATH=/bin:/etc:/usr/bin; export PATH
fi

#-----------------------------------------------------------#

#  Report start time

date

#-----------------------------------------------------------#

#  Clobber everything to insure a clean start

echo "\n========$0  Clobbering source tree\n"
./:clobber > /dev/null 2>&1

#-----------------------------------------------------------#

#  Make /bin/ns32332 true in case this machine is
#  a 32032 machine.

if ns32000; then
	rm /bin/ns32332
	cp /bin/true /bin/ns32332
fi

#-----------------------------------------------------------#
# set up sgs
echo "\n========$0  Setting up sgs.\n"
OPATH=$PATH
PATH=.:$PATH
if clipper ; then
	PROC=clipper
elif ns32000 ; then
	PROC=ns32000
elif m88k ; then
	PROC=m88k
fi
cd /usr/src/cmd/sgs/xenv/$PROC
if [ $CROSS = TRUE ]; then
	SGS=m; export SGS
	OROOT=$ROOT
	ROOT=""; export ROOT
sh pathedit <<!
$PROC
n
n
n
/bin
/lib
/usr/lib
/usr/minclude
$SGS
bin
bin
y
!
	else
sh pathedit <<!
$PROC
y
y
n
y
!
fi
PATH=$OPATH
cd /usr/src

#-----------------------------------------------------------#

#	install /usr/include and /usr/include/sys header files

echo "\n========$0  Installing the header files.\n"
./:mkhead
echo "\n========$0  Installing the sys header files.\n"
./:mksyshead

#-----------------------------------------------------------#

#  Build and install the C safe library,
#  set up for GreenHills compile

if clipper; then 
	echo "\n========$0  Building the C safe library.\n"
	rm /bin/ghs
	mv /bin/cc /bin/ghcc
	./:mklib libc_stk libgen
	cp /bin/true /bin/ghs
	mv /bin/ghcc /bin/cc
fi

#-----------------------------------------------------------#

#	build and install the C library

if [ $CROSS = FALSE ]; then
	echo "\n========$0  Building the C library.\n"
	if m88k; then
		./:mklib libc_m88k
	else
		./:mklib libc_stk
	fi
fi

#-----------------------------------------------------------#

#	build the C compilation tools

echo "\nBuilding SGS.\n"
SGSRETVAL=1
./:mkcmd sgs 
SGSRETVAL=$?
if [ $CROSS = TRUE ]; then
	if [ $SGSRETVAL != 0 ] ; then cp /bin/mmake /bin/make; fi
	ROOT=$OROOT; export ROOT
fi

#-----------------------------------------------------------#

#	build the ethernet libraries (Opus only)

if [ $CROSS = FALSE ]; then
	if clipper || ns32000 ; then
		echo "\n========$0  Building the nfs libraries.\n" 
		cd local/nfs; ./:mklib; cd ../..
		echo "\n========$0  Building the exos libraries.\n" 
		cd local/exos; ./:mklib; cd ../..
	fi
	if m88k ; then
		if [ -d local/bsd ] ; then
			echo "\n========$0  Building the bsd libraries.\n" 
			cd local/bsd; ./:mklib; cd ../..
		fi
	fi
fi

#-----------------------------------------------------------#

#  Build the other libraries

echo "========$0  Building the other libraries.\n"
./:mklib \*

#-----------------------------------------------------------#

#  Copy m88k libraries to the root file system

if [ $CROSS = TRUE ]; then
	LIBLIST="lib/libPW.a lib/libc.a /lib/libm.a usr/lib/libcurses.a usr/lib/libgen.a usr/lib/libl.a lib/libld.a"
	echo "========$0  Copying m88k libraries to the root file system.\n"
	LIBLIST="lib/libPW.a lib/libc.a usr/lib/libcurses.a usr/lib/libgen.a lib/libm.a lib/libld.a"
	echo "========$0  Copying m88k libraries to the root file system.\n"
	for LIB in $LIBLIST; do
		BASENAME=`basename $LIB`
		DIRNAME=`dirname $LIB`
		echo "cp $ROOT/$LIB /$DIRNAME/$SGS$BASENAME"
		cp $ROOT/$LIB /$DIRNAME/$SGS$BASENAME
	done
fi

#-----------------------------------------------------------#

# reset sgs parameters
echo "\n========$0  Resetting up sgs.\n"
OPATH=$PATH
PATH=.:$PATH
if clipper ; then
	PROC=clipper
elif ns32000 ; then
	PROC=ns32000
elif m88k ; then
	PROC=m88k
fi
cd /usr/src/cmd/sgs/xenv/$PROC
if [ $CROSS = TRUE ]; then
	SGS=""; export SGS
sh pathedit <<!
$PROC
n
n
n
/bin
/lib
/usr/lib
/usr/include

bin
bin
y
!
	else
sh pathedit <<!
$PROC
y
y
n
y
!
fi
PATH=$OPATH
cd /usr/src

#-----------------------------------------------------------#

#  Build the system administration tools

echo "\n========$0  Building the system administration tools.\n"
./:mkcmd .adm

#-----------------------------------------------------------#

#  Build .cmd

echo "\n========$0  Building .cmd.\n"
./:mkcmd .cmd

#-----------------------------------------------------------#

#  Build all the commands

echo "\n========$0:  Building all the other commands.\n"
./:mkcmd $* \*

#-----------------------------------------------------------#

#	build stand-alone system, if appropriate

#echo "\n========$0:  Building the stand alone system.\n"
#./:mkstand

#-----------------------------------------------------------#

#  Build the OS itself

if ns32000; then
	trap "cp /bin/cc.ctp /bin/cc; sync; exit" 1 2 3 15
fi
echo "\n========$0  Making a kernel.\n"
if ns32000; then
	cp /bin/cc.pcc /bin/cc
fi
if m88k ; then
	cd /usr/src/uts/m88k/cf/m88k
	make
	#make install
elif clipper ; then
	cd /usr/src/uts/clipper
	make install
elif ns32332 ; then
	cd /usr/src/uts/ns32000
	./makeunix pm200 install
else
	cd /usr/src/uts/ns32000
	./makeunix pm100 install
fi
if ns32000; then
	cp /bin/cc.ctp /bin/cc
	trap 1 2 3 15
fi

#-----------------------------------------------------------#

#  For clipper, remake safe libs and cc using pcc

if clipper ; then
	echo "\n========$0  Remaking safe libs and cc (clipper).\n"
	rm -f /bin/ghs
	mv /bin/cc /bin/ghcc
	cd /usr/src/cmd/sgs/cmd/clipper; make cc install SGS= BINDIR=/bin
	cd /usr/src/cmd/sgs/cmd/clipper; make shrink SGS=
	cd /usr/src; ./:mklib \*
	mv /bin/ghcc /bin/cc
fi

#-----------------------------------------------------------#

#  Make the 032 specific files

#  If 032/332, then append machine type to machine-dependent
#  filenames.  This is required because there is a common
#  release for these processors.
#  The sysadm installpkg script removes the extension and
#  installs the proper filename during installation.

if ns32332 ; then
	cd /usr/src
	echo "\n========$0  Makeing the 032 specific files.\n"
	
	LIST=`grep '.332' /usr/src/local/release/master.list | grep -v '^:' | awk '{ print $1 }' | sed 's/\.332$//'`
	for i in $LIST
	do
		rm $i.032 > /dev/null 2>&1
		ln $i $i.332
	done
	./:mk032
	for i in $LIST
	do
		rm $i
		cp $i.332 $i
	done
fi

# report finish time
echo "\n========$0  DONE.\n"
date
exit 0
