# $Id: Makefile,v 1.23 1998/09/21 20:22:17 ghudson Exp $
# 
# Makefile for NetBSD-Athena 8.0 install floppy.
# Requirements:
# Variable NETBSD_SRC must be the path of a NetBSD source tree. 
# KERNEL should point to a kernel with a 2M ramdisk.
# OS and SRVD should point an an os pack an a srvd.
# This makefile calls vnconfig, so it needs to run as root.

OS?= /afs/sipb.mit.edu/system/i386_nbsd1/os-1.3
SRVD?= /afs/sipb.mit.edu/system/i386_nbsd1/srvd-8.0
NETBSD_SRC?= /afs/dev.mit.edu/project/sipb/netbsd/1.3.2/src
KERNEL?= /afs/sipb.mit.edu/project/netbsd/release/athena-8.2/i386/kernels/ATHENAINST

CRUNCHBIN=afs-instbin
CRUNCHCONF=afs-instbin.conf


MOUNT_POINT?=	/mnt
TMPDIR?=	/u1/var/tmp # Needs to be local for vnd and uniondir to work
VND?=		vnd0
VND_DEV=	/dev/${VND}a
VND_CDEV=	/dev/${VND}d
VND_RDEV=	/dev/r${VND}a
DISKTYPE=	floppy3
UNIONDIR?=	${TMPDIR}/netbsd-src
FLOPPYDIR?=	${TMPDIR}/floppy

# Files from this directory that are installed onto the floppy
FILES=CellServDB ThisCell cacheinfo connect.sh continue.sh do-install.sh init.sh rc.sh setup.sh startafs.sh vars resolv.conf 

all: floppy.fs

.INTERRUPT: unconfig ununion
.END: unconfig ununion


clean: unconfig ununion
	rm -f $(CRUNCHBIN) $(CRUNCHBIN).mk $(CRUNCHBIN).cache $(CRUNCHBIN).c
	rm -f core *.o *.lo *.c .setup-afsd .setup-union
	rm -rf afsd $(FLOPPYDIR)
	rm -f ramdisk.fs floppy.fs netbsd netbsd.gz ramdisk.tar
	rm -rf $(UNIONDIR)

$(CRUNCHBIN).mk $(CRUNCHBIN).cache $(CRUNCHBIN).c: $(CRUNCHCONF) .setup-afsd .setup-union
	crunchgen -D$(UNIONDIR) -L./afsd $(CRUNCHCONF)

$(CRUNCHBIN): $(CRUNCHBIN).mk $(CRUNCHBIN).cache $(CRUNCHBIN).c
	$(MAKE) -f $(CRUNCHBIN).mk all

.setup-afsd:
	-mkdir afsd
	cp afsd.object afsd/afsd.o
	cp $(SRVD)/usr/afsws/lib/liblwp.a afsd
	cp $(SRVD)/usr/afsws/lib/librx.a afsd/librx.a.pre
	cp $(SRVD)/usr/afsws/lib/afs/libauth.a afsd
	cp $(SRVD)/usr/afsws/lib/afs/libcmd.a afsd
	cp $(SRVD)/usr/afsws/lib/afs/libsys.a afsd	
	cp $(SRVD)/usr/afsws/lib/afs/util.a afsd/libafsutil.a
	# Nuke XDR stuff from librx; it conflicts with libc.
	-mkdir afsd/rx
	(cd afsd/rx; \
	 ar x ../librx.a.pre; \
	 rm xdr.o xdr_array.o xdr_float.o xdr_mem.o; \
	 rm xdr_rec.o xdr_refernce.o xdr_update.o; \
	 ar r ../librx.a *.o; \
	 ranlib ../librx.a)
	touch .setup-afsd

.setup-union: 
	-mkdir $(UNIONDIR)
	mount_union -b $(NETBSD_SRC) $(UNIONDIR)
	touch .setup-union

ununion: 
	-umount $(UNIONDIR)
	-rm -f .setup-union

ramdisk.fs: ramdisk.tar
	rm -f $(TMPDIR)/$(.TARGET)
	dd if=/dev/zero of=$(TMPDIR)/$(.TARGET) bs=1m count=2
	vnconfig -v -c $(VND) $(TMPDIR)/$(.TARGET)
	-newfs -i 2048 -m 0 -o space $(VND_RDEV) 
	mount $(VND_DEV) $(MOUNT_POINT)
	tar -xf ramdisk.tar -C $(MOUNT_POINT)
	umount ${MOUNT_POINT}
	vnconfig -u ${VND_DEV}
	sync
	cp $(TMPDIR)/$(.TARGET) . 


ramdisk.tar: $(CRUNCHBIN) $(FILES)
	-mkdir $(FLOPPYDIR)
	cd $(FLOPPYDIR); mkdir -p -m 755 afs bin dev etc sbin tmp var
	cd $(FLOPPYDIR); mkdir -p -m 755 hd floppy install
	cd $(FLOPPYDIR); mkdir -p -m 755 usr/bin usr/sbin usr/vice/etc/dkload
	cd $(FLOPPYDIR); mkdir -p -m 755 usr/bin usr/sbin usr/vice/cache
	cd $(FLOPPYDIR); mkdir -p -m 755 etc/athena
	ln -fs /tmp $(FLOPPYDIR)/var/tmp
	ln -fs /tmp/netbsd $(FLOPPYDIR)/netbsd
	install -c -m 555 $(CRUNCHBIN) $(FLOPPYDIR)/afs-instbin
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/cat
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/chmod
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/cp
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/ed
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/expr
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/ln
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/ls
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/mkdir
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/mv
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/rm
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/sh
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/sync
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/test
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/bin/[
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/dmesg
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/halt
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/ifconfig
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/init.real
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/mknod
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/modload
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/mount
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/mount_ffs
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/mount_mfs
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/mount_nfs
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/newfs
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/ping
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/reboot
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/route
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/sbin/umount
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/chgrp
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/gzip
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/gunzip
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/gzcat
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/ld
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/less
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/more
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/sed
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/zcat
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/bin/chown
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/sbin/chroot
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/sbin/update
	ln -f $(FLOPPYDIR)/afs-instbin $(FLOPPYDIR)/usr/vice/etc/afsd
	install -c -m 555 $(SRVD)/etc/athena/netparams \
				$(FLOPPYDIR)/etc/athena
	install -c -m 444 $(SRVD)/etc/athena/masks \
				$(FLOPPYDIR)/etc/athena
	install -c -m 444 $(.CURDIR)/vars $(FLOPPYDIR)/install/vars
	install -c -m 555 $(.CURDIR)/connect.sh \
				$(FLOPPYDIR)/install/connect
	install -c -m 555 $(.CURDIR)/continue.sh \
				$(FLOPPYDIR)/install/continue
	install -c -m 555 $(.CURDIR)/do-install.sh \
				$(FLOPPYDIR)/install/do-install
	install -c -m 555 $(.CURDIR)/init.sh $(FLOPPYDIR)/sbin/init
	install -c -m 555 $(.CURDIR)/rc.sh $(FLOPPYDIR)/etc/rc
	install -c -m 555 $(.CURDIR)/setup.sh \
				$(FLOPPYDIR)/install/setup
	install -c -m 555 $(.CURDIR)/startafs.sh \
				$(FLOPPYDIR)/install/startafs
	install -c -m 444 $(OS)/etc/group $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/master.passwd $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/passwd $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/protocols $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/pwd.db $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/services $(FLOPPYDIR)/etc
	install -c -m 444 $(OS)/etc/spwd.db $(FLOPPYDIR)/etc
	install -c -m 444 $(.CURDIR)/resolv.conf $(FLOPPYDIR)/etc
	install -c -m 444 $(.CURDIR)/CellServDB \
				$(FLOPPYDIR)/usr/vice/etc
	install -c -m 444 $(.CURDIR)/ThisCell \
				$(FLOPPYDIR)/usr/vice/etc
	install -c -m 444 $(.CURDIR)/cacheinfo \
				$(FLOPPYDIR)/usr/vice/etc
	install -c -m 555 $(SRVD)/usr/vice/etc/dkload/libafs.o \
				$(FLOPPYDIR)/usr/vice/etc/dkload/libafs.o
	cd $(FLOPPYDIR)/dev; cp $(OS)/dev/MAKEDEV* .; sh MAKEDEV all
	cd $(FLOPPYDIR) ; tar -cf $(.CURDIR)/ramdisk.tar . 

netbsd.gz: $(KERNEL) ramdisk.fs
	cp $(KERNEL) $(.CURDIR)/netbsd
	mdsetimage -v $(.CURDIR)/netbsd $(TMPDIR)/ramdisk.fs
	gzip -9v < netbsd > netbsd.gz

floppy.fs: netbsd.gz
	dd if=/dev/zero of=$(TMPDIR)/$(.TARGET) bs=1k count=1440
	vnconfig -v -c -t $(DISKTYPE) $(VND) $(TMPDIR)/$(.TARGET)
	disklabel -rw $(VND_CDEV) $(DISKTYPE)
	-newfs -m 0 -o space -c 80 $(VND_RDEV) 
	/usr/mdec/installboot -vf /usr/mdec/biosboot.sym $(VND_RDEV)
	mount $(VND_DEV) $(MOUNT_POINT)
	install -c -m 555 $(.CURDIR)/netbsd.gz $(MOUNT_POINT)
	umount ${MOUNT_POINT}
	vnconfig -u ${VND_DEV}
	sync
	cp $(TMPDIR)/$(.TARGET) . 

unconfig:
	-umount -f $(MOUNT_POINT)
	-vnconfig -u $(VND_DEV)
	-rm -f $(TMPDIR)/floppy.fs
