#! /bin/sh
#
#	2.10BSD script to set up a new kernel configuration directory.

LO=../h/localopts.h

if [ $# -ne 1 ]; then
	echo "usage: $0 machinename"
	exit 1
fi

MACHINE=$1
if [ ! -s $MACHINE ]; then
	echo "$0: unable to read $MACHINE."
	exit 1
fi

if mkdir ../$MACHINE; then
	echo "Creating ../$MACHINE."
	chmod 775 ../$MACHINE
else
	exit 1
fi

# read in specification file
eval `awk '/^#/ || /^$/ || /^	/ {next} $1 != "" { printf "%s=\\"%s\\"\\n", $1, $2 }' $MACHINE`

if [ ! "$MAXUSERS" ]; then
	echo "$0: maxusers not set in $MACHINE."
	exit 1
fi

if [ ! "$TIMEZONE" ]; then
	echo "$0: timezone not set in $MACHINE."
	exit 1
fi

if [ ! "$DST" ]; then
	echo "$0: dst flag not set in $MACHINE."
	exit 1
fi

cat << EOF > $LO
/*
 * This file is generated by the configuration script sys/conf/config.
 *
 * It contains a list of kernel options, used when compiling the kernel
 * as well as in a few of the application programs.
 *
 * All defines previously found in the whoami.h file have been moved
 * into this file, as well as several originally found in device header
 * files.  Do not hand edit unless you know what you're doing.  (You
 * probably shouldn't hand edit anyway...)  See sys/conf/GENERIC for a
 * list of available options and explanation as to what an option does,
 * and sys/conf/README for more information.
 */

EOF

case $PDP11 in
	23|24|34)
		KERN_NONSEP=YES
		SPLFIX=:splfix.mtps;;
	GENERIC)
#		if [ $NONFP = NO ]; then
#			echo "NONFP changed; should be set for GENERIC kernels."
#			NONFP=YES
#		fi
#		eventually: KERN_NONSEP=YES
		KERN_NONSEP=NO
		SPLFIX=:splfix.movb;;
	35|40|60)
		KERN_NONSEP=YES
		SPLFIX=:splfix.movb;;
	53|73|83)
		KERN_NONSEP=NO
		SPLFIX=:splfix.mtps;;
	44|45|50|55|70|84)
		KERN_NONSEP=NO
		SPLFIX=:splfix.spl;;
	*)
		echo "$0: unrecognized PDP11 type $PDP11."
		echo 'use GENERIC, 23, 24, 34, 35, 40, 44, 45, 50, 53, 55,'
		echo '	60, 70, 73, 83, 84.'
		exit 1;;
esac

if [ $NKL -lt 1 ]; then
	echo "$0: NKL must be at least one (for the console)."
	exit 1
fi

# Copy in the standard configuration files, boot program,
# makefiles, and cpu-dependent files.  Modify as necessary.

if [ $MACHINE = VAX ]; then
	echo "Copying standard VAX files to ../$MACHINE."
	cp VAX.compile/VAX.c VAX.compile/Makefile ../$MACHINE
else
	echo "Copying standard files to ../$MACHINE."
	if [ $NEC = 0 ]; then
		if [ $UCB_NET = YES ]; then
			cp spl/:splfix.movb ../$MACHINE/NETSPLFIX
		fi
		cp spl/$SPLFIX ../$MACHINE/SPLFIX
	else
		if [ $UCB_NET = YES ]; then
			cp spl_3com/:splfix.movb ../$MACHINE/NETSPLFIX
		fi
		cp spl_3com/$SPLFIX ../$MACHINE/SPLFIX
	fi
	if [ $UCB_NET = YES ]; then
		cp Make.nsunix ../$MACHINE/Makefile
		cp Make.sys Make.net Make.pdpnet Make.pdpuba Make.pdp \
			Make.pdpmba ../$MACHINE
	elif [ $KERN_NONSEP = NO ]; then
		cp Make.sunix ../$MACHINE/Makefile
		cp Make.sys Make.pdpuba Make.pdp Make.pdpmba ../$MACHINE
	else
		cp Make.unix ../$MACHINE/Makefile
		cp Make.sys Make.pdpuba Make.pdp Make.pdpmba ../$MACHINE
	fi
	cp boot/${BOOTDEV}boot.s ../$MACHINE/boot.s
fi

sed	-e "s/%ROOTDEV%/$ROOTDEV/" \
	-e "s/%SWAPDEV%/$SWAPDEV/" \
	-e "s/%PIPEDEV%/$PIPEDEV/" \
	-e "s/%NSWAP%/$NSWAP/" \
	-e "s/%SWAPLO%/$SWAPLO/" \
	-e "s/%DUMPDEV%/$DUMPDEV/" \
	-e "s/%DUMPLO%/$DUMPLO/" \
	-e "s/%DUMPROUTINE%/$DUMPROUTINE/" < ioconf.c > ../$MACHINE/ioconf.c

sed	-e "s/%TIMEZONE%/$TIMEZONE/" \
	-e "s/%DST%/$DST/" \
	-e "s/%MAXUSERS%/$MAXUSERS/" < param.c > ../$MACHINE/param.c

echo "Setting configuration options for $MACHINE."

if [ $IDENT != GENERIC ]; then
	echo "#define $IDENT"		>> $LO
fi
echo "#define PDP11	$PDP11"		>> $LO
echo "#define MAXMEM	$MAXMEM"	>> $LO
echo "#define LINEHZ	$LINEHZ"	>> $LO
if [ $NONFP = YES -a $FPSIM = NO ]; then
	echo NONFP is YES and FPSIM is NO.  You have to provide some form of
	echo floating point support!!!!
	exit 1
fi
if [ $NONFP = YES ]; then
	echo "#define NONFP 1"		>> $LO
fi
if [ $FPSIM = YES ]; then
	echo "#define FPSIM 1"		>> $LO
fi
echo "#ifdef KERNEL"			>> $LO
echo "#define NBUF	$NBUF"		>> $LO

if [ $KERN_NONSEP = YES ]; then
	echo "#define KERN_NONSEP"	>> $LO
fi

if [ $UNIBUS_MAP = YES ]; then
	echo "#define UNIBUS_MAP 1"	>> $LO
fi
if [ $Q22 = YES ]; then
	echo "#define Q22 1"		>> $LO
fi
if [ $VIRUS_VFORK = YES ]; then
	echo "#define VIRUS_VFORK 1"	>> $LO
fi
if [ $EXTERNALITIMES = YES ]; then
	echo "#define EXTERNALITIMES 1"	>> $LO
fi
if [ $CGL_RTP = YES ]; then
	echo "#define CGL_RTP 1"	>> $LO
fi
if [ $UCB_METER = YES ]; then
	echo "#define UCB_METER 1"	>> $LO
fi
if [ $UCB_RUSAGE = YES ]; then
	echo "#define UCB_RUSAGE 1"	>> $LO
fi
if [ $NOKA5 = YES ]; then
	echo "#define NOKA5 1"		>> $LO
fi
if [ $UCB_NET = YES ]; then
	echo "#define UCB_NET 1"	>> $LO
	echo "#define INET 1"		>> $LO
	echo "#define TCP_COMPAT_42"	>> $LO
	echo "#define NLOOP	1"	> ../$MACHINE/loop.h
else
	echo "#define NLOOP	0"	> ../$MACHINE/loop.h
fi
if [ $BADSECT = YES ]; then
	echo "#define BADSECT 1"	>> $LO
fi
if [ $CHECKSTACK = YES ]; then
	echo "#define CHECKSTACK 1"	>> $LO
fi
if [ $DIAGNOSTIC = YES ]; then
	echo "#define DIAGNOSTIC 1"	>> $LO
	echo "#define ICMPPRINTFS"	>> $LO
fi
if [ $QUOTA = YES ]; then
	echo "#define QUOTA 1"		>> $LO
fi
if [ $UCB_FRCSWAP = YES ]; then
	echo "#define UCB_FRCSWAP 1"	>> $LO
fi
if [ $UCB_CLIST = YES ]; then
	echo "#define UCB_CLIST 1"	>> $LO
fi
if [ $SMALL = YES ]; then
	echo "#define SMALL 1"		>> $LO
fi
if [ $PROFILE = YES ]; then
	echo "#define PROFILE 1"	>> $LO
fi
echo "#endif /* KERNEL */"		>> $LO

echo "Creating device header files."

echo "#define NCSS	$NCSS"		> ../$MACHINE/css.h
echo "#define NDE	$NDE"		> ../$MACHINE/de.h
echo "#define NDH	$NDH"		> ../$MACHINE/dh.h
echo "#define NDM	$NDM"		>> ../$MACHINE/dh.h
echo "#define NDN	$NDN"		> ../$MACHINE/dn.h
echo "#define NDHU	$NDHU"		> ../$MACHINE/dhu.h
echo "#define NDMC	$NDMC"		> ../$MACHINE/dmc.h
echo "#define NDR	$NDR"		> ../$MACHINE/dr.h
echo "#define NDZ	$NDZ"		> ../$MACHINE/dz.h
echo "#define NEC	$NEC"		> ../$MACHINE/ec.h
echo "#define NEN	$NEN"		> ../$MACHINE/en.h
echo "#define NETHER	$NETHER"	> ../$MACHINE/ether.h
echo "#define NHT	$NHT"		> ../$MACHINE/ht.h
echo "#define NHY	$NHY"		> ../$MACHINE/hy.h
echo "#define NIL	$NIL"		> ../$MACHINE/il.h
echo "#define NIMP	$NIMP"		> ../$MACHINE/imp.h
echo "#define NKL	$NKL"		> ../$MACHINE/cn.h
echo "#define NLP	$NLP"		> ../$MACHINE/lp.h
echo "#define LP_MAXCOL	$LP_MAXCOL"	>> ../$MACHINE/lp.h
echo "#define NPTY	$NPTY"		> ../$MACHINE/pty.h
echo "#define NPUP	$NPUP"		> ../$MACHINE/npup.h
echo "#define NQE	$NQE"		> ../$MACHINE/qe.h
echo "#define NRAM	$NRAM"		> ../$MACHINE/ram.h
echo "#define NRK	$NRK"		> ../$MACHINE/rk.h
echo "#define NRX	$NRX"		> ../$MACHINE/rx.h
echo "#define NSL	$NSL"		> ../$MACHINE/sl.h
echo "#define NSRI	$NSRI"		> ../$MACHINE/sri.h
echo "#define NTB	$NTB"		> ../$MACHINE/tb.h
echo "#define NTS	$NTS"		> ../$MACHINE/ts.h
echo "#define NVV	$NVV"		> ../$MACHINE/vv.h

echo "#define NACC	$NACC"		> ../$MACHINE/acc.h
if [ $PLI = YES ]; then
	echo "#define PLI 1"		>> ../$MACHINE/acc.h
fi

echo "#define NHK	$NHK"		> ../$MACHINE/hk.h
if [ $DUMPROUTINE = hkdump ]; then
	echo "#define HK_DUMP 1"	>> ../$MACHINE/hk.h
fi

echo "#define NRAC	$NRAC"		> ../$MACHINE/ra.h
echo "#define NRAD	$NRAD"		>> ../$MACHINE/ra.h
if [ $DUMPROUTINE = radump ]; then
	echo "#define RA_DUMP 1"	>> ../$MACHINE/ra.h
fi

echo "#define NRL	$NRL"		> ../$MACHINE/rl.h
if [ $DUMPROUTINE = rldump ]; then
	echo "#define RL_DUMP 1"	>> ../$MACHINE/rl.h
fi

echo "#define NBR	$NBR"		> ../$MACHINE/br.h
if [ $DUMPROUTINE = brdump ]; then
	echo "#define BR_DUMP 1"	>> ../$MACHINE/br.h
fi

echo "#define NSI	$NSI"		> ../$MACHINE/si.h
if [ $DUMPROUTINE = sidump ]; then
	echo "#define SI_DUMP 1"	>> ../$MACHINE/si.h
fi

echo "#define NTM	$NTM"		> ../$MACHINE/tm.h
if [ $AVIVTM = YES ] ; then
	echo "#define AVIVTM 1"		>> ../$MACHINE/tm.h
fi

echo "#define NXPD	$NXPD"		> ../$MACHINE/xp.h
echo "#define NXPC	$NXPC"		>> ../$MACHINE/xp.h
if [ $DUMPROUTINE = xpdump ]; then
	echo "#define XP_DUMP 1"	>> ../$MACHINE/xp.h
fi
if [ $XP_PROBE = YES ]; then
	echo "#define XP_PROBE 1"	>> ../$MACHINE/xp.h
fi

# save copy of localopts.h
cp $LO ../$MACHINE

# set permissions
chmod 664 ../$MACHINE/*
