#!/bin/csh -f
#
# This csh script configures the uSystem software
# after it has been distributed.  It is called from either
# the INSTALL command when a user is manually installing the
# uSystem, or from the RDIST command when a user is using the
# remote distribution facilities to install the software.
#
# usage: CONFIGURE vendor-name os-name cpu-name page-format
#	 eg. CONFIGURE sequent bsd43 i386 imperial

# The following arguments are passed on the command line.

set vendor = $1			# vendor name
set os = $2			# operating system name
set cpu = $3			# cpu name
set pageformat = $4		# page format name
set dmos = $5			# dmake operating system name

# The following variables specify where the compiled objects
# will be placed.

set dir = `pwd`			# the home directory
set bin = ${dir}/bin		# the bin directory
set dmake = ${bin}/dmake	# the dmake program

# Set the file creation mask, so that files
# are not writable by others.  You may wish to change this.

umask 002

# If necessary, create the local bin directory.

if ( ! -e ${bin} ) mkdir ${bin}

# Build the dmake program.

echo "*****"
echo "Building dmake for a ${vendor}-${os}-${cpu}-${pageformat}-${dmos} system."
echo "*****"

cd dmake
foreach file ( `find . \( -name startup.h \) -print` )
	sed 's^\"MAKESTARTUP := .*^\"MAKESTARTUP := '${dir}/dmake/startup.mk\",'^' ${file} > /tmp/rdist$$
	cmp -s ${file} /tmp/rdist$$
	if ( ${status} ) then
		echo "-- Setting hard coded variables in file "${file}"."
		rm ${file}
		mv /tmp/rdist$$ ${file}
	else
		rm /tmp/rdist$$
	endif
end
switch ( $dmos:q )
	case bsd43:
		cp unix/bsd43/startup.mk .
		breaksw
	case bsd43uw:
		cp unix/bsd43/uw/startup.mk .
		breaksw
	case bsd43vf:
		cp unix/bsd43/vf/startup.mk .
		breaksw
	case sysvr3:
		cp unix/sysvr3/startup.mk .
		breaksw
endsw
if ( -x ${dmake} ) then
	setenv OS unix
	switch ( $dmos:q )
		case bsd43:
			setenv OSRELEASE bsd43
			breaksw
		case bsd43uw:
			setenv OSRELEASE bsd43
			setenv OSENVIRONMENT uw
			breaksw
		case bsd43vf:
			setenv OSRELEASE bsd43
			setenv OSENVIRONMENT vf
			breaksw
		case sysvr3:
			setenv OSRELEASE sysvr3
			breaksw
	endsw
	${dmake}
else
	rm -f -r objects
	make ${dmos}
endif
if ( ${status} == 0 ) then
	echo "-- Installing dmake"
	cp dmake ${dmake}
endif
cd ..

# Build the uSystem.

if ( ${vendor} == mips ) then				# expecting bsd commands
	setenv PATH "/bsd43/bin/:"${PATH}
endif

echo "*****"
echo "Building the uSystem for a ${vendor}-${os}-${cpu}-${pageformat}-${dmos} system."
echo "*****"

# If we are building on certain types of machines, we must
# create links to the stock assemblers and loaders because
# the GNU C compiler on those machines produces code that the
# GNU assembler and GNU loader cannot understand.  Yes, that's
# right.  We hope this will be fixed in the future.

pushd gnu
if ( ! -e bin ) mkdir bin
if ( ${cpu} == "i386" || ${cpu} == "ns32k" ) then
	if ( ! -e bin/as ) ln -s /bin/as bin/.
	if ( ! -e bin/ld ) ln -s /bin/ld bin/.
endif
popd
	
foreach file ( `find . \( -name Makefile \) -print` )
	sed 's^HOME = .*^HOME = '${dir}'^;s^VENDOR = .*^VENDOR = '${vendor}'^;s^OS = .*^OS = '${os}'^;s^CPU = .*^CPU = '${cpu}'^' ${file} > /tmp/rdist$$
	cmp -s ${file} /tmp/rdist$$
	if ( ${status} ) then
		echo "-- Setting hard coded variables in file "${file}"."
		rm ${file}
		mv /tmp/rdist$$ ${file}
	else
		rm /tmp/rdist$$
	endif
end

#
# Differentiating between Ultrix 3 and 4.
#

if ( ${vendor} == dec && ${os} == bsd43 ) then
	if ( -x /bin/uname ) then 
		if ( "`uname -r`" =~ 4* ) then
			# we are running Ultrix 4
			sed '/uUltrixStupidity/s^undef^define^' inc/uUnix.h > /tmp/rdist$$
		else
			# we are running Ultrix 3
			sed '/uUltrixStupidity/s^define^undef^' inc/uUnix.h > /tmp/rdist$$
		endif
	else
		# we are not running Ultrix 4
		sed '/uUltrixStupidity/s^define^undef^' inc/uUnix.h > /tmp/rdist$$
	endif
	rm inc/uUnix.h
	mv /tmp/rdist$$ inc/uUnix.h
endif

echo "-- Setting the page format for TeX formatting"
pushd TeX
if ( ${pageformat} == "metric" ) then
	cp metric fullpage.sty
else
	cp imperial fullpage.sty
endif
popd

echo "-- Compiling the uSystem library functions."
pushd src
${dmake} all
popd

echo "*****"
echo "Building the uKernel and uLibrary"
echo "*****"

echo "-- Installing the ${vendor}-${os}-${cpu} system dependent kernel files."
pushd kernel/work/src
if ( -e uMachine.i ) then
	cmp -s configure/cd-${vendor}-${os}-${cpu}.i uMachine.i
	if ( ${status} ) then
		cp configure/cd-${vendor}-${os}-${cpu}.i uMachine.i
		echo '-- Installing '`pwd`'/uMachine.i'
	endif
else
	cp configure/cd-${vendor}-${os}-${cpu}.i uMachine.i
	echo '-- Installing '`pwd`'/uMachine.i'
endif
cd ../inc
if ( -e uMachine.h ) then
	cmp -s configure/cd-${vendor}-${cpu}.h uMachine.h
	if ( ${status} ) then
		cp configure/cd-${vendor}-${cpu}.h uMachine.h
		echo '-- Installing '`pwd`'/uMachine.h'
	endif
else
	cp configure/cd-${vendor}-${cpu}.h uMachine.h
	echo '-- Installing '`pwd`'/uMachine.h'
endif
popd

echo "-- Compiling the work version of the kernel."
pushd kernel/work/com
${dmake} all
cd ../src
${dmake} all
popd

echo "-- Compiling the work version of the library."
pushd library/work/src
${dmake} all
popd

echo "-- Testing the work version of kernel and library."
pushd examples/Benchmark
${dmake} all
popd

echo "-- Installing the release version of the kernel."
pushd kernel
if ( -e release ) rm -r release
mkdir release
cp -r work/inc release/.
cp -r work/lib release/.
cd release/lib
if ( ${os} == "bsd43" ) ranlib *.a	# ranlib only under BSD 4.3
popd

echo "-- Installing the release version of the library."
pushd library
if ( -e release ) rm -r release
mkdir release
cp -r work/inc release/.
cp -r work/lib release/.
cd release/lib
if ( ${os} == "bsd43" ) ranlib *.a
popd

# Build the uMonitor.

echo "*****"
echo "Building the uMonitor"
echo "*****"

echo "-- Compiling the work version of the monitor."
pushd monitor/work/com
${dmake} all
cd ../src
${dmake} all
popd

echo "-- Testing the work version of the monitor."
pushd examples/Benchmark/Monitor
${dmake} all
popd

echo "-- Installing the release version of the monitor."
pushd monitor
if ( -e release ) rm -r release
mkdir release
mkdir release/bin
cp -r work/bin/cpp release/bin/.
cp -r work/bin/mon-cpp release/bin/.
cp -r work/inc release/.
popd

echo "*****"
echo "Done."
echo "*****"
