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

# setup the paths for debian
if [ -d /usr/lib/netboot/binaries ]; then
    NETBOOTBINARYPATH=/usr/lib/netboot/binaries
else
    NETBOOTBINARYPATH=./binaries
fi
if [ -d /usr/lib/netboot/binaries ]; then
    # Debian with compiled binaries in
    NETBOOTUTILSPATH=/usr/lib/netboot/utils
else
    NETBOOTUTILSPATH=./utils

    #
    # First check if all necessary (util) files are available
    #
    for i in $NETBOOTUTILSPATH/utilsrc/pass1.c $NETBOOTUTILSPATH/utilsrc/pass2.c \
	     $NETBOOTUTILSPATH/utilsrc/config.c
    do
	    if [ ! -r $i ]; then
		    echo "The file $i is missing! Cannot continue."
		    exit 1
	    fi
    done
    #
    # 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 ../..

fi

#
# 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 (binary) files are available
#
for i in $NETBOOTBINARYPATH/rom.bin $NETBOOTBINARYPATH/floppy.bin $NETBOOTBINARYPATH/kernel.bin 
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 $2 $2.Z' >>compress
	chmod 755 compress
	PATH=$PATH:.
fi

#
# 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
#
$NETBOOTUTILSPATH/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

