architecture=$(shell dpkg --print-architecture)
export architecture

# [ED] avoid direct compilation of C programs from the utilities subdir
# (see the root.bin dependencies).  BTW all_subdirs target do the right job.
.SUFFIXES:

all::	build

# Edit the variables below this line

# Customize this to refer to the path of the Debian archive.
# It has to be an absolute PATH or else the basedisks.sh script will fail.
ftp_archive=/cdrom

archive=$(ftp_archive)/dists/hamm/main/binary-$(architecture)
tools_dir=$(ftp_archive)/tools
kernel_dir=$(ftp_archive)/project/other_kernels
# incoming=$(ftp_archive)/Incoming
incoming=../../Incoming

# where to look for .deb files (local updates, archive base & admin dirs)
pathcmd:=ls -1ft updates/P $(archive)/base/P $(archive)/admin/P 2> /dev/null | head -1

kbd_package:=$(shell $(pathcmd:P=kbd_0.95-*.deb))
kernel:=$(shell $(pathcmd:P=kernel-image-2.0.34_2.0.34-*.deb))

kernelatari=$(archive)/base/kernel-image-2.0.33-atari_2.0.33-2.deb
kernelamiga=$(archive)/base/kernel-image-2.0.33-amiga_2.0.33-2.deb
kernelmac=$(archive)/base/kernel-image-2.0.33-mac_2.0.33-1.deb

kerneltecra=$(incoming)/kernel-image-2.0.34_2.0.34-*_i386tecra.deb

kernel_version=$(shell echo $(kernel) | sed -e 's/.*kernel-image-//' | \
			sed -e 's/_.*//')
pcmcia_package=$(shell $(pathcmd:P=pcmcia-modules-$(kernel_version)_*.deb))

packages_with_modules=$(pcmcia_package)

debianversion=2.0

# Don't edit below this line

ifeq ($(architecture), sparc)
silo_pkg:=$(shell $(pathcmd:P=silo_*.deb))
arch_rootdisk_deps:=silo1440k.gz
arch_rescue_deps:=silo1440k.gz
endif

localfiles:: updates
	for f in $(incoming)/*_{all,$(architecture)}.deb; do \
		if [ ! -f updates/`basename $$f` ]; then \
			echo copying `basename $$f` to updates/ ; \
			cp $$f updates/; \
		fi;\
	done

# dependency for the files that have to be copied
updates::
	mkdir -p updates

base_archive=base$(shell echo ${debianversion} | sed -e 's/\./_/').tgz

build::	localfiles
	$(MAKE) all_subdirs
	find . -name \*~ | xargs rm -f
ifeq ($(architecture), i386)
	$(MAKE) resc1440.bin drv1440.bin
	$(MAKE) resc1200.bin drv1200.bin 
	$(MAKE) resc1440tecra.bin drv1440tecra.bin
	$(MAKE) resc1440tecra-fast.bin resc1440-fast.bin
	$(MAKE) lowmem.bin
else 
  ifeq ($(architecture), m68k)
	$(MAKE) resc720atari.bin drv720atari.bin 
	$(MAKE) resc1440atari.bin drv1440atari.bin
	$(MAKE) resc1440amiga.bin drv1440amiga.bin
#	$(MAKE) resc1440mac.bin drv1440mac.bin
  else
    ifeq ($(architecture), sparc)
	$(MAKE) resc1440.bin root1440.bin drv1440.bin
	$(MAKE) tftpboot.img root.tar.gz
    else
	echo unsupported architecture: $(architecture); false
    endif
  endif
endif
	$(MAKE) ${base_archive}

all_subdirs:
	(cd utilities;make KVER=$(kernel_version) )
	(cd documentation;make)

release:: build
	rm -rf release
	mkdir -p release
	( cd documentation; cp *.txt *.html ../release )
	rm release/*.in.html
	cp base-contents release/basecont.txt
	cp *.bin base*.tgz release/
ifneq ($(architecture), sparc)
	cp documentation/README-tecra documentation/README-fast release/
	cp linux release/
	test -f linuxtecra && cp linuxtecra release/ltecra
	# no DOS tools provided for sparc ;-) (if needed, eg. rawrite, take
	# them from i386 boot-disks directory)
	unix2dos <scripts/dos/install.bat >release/install.bat
	( cd release; unzip $(tools_dir)/rawrite2.zip rawrite2.exe rawrite2.txt)
	( cd release; unzip -p $(tools_dir)/lodlin16.zip \
		LODLIN16/LOADLIN.EXE >loadlin.exe )
else
	# the linux kernel is mainly dedicated to net boot: convert it to
	# a.out (required by the SUN TFTP boot process).
	gzip -dc linux > release/linux
	elftoaout -o release/linux-a.out release/linux
	rm -f release/linux
	# install netserver miniroot
	cp root.tar.gz release/
	# install tftpboot image
	cp tftpboot.img release/
endif
	( cd release; md5sum `find . -type f | grep -v md5sum.txt` \
		> md5sum.txt)

clean: distclean

install:
	mkdir -m 755 -p $(prefix)/usr/src/boot-floppies
	tar cf - `echo * | sed 's:debian::'` --exclude CVS \
	 | (cd $(prefix)/usr/src/boot-floppies;tar xf -)

distclean:
	#(cd utilities;make distclean)
	(cd documentation;make distclean)
	rm -f *.bin *.tgz sys_map*.gz  linux* modcont* core base-contents
	rm -f root.tar.gz
	rm -f `find -name \*~` disks-$(architecture)
	rm -rf release updates

keymaps.tgz:
	./keymaps.sh $(kbd_package)

modules.tgz sys_map.gz linux \
modcont: $(packages_with_modules) kernel.sh $(kernel) Makefile
	./kernel.sh "" $(kernel) $(packages_with_modules)

modulesatari.tgz sys_mapatari.gz linuxatari \
modcontatari: kernel.sh $(kernelatari) Makefile
	./kernel.sh atari $(kernelatari)

modulesamiga.tgz sys_mapamiga.gz linuxamiga \
modcontamiga: kernel.sh $(kernelamiga) Makefile
	./kernel.sh amiga $(kernelamiga)

modulesmac.tgz sys_mapmac.gz linuxmac \
modcontmac: kernel.sh $(kernelmac) Makefile
	./kernel.sh mac $(kernelmac)

modulestecra.tgz sys_maptecra.gz linuxtecra \
modconttecra: $(packages_with_modules) kernel.sh $(kerneltecra) Makefile
	./kernel.sh tecra $(kerneltecra) $(packages_with_modules)

drv1440.bin: modules.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh "" 1440 modules.tgz modcont ${archive}

drv1440atari.bin: modulesatari.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh atari 1440 modulesatari.tgz modcontatari ${archive}

drv1440amiga.bin: modulesamiga.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh amiga 1440 modulesamiga.tgz modcontamiga ${archive}

drv1440mac.bin: modulesmac.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh mac 1440 modulesmac.tgz modcontmac ${archive}

drv1440tecra.bin: modulestecra.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh tecra 1440 modulestecra.tgz modconttecra ${archive}

drv1200.bin: modules.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh "" 1200 modules.tgz modcont ${archive}

drv720atari.bin: modulesatari.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh atari 720 modulesatari.tgz modcontatari ${archive}

root.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		$(arch_rootdisk_deps) \
		rootdisk.sh Makefile keymaps.tgz
	case $(architecture) in \
	alpha)	./rootdisk.sh "" ${archive} 3800 ${debianversion} "" \
		;; \
	*)	./rootdisk.sh "" ${archive} 1800 ${debianversion} "" \
		;;\
	esac

rootatari.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile keymaps.tgz
	./rootdisk.sh "" ${archive} 1800 ${debianversion} atari

rootamiga.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile keymaps.tgz
	./rootdisk.sh "" ${archive} 1800 ${debianversion} amiga

rootmac.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile keymaps.tgz
	./rootdisk.sh "" ${archive} 1800 ${debianversion} mac

# netboot & low memory (not enough space for ramdisk)
root.tar.gz:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		$(arch_rootdisk_deps) \
		rootdisk.sh Makefile keymaps.tgz
	./rootdisk.sh net ${archive} 0 ${debianversion} ""

# [sparc] root.bin cannot fit on rescue disk
root1440.bin:	root.bin rootfloppy.sh Makefile
	./rootfloppy.sh "" root.bin 1440 ${debianversion}

lowmemrd.bin:	$(shell find utilities/lowmemrd -type f ) \
		lowmemrd.sh Makefile 
	./lowmemrd.sh

lowmem.bin:	linux sys_map.gz lowmem.sh Makefile \
		lowmemrd.bin
	./lowmem.sh "" $(kernel) lowmemrd.bin 1440 ${debianversion} ""

resc1440.bin:	linux sys_map.gz rescue.sh Makefile \
		$(arch_rescue_deps) \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh "" $(kernel) root.bin 1440 ${debianversion} ""

resc1440atari.bin: linuxatari sys_mapatari.gz rescue.sh Makefile \
		rootatari.bin $(shell find scripts/rescue -type f )
	./rescue.sh atari $(kernelatari) rootatari.bin 1440 ${debianversion} atari

resc1440amiga.bin: linuxamiga sys_mapamiga.gz rescue.sh Makefile \
		rootamiga.bin $(shell find scripts/rescue -type f )
	./rescue.sh amiga $(kernelamiga) rootamiga.bin 1440 ${debianversion} amiga

resc1440mac.bin: linuxmac sys_mapmac.gz rescue.sh Makefile \
		rootmac.bin $(shell find scripts/rescue -type f )
	./rescue.sh mac $(kernelmac) rootmac.bin 1440 ${debianversion} mac

resc1440-fast.bin:	resc1440.bin fast.sh
	./fast.sh resc1440.bin resc1440-fast.bin
	
resc1440tecra-fast.bin:	resc1440tecra.bin fast.sh
	./fast.sh resc1440tecra.bin resc1440tecra-fast.bin
	
resc1440tecra.bin:	linuxtecra sys_maptecra.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh tecra $(kerneltecra) root.bin 1440 ${debianversion} ""

resc1200.bin:	linux sys_map.gz rescue.sh Makefile \
		$(shell find scripts/rescue -type f )
	./rescue.sh "" ${kernel} root.bin 1200 ${debianversion} ""

resc720atari.bin: linuxatari sys_mapatari.gz rescue.sh Makefile \
		$(shell find scripts/rescue -type f )
	./rescue.sh atari ${kernelatari} rootatari.bin 720 ${debianversion} atari

# netboot for Sparc architecture
tftpboot:	tftpboot.img
tftpboot.img:	linux sys_map.gz root.bin tftpboot.sh Makefile
	./tftpboot.sh linux sys_map.gz root.bin ${debianversion}

ifeq ($(architecture), sparc)
# build SILO image used by rootdisk.sh & rescue.sh
silo1440k.gz: $(silo_pkg) silo_proto.sh Makefile
	./silo_proto.sh ${archive} ${@:.gz=} 1440
	rm -f $@
	gzip -9v ${@:.gz=}
endif

base:	${base_archive}

${base_archive}:	basedisks.sh \
			$(shell find scripts/basedisks -type f ) \
			$(shell find ${archive} -type f )
	./basedisks.sh ${archive} ${debianversion}

sign:
	pgp +clearsig=on -fast <release/md5sum.txt >release/md5sum.tmp
	mv release/md5sum.tmp release/md5sum.txt
