: Modified from Gregs version by substituting $R for /usr/sys.
: Also added a test for c.c and l.s existing in SYSTEM-DIR,
: so that the use of mkconf can be subverted entirely.
: This MUST be run with $R/conf as current directory
: because the sh and cc needed are special and kept in $R/conf.

: compile [ROOT-DIR] [ SYSTEM-DIR ] [ load [ UNIX-NAME ] ]
: SYSTEM-DIR may not be "load"
: SYSTEM-DIR is the directory to which the compile will be relative
: load [ UNIX-NAME ] causes an attempt to load a system via loadit

set R = $1

if -r $R/conf/mkconf -a -r $R/conf/isconf -a \
   -r $R/conf/sysfix goto setup
echo "To compile, the files mkconf, isconf, and sysfix must be in the"
echo "$R/conf directory.  At least one of these files is missing."
: begin

set A = wk
if $1x = x goto startit
if $1 = load goto startit
set A = $1
: startit

echo configuring $A
if ! -r $A mkdir $A
if ! -r $A exit;: it failed
chmod 751 $A
if -r $A/configuration goto std
echo "rk" >$A/configuration
if -r $A/configuration goto std
echo "couldn't create default configuration file -- quitting"
: std
if -r $A/lib rm -f $A/lib
if -r $A/l.s -a -r $A/c.c goto skipmkconf
$R/conf/mkconf <$A/configuration
if -r $A/conf.mod sh -c "ed - <"$A"/conf.mod"
echo l.o:
	as l.s
	if -r $A/l.o rm -f $A/l.o
	mv a.out $A/l.o
	rm -f l.s
echo c.o:
	cc -c -O c.c
	if -r $A/c.o rm -f $A/c.o
	mv c.o $A/c.o
	rm -f c.c
sh /tmp/:compile: $A conf -P mch
chmod 644 $A/l.o $A/c.o $A/lib
goto kercompile

: skipmkconf
echo Using $A/l.s and $A/c.c
echo $A/l.o:
if { $R/conf/isconf $A/configuration split } as $R/conf/data.s $A/l.s
if ! { $R/conf/isconf $A/configuration split } as $A/l.s
	if -r $A/l.o rm -f $A/l.o
	mv a.out $A/l.o
echo $A/c.o:
	cc -c -O $A/c.c
	if -r $A/c.o rm -f $A/c.o
	mv c.o $A/c.o
sh /tmp/:compile: $A conf -P mch
chmod 644 $A/l.o $A/c.o $A/lib

: kercompile
echo Unix kernel code:
sh /tmp/:compile: $A ken \
	main	alloc	iget	prf	rdwri	slp	subr	text	\
	trap	sig	sysent	clock	fio	malloc	nami	pipe	\
	sys1	sys2	sys3	sys4	acct	badint

echo NCP resident code:
sh /tmp/:compile: $A ncpk nrdwr nopcls contab kerbuf ncpio btest
sh /tmp/:compile: $A ncpk -P itol

echo Unix device drivers:
: default device drivers -- always present
sh /tmp/:compile: $A dmr bio tty partab kl mem sys
: block device drivers -- disks and tapes
if { $R/conf/isconf $A/configuration rk } sh /tmp/:compile: $A dmr rk
if { $R/conf/isconf $A/configuration si } sh /tmp/:compile: $A dmr si
if { $R/conf/isconf $A/configuration rp } sh /tmp/:compile: $A dmr rp
if { $R/conf/isconf $A/configuration hp } sh /tmp/:compile: $A dmr hp
if { $R/conf/isconf $A/configuration rf } sh /tmp/:compile: $A dmr rf
if { $R/conf/isconf $A/configuration hs } sh /tmp/:compile: $A dmr hs
if { $R/conf/isconf $A/configuration rl } sh /tmp/:compile: $A dmr rl
if { $R/conf/isconf $A/configuration rx } sh /tmp/:compile: $A dmr rx
if { $R/conf/isconf $A/configuration tm } sh /tmp/:compile: $A dmr tm
if { $R/conf/isconf $A/configuration ht } sh /tmp/:compile: $A dmr ht
if { $R/conf/isconf $A/configuration tc } sh /tmp/:compile: $A dmr tc
sh /tmp/:compile: $A dmr devstart rhstart
: character device drivers -- tty-like devices
if { $R/conf/isconf $A/configuration dh } sh /tmp/:compile: $A dmr dhdm dh dhfdm
if { $R/conf/isconf $A/configuration dj } sh /tmp/:compile: $A dmr dj
if { $R/conf/isconf $A/configuration dz } sh /tmp/:compile: $A dmr dz
if { $R/conf/isconf $A/configuration dc } sh /tmp/:compile: $A dmr dc
if { $R/conf/isconf $A/configuration ptyc } sh /tmp/:compile: $A dmr pty
: character device drivers -- communication line drivers
if { $R/conf/isconf $A/configuration dq } sh /tmp/:compile: $A dmr dq
if { $R/conf/isconf $A/configuration du } sh /tmp/:compile: $A dmr du
if { $R/conf/isconf $A/configuration dp } sh /tmp/:compile: $A dmr dp
: character device drivers -- all other devices
if { $R/conf/isconf $A/configuration lp } sh /tmp/:compile: $A dmr lp
if { $R/conf/isconf $A/configuration cr } sh /tmp/:compile: $A dmr cr
if { $R/conf/isconf $A/configuration pc } sh /tmp/:compile: $A dmr pc
if { $R/conf/isconf $A/configuration dn } sh /tmp/:compile: $A dmr dn

echo IMP driver code:
if ! { $R/conf/isconf $A/configuration vdh } goto notvdh
	sh /tmp/:compile: $A dvr vdhio vdh
	sh /tmp/:compile: $A dvr -P vdh_swabuf vdh_clock
	goto vdhdone
: notvdh
	sh /tmp/:compile: $A dvr impio
	if { $R/conf/isconf $A/configuration acc } \
		sh /tmp/:compile: $A dvr acc
	: LATER ill
	if { $R/conf/isconf $A/configuration imp } \
		sh /tmp/:compile: $A dvr imp11a
: vdhdone

: this should be at the very end of the last archive library
sh /tmp/:compile: $A dvr -P CKSUM TRACE

rm -f /tmp/:compile:

if $1x = loadx sh loadit $A $2
echo done!

: setup
cat >/tmp/:compile:;goto begin
set A = $1
set B = $2
if $B != dvr goto notdvr
set B = ncpk/drivers
: notdvr
if $3x != -Px goto loop
set P = -P
: loop
if $3x = x exit
set D = $A/
if -r $D$2.c goto compile
set D = $B/
if -r $D$2.c goto compile
set D = $R/$D
if -r $D$2.c goto compile
echo $B/$2.c: module not found -- dummy module included
: comperr
as /dev/null
mv a.out $2.o
goto comptest
: compile
echo $B/$2: found as $D$2.c
cc -O -c -L$A:h:$B:$R/h:$R/$B:/usr/include $P $D$2.c
if $Px != -Px goto comptest
if ! -r $2.i cp $D$2.c $2.i
as $2.i
rm -f $2.i
if ! -r a.out goto loop
mv a.out $2.o
: comptest
if ! -r $2.o goto compfail
size $2.o
ar r $A/lib $2.o
rm -f $2.o
goto loop
: compfail
echo "Compile failed -- placeholder included"
goto comperr