#	from: @(#)Makefile	8.1 (Berkeley) 6/10/93
#	     $Id: Makefile,v 1.11.2.1 1994/09/20 05:07:33 cgd Exp $

RELOC=1020000

DEFS= -DSTANDALONE -DCOMPAT_NOLABEL
CFLAGS=	-O ${INCPATH} ${DEFS}

BIN=../bin

AS=/usr/local/bin/gas
AR=$(BIN)/ar
CC=/usr/local/bin/gcc
LD=$(BIN)/ld
RANLIB=$(BIN)/ranlib
STRIP=$(BIN)/strip

.SUFFIXES: .s .rfc

.s.o:
	${CC} ${INCPATH} ${DEFS} -DRELOC=${RELOC} -c ${.CURDIR}/$<

SRCS=   autoconf.c cons.c ct.c devopen.c dca.c dcm.c fhpib.c hil.c hpib.c \
	ite.c ite_dv.c ite_gb.c ite_rb.c ite_subr.c ite_tc.c ite_hy.c \
	nhpib.c rd.c scsi.c sd.c
SRCS+=	conf.c machdep.c prf.c tgets.c

S=	${.CURDIR}/../../..

.PATH: ${S}/arch/${MACHINE_ARCH}/${MACHINE_ARCH}
.PATH: ${S}/stand ${S}/lib/libsa

.CURDIR=.
INCPATH=-I. -I.. -I../libsa -I../src/include -I../src/sys
#INCPATH=-I${.CURDIR} -I${.CURDIR}/../.. -I${S} -I${S}/lib/libsa

### find out what to use for libkern
# .include "$S/lib/libkern/Makefile.inc"
LIBKERN=libkern.a

# .include "$S/lib/libsa/Makefile.inc"
LIBSA=../libsa/libsa.a
#LIBSA=	${SA_LIB}

LIBS=	${LIBSA} libdrive.a \
	${LIBSA} ${LIBKERN}

BOOTS=	dboot.rfc netboot.rfc # dcopy cat ls
ALL=	${BOOTS} # mkboot installboot

all: ${ALL}

${BOOTS}: ${LIBS}

OBJS=bootconf.o devopen.o prom.o machdep.o
libdrive.a: ${OBJS}
	rm -f $@
	${AR} cq $@ ${OBJS}
	${RANLIB} $@

# depend on DEFS

#devopen.o machdep.o srt0.o: Makefile
#cons.o dca.o hil.o: Makefile
#ite.o ite_subr.o ite_dv.o ite_gb.o ite_hy.o ite_rb.o ite_tc.o: Makefile

# new boot
pboot:	pboot.o srt0.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o pboot.o ${LIBS} -o $@
	@size pboot
	@echo pboot total size should not exceed 1044480 bytes

ls:	ls.o srt0.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o ls.o ${LIBS} -o $@
	@size ls
	@echo ls total size should not exceed 1044480 bytes

cat:	cat.o srt0.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o cat.o ${LIBS} -o $@
	@size cat
	@echo cat total size should not exceed 1044480 bytes

# bootable from tape

tboot:	tboot.o srt0.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o tboot.o ${LIBS} -o $@
	@size tboot
	@echo tboot total size should not exceed 1044480 bytes

tboot.o: boot.c
	cp ${.CURDIR}/boot.c tboot.c; chmod +w tboot.c
	${CC} -c ${CFLAGS} -DJUSTASK tboot.c
	rm -f tboot.c

tcopy:	copy.o tpsrt0.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin tpsrt0.o copy.o ${LIBS} -o $@
	@size tcopy
	@echo tcopy total size should not exceed 1044480 bytes

# bootable from floppy or real disks

dboot.rfc:	dboot
	cp $* a.out
	${STRIP} a.out
	dd if=a.out of=$@ bs=32 skip=1
	rm a.out
	chmod +x $@
	ls -l $@
	@echo $@ size must not exceed 10240 bytes

dboot:	srt0.o dboot.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o dboot.o ${LIBS} -o $@

dboot.o:	boot.c
	rm -f dboot.c
	ln -s ${.CURDIR}/boot.c dboot.c
	${CC} -c ${CFLAGS} -DDBOOT dboot.c
	rm -f dboot.c

netboot.rfc:	netboot
	cp $* a.out
	${STRIP} a.out
	dd if=a.out of=$@ bs=32 skip=1
	rm a.out
	chmod +x $@
	ls -l $@
	@echo $@ size must not exceed 10240 bytes

netboot:	srt0.o netboot.o net.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin srt0.o netboot.o net.o ${LIBS} -o $@

netboot.o:	boot.c
	rm -f netboot.c
	ln -s ${.CURDIR}/boot.c netboot.c
	${CC} -c ${CFLAGS} -DNETBOOT netboot.c
	rm -f netboot.c

bootconf.o: ${.CURDIR}/conf.c
	rm -f bootconf.c
	ln -s ${.CURDIR}/conf.c bootconf.c
	${CC} -c ${CFLAGS} -DBOOT bootconf.c
	rm -f bootconf.c

flp:	dboot.rfc
	dd if=dboot.rfc of=/dev/fl0a bs=1k seek=2 count=10 conv=sync

# non-secure boot (allows booting non-root owned or world writable kernels)

#nboot:	nboot.o srt0.o bootconf.o ${LIBS}
#	${LD} -N -T ${RELOC} -e begin -o nboot srt0.o nboot.o bootconf.o ${LIBS}
#	@size nboot
#	@echo nboot text+data size should not exceed 57344 bytes
#	@echo nboot total size should not exceed 1044480 bytes
#
#nboot.o: ${.CURDIR}/boot.c
#	rm -f nboot.c
#	ln -s ${.CURDIR}/boot.c nboot.c
#	${CC} -c ${CFLAGS} -DINSECURE nboot.c
#	rm -f nboot.c

dcopy:	copy.o srt0.o conf.o ${LIBS}
	${LD} -N -T ${RELOC} -e begin -o dcopy srt0.o copy.o conf.o ${LIBS}

mkboot: ${.CURDIR}/mkboot.c
	${CC} ${CFLAGS} -o mkboot ${.CURDIR}/mkboot.c

installboot: ${.CURDIR}/installboot.sh
	@rm -f installboot
	cp -p ${.CURDIR}/installboot.sh installboot

# utilities

clean cleandir:
	rm -f *.o *.exe *.i errs make.out
	rm -f a.out pboot cat ls tboot tcopy
	rm -f boot[a-z]? boot[a-wyz][a-z].c conf[a-wyz][a-z].c
	rm -f core sboot bootconf.c nboot.c
	rm -f libdrive.a mkboot dboot dcopy nboot installboot *.lif

install: mkboot installboot ${ALL}
	./mkboot pboot netbsdboot.lif
	./mkboot dboot diskboot.lif
	./mkboot nboot ndiskboot.lif
	./mkboot tboot tcopy tapeboot.lif
	install -c -o bin -g bin -m 555 installboot ${DESTDIR}/usr/mdec
	install -c -o bin -g bin -m 444 netbsdboot.lif ${DESTDIR}/usr/mdec/rdboot
	rm -f ${DESTDIR}/usr/mdec/bootrd
	ln ${DESTDIR}/usr/mdec/rdboot ${DESTDIR}/usr/mdec/bootrd
	rm -f ${DESTDIR}/usr/mdec/sdboot
	ln ${DESTDIR}/usr/mdec/rdboot ${DESTDIR}/usr/mdec/sdboot
	rm -f ${DESTDIR}/usr/mdec/bootsd
	ln ${DESTDIR}/usr/mdec/sdboot ${DESTDIR}/usr/mdec/bootsd
	install -c -o bin -g bin -m 644 tapeboot.lif ${DESTDIR}/usr/mdec/bootct
#	install -o bin -g bin -m 755 -d ${DESTDIR}/sys/hpdist/tp
#	install -o bin -g bin -m 444 netbsdboot.lif ${DESTDIR}/sys/hpdist/tp
#	install -o bin -g bin -m 444 tapeboot.lif ${DESTDIR}/sys/hpdist/tp
#	install -o bin -g bin -m 444 diskboot.lif ${DESTDIR}/sys/hpdist/tp
#	install -o bin -g bin -m 444 ndiskboot.lif ${DESTDIR}/sys/hpdist/tp

FRC:
