# kgen version 1.0.0
#
# Copyright 1992, Mark Williams Company
#
# Link and patch a kernel with configuration used when COHERENT was installed.
#
# Usage:  kgen new-kernel-name

TARGET=${1-/testcoh}
KDIR=${2-/conf}

if echo $TARGET | grep -v "/" > /dev/null
then
	TARGET="/"$TARGET
fi

if echo $TARGET | grep "/.*/" > /dev/null
then
	echo "New kernel must be in root directory in order to boot."
	echo "$0 aborted."
	exit 1
fi

if [ -f $TARGET ]
then
	echo -n "File $TARGET already exists.  Overwrite?  "
	read ANS
	if [ ${ANS} != y ]
	then
		echo "$0 aborted."
		exit 1
	fi
fi

#-------------------------------------
echo "Fetching configuration files..."

# Fetch patch file created during installation.
sed -e 's/\/mnt\/coherent/$TARGET/' < /conf/gen/patches > /tmp/patches || exit 1
chmog 555 bin bin /tmp/patches

PATCH=
PATCH="${PATCH} drvl+60=lpcon"
PATCH="${PATCH} drvl+100=asycon"
PATCH="${PATCH} drvl+180=ptycon"
PATCH="${PATCH} drvl+460=semcon"
PATCH="${PATCH} drvl+480=shmcon"
PATCH="${PATCH} drvl+500=msgcon"

UNDEF=
UNDEF="$UNDEF -u lpcon"
UNDEF="$UNDEF -u flcon"
UNDEF="$UNDEF -u asycon"
UNDEF="$UNDEF -u ptycon"
UNDEF="$UNDEF -u semcon"
UNDEF="$UNDEF -u shmcon"
UNDEF="$UNDEF -u msgcon"

. /conf/gen/ldker || exit 1

KB=`cat /conf/gen/kbd` || exit 1


#-------------------------------------
echo "Linking Target File..."

cd $KDIR
/bin/ld $UNDEF $HDUNDEF -K -o $TARGET -e stext ker386.o $KB kl386.a || exit 1

#-------------------------------------
echo "Patching Target File..."

/conf/patch $TARGET $PATCH $HDPATCH				    || exit 1
/conf/asypatch -v $TARGET < /etc/default/async || exit 1

export TARGET
/tmp/patches || exit 1

/bin/chmog 400 sys sys $TARGET

echo "\nNew kernel is $TARGET.\n"
echo "To test this kernel:"
echo "  /etc/shutdown to single-user mode"
echo "  sync"
echo "  Press reset."
echo "  During reboot, press SPACE at the prompt to abort default boot."
echo "  Type `basename $TARGET` at the question mark and press ENTER.\n"
echo "To make the new kernel the default start-up kernel:"
echo "  ln -f $TARGET /autoboot\n"
echo -n "Press ENTER to continue."
read JUNK
