# update_kernel: build new virtual console kernels with new drivers
#                to allow the flakiest of keyboards to work with them.


# backup previous /conf/gen directory
if test -d /conf/gen
	then
	echo "The directory /conf/gen contains files which store all"
	echo "kernel patching information used to build your kernel."
	echo "As this process can be destructive to existing patch data,"
	echo "do you wish to save this data to a new directory (y/n)?"

	read I

	case $I in
		y|Y) cpdir -ade /conf/gen /conf/old_patches;;
		n|N) echo "You have been warned!";;
		  *) echo "You have been warned!";;
	esac
fi

# create a new gen directory in /conf of kernel libraries and objects
cp -d kgen /conf/kgen

export NEWDIR=/conf/kerkbd_gen

echo "Building new kernel gen directory."
mkdir $NEWDIR
echo "Copying libraries and objects to new gen directory."
cp /conf/*.[ao] $NEWDIR/


echo "Copying new keyboard files to new kernel gen directory."
cp *.o $NEWDIR/

echo "Building new virtual console libraries... please wait"
echo "\tcopying libraries..."
cp $NEWDIR/vt.a $NEWDIR/vtkb.a
cp $NEWDIR/vt.a $NEWDIR/vtkb_f.a
cp $NEWDIR/vt.a $NEWDIR/vtkb_d.a
echo "\tremoving old keyboard drivers from libraries..."
ar d $NEWDIR/vtkb.a vtnkb.o
ar d $NEWDIR/vtkb_f.a vtnkb.o
ar d $NEWDIR/vtkb_d.a vtnkb.o
echo "\tadding new keyboard drivers to libraries..."
ar rucksv $NEWDIR/vtkb.a vtkb.o
ar rucksv $NEWDIR/vtkb_f.a vtkb_f.o
ar rucksv $NEWDIR/vtkb_d.a vtkb_d.o
echo "New virtual console libraries built."
rm $NEWDIR/vtkb.o
rm $NEWDIR/vtkb_f.o
rm $NEWDIR/vtkb_d.o

echo "Would you like to build a virtual console kernel using:"
echo
echo "\t1) U.S. 101/83-key AT keyboard (not loadable)"
echo "\t2) French 102-key AT keyboard (not loadable)"
echo "\t3) German 102-key AT keyboard (not loadable)"
echo
echo "Please select 1, 2 or 3...\c"

read N

case $N in
	1) echo vtkb.a >/conf/gen/kbd;;
	2) echo vtkb_f.a >/conf/gen/kbd;;
	3) echo vtkb_d.a >/conf/gen/kdb;;
	*) echo "You have made an invalid choice. Please rerun this script.";
	   rm -r $NEWDIR;
	   echo "New config directory deleted. Now exiting.";
	   exit 1;;
esac

echo "Now generating new kernel. Please wait..."
/usr/bin/ksh /conf/kgen /testcoh $NEWDIR
