#!/bin/sh
I=/usr/inet6
PWD=`pwd`
#
SYSTEM=
case "$1"
in
	netbsd) SYSTEM=netbsd ;;
	bsdi) SYSTEM=bsdi ;;
	ucb) SYSTEM=ucb ;;
	*) echo "usage: prepare-kernel [netbsd|bsdi|ucb]"
	   exit 1; ;;
esac
ARCH=`uname -m`
if [ -z "$ARCH" ];
then
	echo "Error: uname -m failed."
	exit 1
fi
case "$ARCH"
in
	i386|sparc) ;;
	*) echo "Warning: $ARCH is not a tested architecture!" ;;
esac
echo " "
echo "NRL IPv6/IPsec Software Distribution"
echo "Alpha Release 3   Kernel Preparation                                July, 1996"
echo "====================================                                =========="
echo " "

echo "This script will create a modified version of your kernel source tree in"
echo "/usr/src/sys that contains our IPv6/IPsec code and will put this tree in"
echo "$I/src/sys."
echo " "
echo "This script does no hand-holding. Errors are not checked. Existing files"
echo "will be overwritten. If it destroys your system, it's your problem."
echo " "
echo -n "Do you wish to do this? (y/n) [n] "
read FOO
case "$FOO"
in
	y*) FOO=yes ;;
	*) FOO=no ;;
esac
if [ "$FOO" = no ];
then
	exit 0
fi
echo " "
echo "Okay, here goes!"
echo " "
if [ ! -d $I ];
then
	echo -n "Creating $I... "
	mkdir $I
	chmod 755 $I
	echo "done"
else
	echo "Using existing $I... "
fi
if [ -d $I/src ];
then
	echo -n "Renaming $I/src to $I/src.old... "
	mv $I/src $I/src.old
	echo done
fi
echo -n "Creating $I/src... "
mkdir $I/src
chmod 755 $I/src
echo "done"
echo -n "Copying your kernel source tree... "
( cd /usr/src ; tar -cf - sys | ( cd $I/src ; tar -xf - ))
echo "done"
echo -n "Symlinking $I/src/sys to $PWD/sys"
ln -s $I/src/sys $PWD
echo "done"
echo -n "Applying patches for the $SYSTEM kernel... "
( cd $I/src/sys ; cat $PWD/diff.$SYSTEM/* | patch -s 2>&1 >>log)
echo "done"
echo -n "Removing your old netinet tree... "
rm -rf $I/src/sys/netinet
echo "done"
echo -n "Copying new kernel source directories... "
( cd sys.common ; tar -cf - * | ( cd $I/src/sys ; tar -xf - ))
echo "done"
FOO=`uname -v | cut -f5 -d:`
DEF=`basename $FOO`
if [ "$DEF" = IPV6 ];
then
	DEF=GENERIC
fi
case $SYSTEM
in
	netbsd) AD=arch/$ARCH/conf ;;
	bsdi|ucb) AD=$ARCH/conf ;;
esac
if [ ! -f $I/src/sys/$AD/$DEF ]
then
	DEF=GENERIC
fi
echo -n "What's the name of your kernel config file? [$DEF] "
read FOO
if [ -z "$FOO" ];
then
	FOO="$DEF"
fi
echo -n "Creating an IPV6 kernel config file from $FOO... "
cp $I/src/sys/$AD/$FOO $I/src/sys/$AD/IPV6
if [ $SYSTEM = netbsd ];
then
	A=
else
	A=\#
fi
if [ $ARCH = i386 ];
then
	B=
	if [ $SYSTEM != bsdi ];
	then
		C=
	else
		C=\#
	fi
	D=\#
else
	B=\#
	C=\#
	D=
fi
cat >> $I/src/sys/$AD/IPV6 << EOF
#
# This file has been slightly modified by NRL.
# See the NRL Copyright notice for conditions on the modifications.
#

# NRL IPv6/IPsec options
${A}options		KERNEL			# NetBSD uses _KERNEL instead
${C}options		DEBUG			# NetBSD/i386 forgot this one

options		KEY			# Key Engine
options		KEY_DEBUG		#    ... debugging
options		IPSEC			# IP Security
options		IPSEC_DEBUG		#    ... debugging
options		INET6			# IPv6
options		INET6_DEBUG		#    ... debugging

${B}options 	DES386			# Use fast 386 assembly DES code
${C}options		BSWAP			#    ... use 486+ bswap instruction
${D}options		DESPORT		# Use (slower) portable C DES code
# end NRL IPv6/IPsec options
EOF
echo "done"
echo -n "Running config(8) on the new config file... "
( cd $I/src/sys/$AD ; config IPV6 >/dev/null)
echo "done"
echo " "
echo "You should now be able to build a kernel in the object directory"
case $SYSTEM
in
	netbsd) echo "$I/src/sys/arch/`uname -m`/compile/IPV6." ;;
	bsdi|ucb) echo "$I/src/sys/compile/IPV6." ;;
esac
exit 0
