#!/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

# 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 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 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} system."
echo "*****"

# When building on certain types of machines, links to
# the stock assemblers and loaders must be created because
# the GNU C compiler on those machines produces code that the
# GNU assembler and GNU loader cannot understand. 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.
#
# We use the existance and result of "uname" to determine which Ultrix
# version is running.  It seems that uname (or kernel?) is broken for
# 4.0.  It returns 0.0.  Some sites may have this fixed, in which case
# it would return 4.0.  4.0 is equivalent to 4.1 for our purposes.
# Lack of the uname command implies an Ultrix version < 4, which is
# equivalent to 3.?  for our purposes.
#

if ( ${vendor} == dec && ${os} == bsd43 ) then
	if ( -x /bin/uname ) then 
		switch( "`uname -r`" )
			case 4*:
				# Running Ultrix 4.1 or greater (or 4.0 with uname fixed)
				sed '/uUltrixStupidity/s^undef^define^' inc/uUnix.h > /tmp/rdist$$
				breaksw
			case 0.0:
				# Ultrix 4.0 bug... uname -r doesn't work properly
				sed '/uUltrixStupidity/s^undef^define^' inc/uUnix.h > /tmp/rdist$$
				breaksw
			default:
				# Running Ultrix 3 or less (we think)
				sed '/uUltrixStupidity/s^define^undef^' inc/uUnix.h > /tmp/rdist$$
		endsw
	else
		# 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 "*****"
