COMPDIR=sys/compile/DRAWBRIDGE
SYSVER=`uname -r | cut -d'-' -f1`
CONFDIR="config-${SYSVER}"
DBVER=`grep "define VERSION" drawbrdg.h | sed -e 's:.*"\(.*\)".*:\1:'`

all: sys patched config depend build

patched: sys/.patched

config: ${COMPDIR}

depend: ${COMPDIR}/.depend

build: ${COMPDIR}/kernel

package: all boot
	@[ -d ../pkg ] || (echo ../pkg directory does not exist; false)
	@mkdir -p ../pkg/files
	cp ${COMPDIR}/kernel ../pkg/files/kernel.DRAWBRIDGE
	cp sys/i386/boot/biosboot/boot1 ../pkg/files/boot1
	cp sys/i386/boot/biosboot/boot2 ../pkg/files/boot2

diff:
	@[ -d sys ] || (echo 'sys' directory does not exist; false)
	@[ -d ${CONFDIR} ] || mkdir -p ${CONFDIR}
	@[ -e ${CONFDIR}/drawbridge_sys_patch ] && \
		mv -f ${CONFDIR}/drawbridge_sys_patch \
		      ${CONFDIR}/drawbridge_sys_patch.prev || true
	(cd sys/i386/boot/biosboot/; make clean)
	diff -Nur --exclude=compile --exclude=\*.orig \
		  --exclude=drawbr\* --exclude=DRAWBRIDGE \
		  --exclude=newvers.sh \
		  /sys sys/ > ${CONFDIR}/drawbridge_sys_patch || true


sys:
	@echo "Setting up links to the kernel source..."
	@find /sys/. -print | sed -e "s:/sys/\.:sys:" | \
		perl -nle 'if ( -d "/$$_" ) { \
				mkdir($$_, 0755); \
			} else { \
				symlink("/$$_", $$_); \
			}'
	@ln -s ../../drawbrdg.h sys/net/drawbrdg.h
	@ln -s ../../drawbridge.c sys/net/drawbridge.c
	@ln -s ../../../${CONFDIR}/DRAWBRIDGE sys/i386/conf/DRAWBRIDGE


sys/.patched: sys
	@echo "Applying kernel patch '${CONFDIR}/drawbridge_sys_patch'..."
	@patch < ${CONFDIR}/drawbridge_sys_patch > patch.log 2>&1 || \
		(echo; echo "An error occurred applying the patch!"; false)
	@[ -e ${CONFDIR}/security_patch ] && \
		(echo "Applying kernel patch '${CONFDIR}/security_patch'..."; \
		 patch < ${CONFDIR}/security_patch >> patch.log 2>&1) || true;
	@mv sys/conf/newvers.sh sys/conf/newvers.sh.orig;
	@sed -e "s:{VERSION} #$${v}:{VERSION} DRAWBRIDGE-${DBVER} #$${v}:" \
	     -e 's:    $${u}@$${h}\:$${d}\\\\n::' sys/conf/newvers.sh.orig \
	     > sys/conf/newvers.sh;
	@touch sys/.patched

${COMPDIR}: sys/i386/conf/DRAWBRIDGE sys sys/.patched
	(cd sys/i386/conf; config DRAWBRIDGE)

${COMPDIR}/.depend:
	(cd sys/compile/DRAWBRIDGE; make depend)

${COMPDIR}/kernel: ${COMPDIR} ${COMPDIR}/.depend
	(cd ${COMPDIR}; make)

install: ${COMPDIR}/kernel
	@if [ ! -e /dev/drawbridge ]; then \
		echo mknod /dev/drawbridge c 20 0; \
		mknod /dev/drawbridge c 20 0; \
		@chown 2000.2001 /dev/drawbridge; \
		@chmod 640 /dev/drawbridge; \
	fi
	install -c -o root -g wheel -m 755 ${COMPDIR}/kernel /kernel.DRAWBRIDGE
	ln -f /kernel.DRAWBRIDGE /kernel

boot:
	(cd ${COMPDIR}/../../i386/boot/biosboot/; make)

bootinstall:
	@[ ! -e /usr/mdec/boot1.orig ] && \
		mv /usr/mdec/boot1 /usr/mdec/boot.1.orig || true
	@[ ! -e /usr/mdec/boot2.orig ] && \
		mv /usr/mdec/boot2 /usr/mdec/boot.2.orig || true
	install -c -o bin -g bin -m 444 \
		${COMPDIR}/../../i386/boot/biosboot/boot1 /usr/mdec/boot1
	install -c -o bin -g bin -m 444 \
		${COMPDIR}/../../i386/boot/biosboot/boot2 /usr/mdec/boot2
	@disklabel -B wd0 > /dev/null 2>&1 || true
	@disklabel -B sd0 > /dev/null 2>&1 || true

clean:
	rm -rf sys patch.log
	@rm -f ../pkg/files/kernel.DRAWBRIDGE
	@rm -f ../pkg/files/boot1
	@rm -f ../pkg/files/boot2
