#!/bin/sh
#
#  UNIX configuration utility for bootrom code.
#
# Make sure to remove all unnecessary files upon program termination
#
trap "rm -f ./utils/pass1 ./utils/pass2 ./utils/config ./compress \
	./mkimage.sh ./mkimage.cfg ./image.bin ./image.bin.Z" 0

#
# You can give the byteorder on the command line if your system doesn't
# support the endian.h include file. If your system is little endian
# (e.g. Intel systems), it should read "little". If your system is big
# endian (e.g. Motorola systems), it should read "big".
#
# However, please notify me (gero@gkminix.han.de) by mail if you really have
# to use this option, and specify what system you are using.
#
ENDIAN=
if [ -n "$1" ]; then
	ENDIAN="$1"
fi

#
# First check if all necessary files are available
#
for i in ./binaries/rom.bin ./binaries/floppy.bin ./binaries/kernel.bin \
		./utils/utilsrc/pass1.c ./utils/utilsrc/pass2.c \
		./utils/utilsrc/config.c
do
	if [ ! -r $i ]; then
		echo "The file $i is missing! Cannot continue."
		exit 1
	fi
done
if [ -z "`type -path compress`" ]; then
	echo "compress program not available (not in path?)! Cannot compress bootrom."
	rm -f compress
	echo "#!/bin/sh" >compress
	echo "mv $1 $1.Z" >>compress
	chmod 755 compress
	PATH=$PATH:.
fi

#
# Next compile all necessary utility programs
#
BYTEORDER=
if [ ! -r /usr/include/endian.h ]; then
	if [ -z "$ENDIAN" ]; then
		echo "Cannot determine if your system is big or little endian."
		echo "Please edit this script (makerom)."
		exit 1
	fi
	if [ "$ENDIAN" = "big" ]; then
		BYTEORDER="-D__BYTE_ORDER=4321"
	else
		BYTEORDER="-D__BYTE_ORDER=1234"
	fi
fi
OPTIONS="-DUNIX $BYTEORDER"
cd ./utils/utilsrc
cc $OPTIONS -o ../pass1 pass1.c
cc $OPTIONS -o ../pass2 pass2.c
cc $OPTIONS -o ../config config.c
cd ../..

#
# Now check if we have a user configuration script
#
if [ $# -gt 1 ]; then
	if [ ! -r $1 ]; then
		echo "User script specified which doesn't exist!"
		exit 1
	fi
	exec sh $1
fi

#
# Run the configuration program and the script it creates
#
./utils/config -s mkimage.sh -c mkimage.cfg
if [ ! -r ./mkimage.sh ]; then
	echo "Abnormal termination of configuration program!"
	exit 1
fi
echo "Running configuration script now."
sh ./mkimage.sh
echo "Configuration script terminated successfully."
echo
echo
echo "You now have two binary files in the current directory:"
echo
echo "        image.flo  -  floppy image of bootrom code"
echo "        image.rom  -  EPROM image of bootrom code"
echo
echo "You might want to write image.flo onto a floppy disk using the dd program. For"
echo "using image.rom see your EPROM burner\'s users manual on how to burn it into an"
echo "EPROM."
exit 0

