#!/usr/bin/make -f

# Edit the variables below this line
LINGUA=C
export LINGUA

# Only define this if the boot disks are not creaed by the package maintainer
# MAINTAINER=Martin Schulze <joey@debian.org>
# export MAINTAINER

debianversion=2.1

# 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=/archive/debian

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

ifeq ($(architecture), powerpc)
  archive=$(ftp_archive)/dists/potato/main/binary-$(architecture)
else
  archive=$(ftp_archive)/dists/slink/main/binary-$(architecture)
endif
tools_dir=$(ftp_archive)/tools
kernel_dir=$(ftp_archive)/project/other_kernels
# incoming:=$(ftp_archive)/Incoming
incoming:=../../Incoming
local=$(ftp_archive)/local/binary-$(architecture)

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

-include Makefile.config

kbd_package:=$(shell $(pathcmd:P=kbd-data_0.96*.deb))

#---------------------------------------------------------------------------

# IF your language uses the Latin-2 charset, add its two letter code below.
LATIN2:=cs hr pl sk
# IF your language uses the Latin-3 charset, add its two letter code below.
LATIN3:=eo
ifneq ($(strip $(findstring $(LINGUA),$(LATIN2))),)
        export FONT=iso02g.psf
endif
ifneq ($(strip $(findstring $(LINGUA),$(LATIN3))),)
        export FONT=iso03g.psf
endif

ifeq ($(FONT), iso02g.psf)
	export SFONT=iso02.f16
endif
ifeq ($(FONT), iso03g.psf)
	export SFONT=iso03.f16
endif

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

all::	build

#---------------------------------------------------------------------------
# Kernel
#---------------------------------------------------------------------------
# kver    = main kernel for bootdisks
# altkver = alternate kernel for more bootdisks
kver:=2.0.35
ifeq ($(architecture), powerpc)
  kver:=2.2.3
  kbd_package:=$(shell $(pathcmd:P=kbd-data_0.97*.deb))
else
  ifeq ($(architecture), i386)
    kver:=2.0.38
  else
    ifeq ($(architecture), sparc)
      altkver:=2.2.1
    endif
  endif
endif

# find the newest kernel image of each type
kernel:=$(shell $(pathcmd:P=kernel-image-$(kver)_*.deb))
altkernel:=$(shell $(pathcmd:P=kernel-image-$(altkver)_*.deb))

# sanity fallback for kernel
ifeq ($(strip $(kernel)),)
kernel:=kernel-image-$(kver)_$(kver)-1.deb
endif

pcmcia_package:=$(shell $(pathcmd:P=pcmcia-modules-$(kver)_*.deb))

# alpha part
kernelalpha:=../kernel-image-2.0.35-
alpha_arches:= avanti xl xlt cabriolet eb66 eb66p eb64p eb164 pc164 \
      lx164 sx164 jensen noname takara mikasa noritake alcor \
      miata miata-s sable sable-g book1 ruffian 
alpha_rescue_targets := $(alpha_arches:%=resc1440_%.bin)
alpha_drivers_targets := $(alpha_arches:%=drv1440_%.bin)

#### m68k part
kernelatari=$(archive)/base/kernel-image-2.0.36-atari_2.0.36-1.deb
kernelamiga=$(archive)/base/kernel-image-2.0.36-amiga_2.0.36-1.deb
kernelmac=$(archive)/updates/kernel-image-2.0.36-mac_2.0.36-2.deb
kernelbvme6000=$(archive)/base/kernel-image-2.0.36-bvme6000_2.0.36-1.deb
kernelmvme162=$(archive)/base/kernel-image-2.0.36-mvme162_2.0.36-1.deb
kernelmvme167=$(archive)/base/kernel-image-2.0.36-mvme167_2.0.36-1.deb
tftplilo=$(archive)/updates/tftplilo_1.0.0-1_all.deb

#### powerpc part
kernelapus:=$(shell $(pathcmd:P=kernel-image-$(kver)-apus*.deb))
kernelchrp:=$(shell $(pathcmd:P=kernel-image-$(kver)-chrp*.deb))
kernelmbx:=$(shell  $(pathcmd:P=kernel-image-$(kver)-mbx*.deb))
kernelpmac:=$(shell $(pathcmd:P=kernel-image-$(kver)-pmac*.deb))
kernelprep:=$(shell $(pathcmd:P=kernel-image-$(kver)-prep*.deb))

#### sparc part
# Sparc dist contains 3 kernels:
#  2.0.35 for sun4[cdm] (microsparc, supersparc, hypersparc, ...)
#  2.2.1  for sun4[cdm]
#  2.2.1  for sun4u (ultrasparc)
ifeq ($(architecture), sparc)
  altkernel64:=$(shell $(pathcmd:P=kernel-image-$(altkver)-sun4u_*.deb))
endif

#### Laptop part 
kerneltecra:=$(shell $(pathcmd:P=kernel-image-$(kver)-scsimod_*.deb))

#---------------------------------------------------------------------------
# Consistency checks
#---------------------------------------------------------------------------
check: localfiles check_mirror check_loop check_minix check_kernel
	@echo "check successful"

# Use this to check if your kernel has support for the minix filesystem
check_minix:
	grep minix /proc/filesystems 2>&1 >/dev/null || \
		test -f /lib/modules/`uname -r`/fs/minix.o || \
		( echo minix filesystem support needed; exit 1 )

# Use this to check if your kernel has support for the loop device
check_loop:
	grep loop /proc/devices 2>&1 >/dev/null || \
		test -f /lib/modules/`uname -r`/block/loop.o || \
		( echo loop device support needed ; exit 1 )

# Use this to check if your local mirror has all the required packages
check_mirror: check_mirror.sh
	./check_mirror.sh $(archive)

# Use this to see if you have the proper kernels available
check_kernel:
ifeq ($(architecture), i386)
	[ -f "$(kernel)" ] || \
		( echo standard kernel is not available ; exit 1 )
	[ -f "$(pcmcia_package)" ] || \
		( echo PCMCIA modules are not available ; exit 1 )
	[ -f "$(kerneltecra)" ] || \
		( echo tecra kernel is not available ; exit 1 )
else
  ifeq ($(architecture), m68k)
	[ -f "$(kernelatari)" ] || \
		( echo atari kernel is not available ; exit 1 )
	[ -f "$(kernelamiga)" ] || \
		( echo amiga kernel is not available ; exit 1 )
	[ -f "$(kernelmac)" ] || \
		( echo mac kernel is not available ; exit 1 )
	[ -f "$(kernelbvme6000)" ] || \
		( echo bvme6000 kernel is not available ; exit 1 )
	[ -f "$(kernelmvme162)" ] || \
		( echo mvme162 kernel is not available ; exit 1 )
	[ -f "$(kernelmvme167)" ] || \
		( echo mvme167 kernel is not available ; exit 1 )
else
  ifeq ($(architecture), powerpc)
	[ -f "$(kernelchrp)" ] || \
		( echo chrp kernel is not available ; exit 1 )
	[ -f "$(kernelpmac)" ] || \
		( echo pmac kernel is not available ; exit 1 )
	[ -f "$(kernelprep)" ] || \
		( echo prep kernel is not available ; exit 1 )
else
  ifeq ($(architecture), sparc)
	[ -f "$(kernel)" ] || \
		( echo standard kernel is not available ; exit 1 )
	[ -f "$(altkernel)" ] || \
		( echo alternative kernel is not available ; exit 1 )
	[ -f "$(altkernel64)" ] || \
		( echo sun4u kernel is not available ; exit 1 )
else
  ifeq ($(architecture), alpha)
	# alpha is tricky
  else
	echo unsupported architecture: $(architecture); false
       endif
      endif
    endif
  endif
endif

#---------------------------------------------------------------------------
# Don't edit below this line

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

localpackages:=$(shell find $(incoming)/ -name '*_all.deb' -o -name '*_$(architecture).deb')

localfiles:: updates
ifneq ($(strip $(localpackages)),)
	for f in $(localpackages); do \
		if [ ! -f updates/`basename $$f` ]; then \
			echo copying `basename $$f` to updates/ ; \
			cp $$f updates/ ; \
		fi ; \
	done
endif

# 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) utils
	find . -name \*~ | xargs rm -f
ifeq ($(architecture), i386)
	$(MAKE) resc1440.bin drv1440.bin resc1440-safe.bin
	$(MAKE) resc1200.bin drv1200.bin 
# APH DISABLE TECRA
#	$(MAKE) resc1440tecra.bin drv1440tecra.bin resc1440tecra-safe.bin
	$(MAKE) lowmem.bin
else 
  ifeq ($(architecture), m68k)
	$(MAKE) resc720atari.bin drv720atari.bin 
	$(MAKE) resc1440atari.bin drv1440atari.bin lowmematari.bin
	$(MAKE) resc1440amiga.bin drv1440amiga.bin
	$(MAKE) resc1440mac.bin drv1440mac.bin
	$(MAKE) resc1440bvme6000.bin drv1440bvme6000.bin
	$(MAKE) resc1440mvme162.bin drv1440mvme162.bin
	$(MAKE) resc1440mvme167.bin drv1440mvme167.bin
else 
  ifeq ($(architecture), powerpc)
#	$(MAKE) resc1440apus.bin drv1440apus.bin
	$(MAKE) resc1440chrp.bin drv1440chrp.bin
#	$(MAKE) resc1440mbx.bin drv1440mbx.bin
	$(MAKE) resc1440pmac.bin drv1440pmac.bin
	$(MAKE) resc1440prep.bin drv1440prep.bin
	$(MAKE) tftpboot.img tftpboot-powerpc.img root.tar.gz
else
  ifeq ($(architecture), sparc)
	$(MAKE) resc1440.bin drv1440.bin \
			resc1440-$(altkver).bin drv1440-$(altkver).bin \
			resc1440-$(altkver)-sun4u.bin drv1440-$(altkver)-sun4u.bin \
			tftpboot.img tftpboot-$(altkver).img \
			root.tar.gz
else
  ifeq ($(architecture), alpha)
	$(MAKE) $(alpha_rescue_targets) $(alpha_drivers_targets) root1440.bin
  else
	echo unsupported architecture: $(architecture); false
       endif
      endif
    endif
  endif
endif
	$(MAKE) $(base_archive)

utils:
	$(MAKE) -C utilities KVER=$(kver) LINGUA=$(LINGUA)

define hack-dinstall-msgs
  [ -f utilities/dinstall/messages/lang_$(LINGUA).h ] ||		\
	ln -s lang_C.h utilities/dinstall/messages/lang_$(LINGUA).h
endef

define unhack-dinstall-msgs
  [ ! -L utilities/dinstall/messages/lang_$(LINGUA).h ] ||		\
	rm -f utilities/dinstall/messages/lang_$(LINGUA).h
endef

docs:
	$(hack-dinstall-msgs)
	$(MAKE) -C documentation KVER=$(kver) ALTKVER=$(altkver)	\
		RELEASE=$(debianversion)				\
		util-linux=$(shell $(pathcmd:P=util-linux_*.deb))	\
		amiga-fdisk=$(shell $(pathcmd:P=amiga-fdisk_*.deb))	\
		atari-fdisk=$(shell $(pathcmd:P=atari-fdisk_*.deb))	\
		mac-fdisk=$(shell $(pathcmd:P=mac-fdisk_*.deb))		\
		pmac-fdisk-cross=$(shell $(pathcmd:P=pmac-fdisk-cross_*.deb))
	$(unhack-dinstall-msgs)

# documentation for all architectures
all-arch-docs:
	[ -d documentation/arch ] || mkdir documentation/arch
	set -e								;\
	for arch in `grep ARCH documentation/defaults.ent | cut -d' ' -f 3`; do	\
		$(MAKE) architecture=$$arch all-arch-docs-internal	;\
	done

all-arch-docs-internal:
	$(hack-dinstall-msgs)
	$(MAKE) -C documentation KVER=$(kver) ALTKVER=$(altkver)	\
		RELEASE=$(debianversion)				\
		util-linux=$(shell $(pathcmd:P=util-linux_*.deb))	\
		amiga-fdisk=$(shell $(pathcmd:P=amiga-fdisk_*.deb))	\
		atari-fdisk=$(shell $(pathcmd:P=atari-fdisk_*.deb))	\
		mac-fdisk=$(shell $(pathcmd:P=mac-fdisk_*.deb))		\
		pmac-fdisk-cross=$(shell $(pathcmd:P=pmac-fdisk-cross_*.deb)) \
		all-arches
	$(unhack-dinstall-msgs)

# documentation for all architectures and languages
all-lang-arch-docs:
	$(MAKE) LINGUA=C all-arch-docs
	@# note that you have to manually add languages which don't have 
	@# dinstall translations; note that sk is skipped due to lack of
	@# debiandoc-sgml support
	set -e								;\
	for file in utilities/dinstall/messages/lang_*.h fi ja; do	\
	  lang=`basename $$file | sed -e 's/lang_//; s/\.h//;'`		;\
	  if [ -s utilities/dinstall/messages/lang_$$lang.h -a		\
	       -e "documentation/install.$$lang.sgml" -a		\
	       -e "documentation/release-notes.$$lang.sgml" -a		\
	       -e "documentation/dselect-beginner.$$lang.sgml" ]; then	\
	    if [ $$lang != sk ]; then 					\
	      echo "** making documentation for language $$lang"	;\
	      rm -f documentation/messages.ent				;\
	      $(MAKE) LINGUA=$$lang all-arch-docs			;\
	      echo							;\
	    else							\
	      echo "** skipping documentation for language $$lang"	;\
	    fi								\
	  fi								\
	done

release:: build
	mkdir -p release
	$(MAKE) -C documentation DESTDIR=$(shell pwd)/release install
	cp base-contents release/basecont.txt
	cp *.bin base*.tgz release/
ifeq ($(architecture), i386)
	cp documentation/README-tecra documentation/README-safe release/
	if [ -f linuxtecra ]; then cp linuxtecra release/ltecra ; fi
	# DOS tools provided for i386 only (if other arches need, eg.
	# rawrite, take them from i386 boot-disks directory)
	recode latin1:ibmpc <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
	cp linux release/
endif
ifeq ($(architecture), sparc)
	# the linux kernel is mainly dedicated to net boot: convert it to
	# a.out (required by the SUN TFTP boot process) and pad the result to
	# be 4-bytes aligned (required by some old bootproms).
	# Idem for alternate kernel(s)
	for kvext in "" -$(altkver) -$(altkver)-sun4u; do \
	  if [ -f linux$$kvext ]; then \
	    gzip -dc linux$$kvext > release/linux$$kvext ; \
	    elftoaout -o release/linux$$kvext-a.out release/linux$$kvext ; \
	    rm -f release/linux$$kvext ; \
	    size=`ls -l release/linux$$kvext-a.out | awk '{print $$5}'` ; \
	    rem=`expr \( 4 - $$size % 4 \) % 4` ; \
	    dd if=/dev/zero bs=1 count=$$rem >> release/linux$$kvext-a.out ; \
	  fi ; \
	done
	# install netserver miniroot
	cp root.tar.gz release/
	# install tftpboot image
	cp tftpboot.img release/
	cp tftpboot-$(altkver).img release/
endif
ifeq ($(architecture), m68k)
	#
	# m68k particulars; we support 4 m68k platforms so far, all different. If 
	# you ate lunch just recently, simply skip this section.
	#
	# the basics; so far as not copied yet
	if [ -f linuxatari ]; then cp linuxatari release/ ; fi
	if [ -f linuxamiga ]; then cp linuxamiga release/ ; fi
	if [ -f linuxmac ]; then cp linuxmac release/ ; fi
	if [ -f linuxbvme6000 ]; then cp linuxbvme6000 release/ ; fi
	if [ -f linuxmvme162 ]; then cp linuxmvme162 release/ ; fi
	if [ -f linuxmvme167 ]; then cp linuxmvme167 release/ ; fi
	if [ -f sys_mapatari.gz ]; then cp sys_mapatari.gz release/ ; fi
	if [ -f sys_mapamiga.gz ]; then cp sys_mapamiga.gz release/ ; fi
	if [ -f sys_mapmac.gz ]; then cp sys_mapmac.gz release/ ; fi
	if [ -f sys_mapbvme6000.gz ]; then cp sys_mapbvme6000.gz release/ ; fi
	if [ -f sys_mapmvme162.gz ]; then cp sys_mapmvme162.gz release/ ; fi
	if [ -f sys_mapmvme167.gz ]; then cp sys_mapmvme167.gz release/ ; fi
	# add more (bootstra.ttp, rawwrite.ttp, bootargs, amiboot)?
	#
	# Atari install kit: lzh archive
	#
	mkdir -p release/atari
	mkdir -p release/atari/debian
	cp resc1440atari.bin release/atari/debian/resc1440.bin
	cp drv1440atari.bin release/atari/debian/drv1440.bin
	cp linuxatari release/atari/debian/linux
	cp sys_mapatari.gz release/atari/debian/sysmap.gz
	cp rootatari.bin release/atari/debian/root.bin
	cp lowmemrd.bin release/atari/debian/
	cp m68k-specials/bootstra.ttp release/atari/debian/bootstra.prg
	# cp m68k-specials/bootargs release/atari/debian/bootargs
	echo "-s -k linux -r root.bin root=/dev/ram video=keep load_ramdisk=1" > release/atari/debian/bootargs
	cp m68k-specials/debian-atari.txt release/atari/debian/install.doc
	# create lzh archive
	(cd release/atari/ && lha a ../atariinstall.lzh debian/)
	# clean up
	rm -rf release/atari
	# 
	# Atari: loose files
	#
	cp m68k-specials/rawwrite.ttp release/
	cp m68k-specials/debian-atari.txt release/install-atari.txt
	#
	# Amiga install kit: lzh archive
	#
	mkdir -p release/amiga
	mkdir -p release/amiga/debian
	# unpack AmigaOS magic from m68k support directory
	(cd release/amiga/ && lha x ../../m68k-specials/amiga-kit.lzh)
	cp resc1440amiga.bin release/amiga/debian/resc1440.bin
	cp drv1440amiga.bin release/amiga/debian/drv1440.bin
	cp linuxamiga release/amiga/debian/
	cp sys_mapamiga.gz release/amiga/debian/
	cp rootamiga.bin release/amiga/debian/
	cp lowmemrd.bin release/amiga/debian/
	# create lzh archive
	(cd release/amiga/ && lha a ../amigainstall.lzh *)
	# clean up
	rm -rf release/amiga
	#
	# Amiga: loose files
	#
	cp m68k-specials/debian-amiga.txt release/install-amiga.txt
	#
	# Mac install kit: HFS floppy image, to be packaged as StuffIt archive
	#
	# create HFS floppy image
	dd if=/dev/zero of=release/macinstall-floppy-hfs.img bs=1024 count=1440
	hformat -l "Debian/68k" release/macinstall-floppy-hfs.img
	# mount it (':' is top dir)
	hmount release/macinstall-floppy-hfs.img
	# copy kernel/sysmap/ramdisk as raw
	hcopy -r linuxmac :linux
	hcopy -r sys_mapmac.gz :System-Map.gz
	hcopy -r rootmac.bin :root.bin
	# MacOS magic; useful for HFS CD I hope
	#
	# hybrid CD note:
	# mount this HFS floppy image using the mount command
	# mount -t hfs -o loop,fork=netatalk,afpd
	# copy over both Penguin-15 and .AppleDouble/Penguin-15
	# copy over both Penguin Prefs and .AppleDouble/Penguin Prefs
	# (.AppleDouble files go into .AppleDouble subdir of target dir)
	#
	# make CD image using the command
	# mkhybrid -map <mapfile> --netatalk -a -j -hfs -r -V <label> -o <image> <source dir>
	#
	# map file contains stuff like
	# # ext. xlate  creator  type    comment
	# .hqx   Ascii  'BnHx'   'TEXT'  "BinHex file"
	# .mov   Raw    'TVOD'   'MooV'  "QuickTime Movie"
	# .deb   Raw    'Debn'   'bina'  "Debian package"
	# .bin   Raw    'Debn'   'bina'  "Floppy or ramdisk image"
	# *      Ascii  'ttxt'   'TEXT'  "Text file"
	#
	# Note that we can't use MacBinary mapping for .bin files due to
	# name clashes with the binary image files, so we have to provide 
	# all encoded Mac files in BinHex format.
	#
	# Kudos Brad Midgley, <brad@pht.com> for the tricks!
	#
	# copy booter as BinHex
	hcopy -b m68k-specials/Penguin-17.hqx :
	# copy Preferences as BinHex
	hcopy -b m68k-specials/Penguin_Prefs_floppy.hqx :"Penguin Prefs"
	# copy install doc as text
	hcopy -t m68k-specials/debian-mac.txt :Install.doc
	humount release/macinstall-floppy-hfs.img
	# package Mac booter stuff for the CD install dir
	# (tar format until I can come up with something nicer)
	mkdir -p release/mac
	mkdir -p release/mac/install/
	mkdir -p release/mac/install/mac/
	cp m68k-specials/Penguin-17.hqx release/mac/install/mac/
	#cp m68k-specials/Penguin_Prefs_CD.hqx release/mac/install/mac/
	#cp m68k-specials/Penguin_Prefs_CD_autoboot.hqx release/mac/install/mac/
	dd if=/dev/zero of=release/macinstall-temp.img bs=1024 count=1440
	hformat release/macinstall-temp.img
	# mount it (':' is top dir)
	hmount release/macinstall-temp.img
	hcopy -b m68k-specials/Penguin_Prefs_CD.hqx :"Penguin Prefs"
	hcopy -b m68k-specials/Penguin_Prefs_CD_autoboot.hqx :"Penguin Prefs (autoboot)"
	hcopy -b :"Penguin Prefs" release/mac/install/mac/
	hcopy -b :"Penguin Prefs (autoboot)" release/mac/install/mac/
	humount release/macinstall-temp.img
	rm release/macinstall-temp.img
	cp m68k-specials/debian-mac.txt release/mac/install/mac/
	(cd release/mac && tar cfvz ../macinstall.tar.gz install/)
	# clean up
	rm -rf release/mac
	#
	# Mac: loose files
	#
	cp m68k-specials/debian-mac.txt release/install-mac.txt
	#
	# VME stuff
	#
	(rm -rf /var/tmp/extract-tftplilo; \
	mkdir /var/tmp/extract-tftplilo; \
	dpkg-deb --extract $(tftplilo) /var/tmp/extract-tftplilo; \
	cp /var/tmp/extract-tftplilo/boot/tftplilo.* release/; \
	rm -rf /var/tmp/extract-tftplilo )
	#
	#
	# VME: loose files
	#
	cp m68k-specials/debian-vme.txt release/install-vme.txt
	cp m68k-specials/tftplilo.txt release/
	cp m68k-specials/tftplilo.conf release/
	cp m68k-specials/rawrite2.exe release/
	cp m68k-specials/bvmbug-G.bin release/
	cp m68k-specials/bvmbug.txt release/
	#
	# Copy the m68k magic used for this build to release/
	#
	cp m68k-specials/m68k-support.tar.gz release/
	# map files; tell the FTP maintainer what goes where
	cp m68k-specials/disks-m68k.map release/
	cp m68k-specials/cd-m68k.map release/
	# install scripts for the FTP and CD people
	nawk '{ print "cp " $$1 " " $$2 }' release/disks-m68k.map > release/install-disks-m68k.sh
	nawk '{ print "rm " $$1 }' release/disks-m68k.map >> release/install-disks-m68k.sh
	nawk '{ print "cp " $$1 " " $$2 }' release/cd-m68k.map > release/install-cd-m68k.sh
endif
ifeq ($(architecture), powerpc)
	# the basics; so far as not copied yet
	if [ -f linuxchrp ]; then cp linuxchrp release/ ; fi
	if [ -f linuxpmac ]; then cp linuxpmac release/ ; fi
	if [ -f linuxprep ]; then cp linuxprep release/ ; fi
	if [ -f sys_mapchrp.gz ]; then cp sys_mapchrp.gz release/ ; fi
	if [ -f sys_mappmac.gz ]; then cp sys_mappmac.gz release/ ; fi
	if [ -f sys_mapprep.gz ]; then cp sys_mapprep.gz release/ ; fi
	#
	# CHRP install kit: tar.gz archive (for the moment)
	#
	mkdir -p release/chrp
	mkdir -p release/chrp/debian
	cp resc1440chrp.bin release/chrp/debian/resc1440.bin
	cp drv1440chrp.bin release/chrp/debian/drv1440.bin
	cp linuxchrp release/chrp/debian/linux
	cp sys_mapchrp.gz release/chrp/debian/sysmap.gz
	cp root.bin release/chrp/debian/root.bin
	(cd release/chrp/ && tar cfvz ../chrpinstall.tar.gz debian/)
	rm -rf release/chrp
	#cp powerpc-specials/debian-pmac.txt release/install-pmac.txt
	#
	# PMAC install kit: HFS floppy image, to be packaged as StuffIt archive
	#
	dd if=/dev/zero of=release/pmacinstall-floppy-hfs.img bs=1024 count=1440
	hformat -l "Debian/PowerPC" release/pmacinstall-floppy-hfs.img
	# mount it (':' is top dir)
	hmount release/pmacinstall-floppy-hfs.img
	# copy kernel/sysmap/ramdisk as raw
	hcopy -r linuxpmac :linux
	hcopy -r sys_mappmac.gz :System-Map.gz
	hcopy -r root.bin :root.bin
	# copy booter 
	#hcopy -b powerpc-specials/bootX :
	# copy install doc as text
	#hcopy -t powerpc-specials/debian-pmac.txt :Install.doc
	humount release/pmacinstall-floppy-hfs.img
	#cp powerpc-specials/debian-pmac.txt release/pmac/install/pmac/
	(cd release/pmac && tar cfvz ../pmacinstall.tar.gz install/)
	rm -rf release/pmac
	#cp powerpc-specials/debian-pmac.txt release/install-pmac.txt
	#
	# PReP install kit: tar.gz archive (for the moment)
	#
	mkdir -p release/prep
	mkdir -p release/prep/debian
	cp resc1440prep.bin release/prep/debian/resc1440.bin
	cp drv1440prep.bin release/prep/debian/drv1440.bin
	cp linuxprep release/prep/debian/linux
	cp sys_mapprep.gz release/prep/debian/sysmap.gz
	cp root.bin release/prep/debian/root.bin
	(cd release/prep/ && tar cfvz ../prepinstall.tar.gz debian/)
	rm -rf release/prep
	#cp powerpc-specials/debian-prep.txt release/install-prep.txt
	#
	# Copy the powerpc magic used for this build to release/
	#
	#cp powerpc-specials/powerpc-support.tar.gz release/
	# map files; tell the FTP maintainer what goes where
	#cp powerpc-specials/disks-powerpc.map release/
	#cp powerpc-specials/cd-powerpc.map release/
	# install scripts for the FTP and CD people
	#nawk '{ print "cp " $$1 " " $$2 }' release/disks-powerpc.map > release/install-disks-powerpc.sh
	#nawk '{ print "rm " $$1 }' release/disks-powerpc.map >> release/install-disks-powerpc.sh
	#nawk '{ print "cp " $$1 " " $$2 }' release/cd-powerpc.map > release/install-cd-powerpc.sh
	#
	# TFTP install kit
	#	
	cp root.tar.gz release/
	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 --exclude debian/tmp -cf - . \
		| tar -C $(prefix)/usr/src/boot-floppies -xvf -

distclean:
	$(MAKE) -C utilities distclean
	$(MAKE) -C documentation distclean
	$(MAKE) -C scripts/basedisks/master2files clean
	rm -f *.bin *.tgz sys_map*.gz linux* modcont* core base-contents $(SFONT)
	rm -f root.tar.gz silo1440k*.gz
	rm -f `find -name \*~` disks-$(architecture)
	rm -rf release updates
	rm -rf scripts/basedisks/new_root_home/messages
	rm -rf check_basedeps

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

tasks_n_profiles:
	$(MAKE) -C scripts/basedisks/master2files PACKAGES=$(archive)/Packages

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


#-----------------------------------------------------------------------
# Modules
#-----------------------------------------------------------------------
packages_with_modules=$(pcmcia_package)

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

modulesalpha_%.tgz sys_mapalpha_%.gz linuxalpha_% \
modcontalpha_% :  kernel.sh $(kernelalpha)%.deb Makefile
	./kernel.sh alpha_$* $(kernelalpha)$*.deb

#### m68k part ####
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)

modulesbvme6000.tgz sys_mapbvme6000.gz linuxbvme6000 \
modcontbvme6000: kernel.sh $(kernelbvme6000) Makefile
	./kernel.sh bvme6000 $(kernelbvme6000)

modulesmvme162.tgz sys_mapmvme162.gz linuxmvme162 \
modcontmvme162: kernel.sh $(kernelmvme162) Makefile
	./kernel.sh mvme162 $(kernelmvme162)

modulesmvme167.tgz sys_mapmvme167.gz linuxmvme167 \
modcontmvme167: kernel.sh $(kernelmvme167) Makefile
	./kernel.sh mvme167 $(kernelmvme167)


#### powerpc part ####
modulesapus.tgz sys_mapapus.gz linuxapus \
modcontapus: kernel.sh $(kernelapus) Makefile
	./kernel.sh apus $(kernelapus)

moduleschrp.tgz sys_mapchrp.gz linuxchrp \
modcontchrp: kernel.sh $(kernelchrp) Makefile
	./kernel.sh chrp $(kernelchrp)

modulesmbx.tgz sys_mapmbx.gz linuxmbx \
modcontmbx: kernel.sh $(kernelmbx) Makefile
	./kernel.sh mbx $(kernelmbx)

modulespmac.tgz sys_mappmac.gz linuxpmac \
modcontpmac: kernel.sh $(kernelpmac) Makefile
	./kernel.sh pmac $(kernelpmac)

modulesprep.tgz sys_mapprep.gz linuxprep \
modcontprep: kernel.sh $(kernelprep) Makefile
	./kernel.sh prep $(kernelprep)

#### sparc part
modules-$(altkver).tgz sys_map-$(altkver).gz linux-$(altkver) \
modcont-$(altkver): kernel.sh $(altkernel) Makefile
	./kernel.sh -$(altkver) $(altkernel)

modules-$(altkver)-sun4u.tgz sys_map-$(altkver)-sun4u.gz linux-$(altkver)-sun4u \
modcont-$(altkver)-sun4u: kernel.sh $(altkernel64) Makefile
	./kernel.sh -$(altkver)-sun4u $(altkernel64)


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


#------------------------------------------------------------------------
# Driver 
#------------------------------------------------------------------------

#### alpha part
$(alpha_drivers_targets) : drv1440_%.bin: modulesalpha_%.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh alpha_$* 1440 modulesalpha_$*.tgz modcontalpha_$* $(archive)
	mv drv1440alpha_$*.bin $@


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

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


#### m68k part
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)

drv1440bvme6000.bin: modulesbvme6000.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh bvme 1440 modulesbvme6000.tgz modcontbvme6000 $(archive)
	mv drv1440bvme.bin drv1440bvme6000.bin

drv1440mvme162.bin: modulesmvme162.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh mvme 1440 modulesmvme162.tgz modcontmvme162 $(archive)
	mv drv1440mvme.bin drv1440mvme162.bin

drv1440mvme167.bin: modulesmvme167.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh mvme 1440 modulesmvme167.tgz modcontmvme167 $(archive)
	mv drv1440mvme.bin drv1440mvme167.bin

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


#### powerpc part
drv1440apus.bin: modulesapus.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh apus 1440 modulesapus.tgz modcontapus $(archive)

drv1440chrp.bin: moduleschrp.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh chrp 1440 moduleschrp.tgz modcontchrp $(archive)

drv1440mbx.bin: modulesmbx.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh mbx 1440 modulesmbx.tgz modcontmbx $(archive)

drv1440pmac.bin: modulespmac.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh pmac 1440 modulespmac.tgz modcontpmac $(archive)

drv1440prep.bin: modulesprep.tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh prep 1440 modulesprep.tgz modcontprep $(archive)

#### sparc
drv1440-$(altkver).bin: modules-$(altkver).tgz drivers.sh Makefile \
		$(shell find scripts/drivers -type f )
	./drivers.sh -$(altkver) 1440 modules-$(altkver).tgz modcont-$(altkver) $(archive)

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


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


#-----------------------------------------------------------------------------
# Root - the ramdisk, mounted as / 
#-----------------------------------------------------------------------------

root.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		$(arch_rootdisk_deps) \
		rootdisk.sh Makefile keymaps.tgz utils
	case $(architecture) in \
	alpha)	./rootdisk.sh "" $(archive) 3000 $(debianversion) "" $(LINGUA) "$(FONT)"\
		;; \
	sparc)	./rootdisk.sh "" $(archive) 2800 $(debianversion) "" $(LINGUA) "$(FONT)"\
		;; \
	powerpc) \
		./rootdisk.sh "" $(archive) 2800 $(debianversion) "" $(LINGUA) "$(FONT)"\
		;; \
	*)	./rootdisk.sh "" $(archive) 1800 $(debianversion) "" $(LINGUA) "$(FONT)" $(SFONT)\
		;;\
	esac

#### m68k part
rootatari.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile keymaps.tgz utils
	./rootdisk.sh atari $(archive) 1800 $(debianversion) atari $(LINGUA)

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

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

rootbvme.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile #keymaps.tgz
	./rootdisk.sh bvme $(archive) 1800 $(debianversion) bvme $(LINGUA)

rootmvme.bin:	$(shell find scripts/rootdisk -type f ) \
		$(shell find utilities -type f ) \
		rootdisk.sh Makefile #keymaps.tgz
	./rootdisk.sh mvme $(archive) 1800 $(debianversion) mvme $(LINGUA)


# netboot & low memory (not enough space for ramdisk)
root.tar.gz:	root.bin nfsroot.sh Makefile
	./nfsroot.sh root.bin $(debianversion) "" $(LINGUA)

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) ""

lowmematari.bin:	linuxatari sys_mapatari.gz lowmem.sh Makefile \
		lowmemrd.bin
	./lowmem.sh atari $(kernelatari) lowmemrd.bin 1440 $(debianversion) atari

lowmemamiga.bin:	linuxamiga sys_mapamiga.gz lowmem.sh Makefile \
		lowmemrd.bin
	./lowmem.sh amiga $(kernelamiga) lowmemrd.bin 1440 $(debianversion) amiga

#-----------------------------------------------------------------------------
# Rescue
#-----------------------------------------------------------------------------

$(alpha_rescue_targets) : resc1440_%.bin: linuxalpha_% sys_mapalpha_%.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh alpha_$* $(kernelalpha)$*.deb /dev/null 1440 $(debianversion) alpha $(LINGUA)
	mv resc1440alpha_$*.bin $@

#### i386 part
resc1200.bin:	linux sys_map.gz rescue.sh Makefile \
		$(shell find scripts/rescue -type f )
	./rescue.sh "" $(kernel) root.bin 1200 $(debianversion) "" $(LINGUA) $(SFONT)

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) "" $(LINGUA) $(SFONT)

resc1440-safe.bin:	resc1440.bin safe.sh
	./safe.sh resc1440.bin resc1440-safe.bin


#### m68 part
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 $(LINGUA)

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 $(LINGUA)

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 $(LINGUA)

resc1440bvme6000.bin: linuxbvme6000 sys_mapbvme6000.gz rescue.sh Makefile \
		rootbvme.bin $(shell find scripts/rescue -type f )
	./rescue.sh bvme6000 $(kernelbvme6000) rootbvme.bin 1440 $(debianversion) bvme $(LINGUA)

resc1440mvme162.bin: linuxmvme162 sys_mapmvme162.gz rescue.sh Makefile \
		rootmvme.bin $(shell find scripts/rescue -type f )
	./rescue.sh mvme162 $(kernelmvme162) rootmvme.bin 1440 $(debianversion) mvme $(LINGUA)

resc1440mvme167.bin: linuxmvme167 sys_mapmvme167.gz rescue.sh Makefile \
		rootmvme.bin $(shell find scripts/rescue -type f )
	./rescue.sh mvme167 $(kernelmvme167) rootmvme.bin 1440 $(debianversion) mvme $(LINGUA)

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


#### powerpc part
resc1440apus.bin: linuxapus sys_mapapus.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh apus $(kernelapus) root.bin 1440 $(debianversion) apus $(LINGUA)

resc1440chrp.bin: linuxchrp sys_mapchrp.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh chrp $(kernelchrp) root.bin 1440 $(debianversion) chrp $(LINGUA)

resc1440mbx.bin: linuxmbx sys_mapmbx.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh mbx $(kernelmbx) root.bin 1440 $(debianversion) mbx $(LINGUA)

resc1440pmac.bin: linuxpmac sys_mappmac.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh pmac $(kernelpmac) root.bin 1440 $(debianversion) pmac $(LINGUA)

resc1440prep.bin: linuxprep sys_mapprep.gz rescue.sh Makefile \
		root.bin $(shell find scripts/rescue -type f )
	./rescue.sh prep $(kernelprep) root.bin 1440 $(debianversion) prep $(LINGUA)

#### sparc part
resc1440-$(altkver).bin: linux-$(altkver) sys_map-$(altkver).gz rescue.sh Makefile \
		root.bin silo1440k.gz $(shell find scripts/rescue -type f )
	./rescue.sh -$(altkver) $(altkernel) root.bin 1440 $(debianversion) "" $(LINGUA)

resc1440-$(altkver)-sun4u.bin: linux-$(altkver)-sun4u \
		sys_map-$(altkver)-sun4u.gz rescue.sh Makefile \
		root.bin silo1440k-sun4u.gz $(shell find scripts/rescue -type f )
	./rescue.sh -$(altkver)-sun4u $(altkernel64) root.bin 1440 $(debianversion) "" $(LINGUA)


#### Laptop part
resc1440tecra-safe.bin:	resc1440tecra.bin safe.sh
	./safe.sh resc1440tecra.bin resc1440tecra-safe.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) "" $(LINGUA) $(SFONT)


#-----------------------------------------------------------------------------
# TFTP-Boot   - real good setup method, if the system is without an floppy
#-----------------------------------------------------------------------------
# netboot for Sparc architecture
tftpboot:	tftpboot.img tftpboot-$(altkver).img
tftpboot.img:	linux sys_map.gz root.bin tftpboot.sh Makefile
	./tftpboot.sh linux sys_map.gz root.bin $(debianversion)

ifeq ($(architecture), sparc)
tftpboot-$(altkver).img:	linux-$(altkver) linux-$(altkver)-sun4u \
		root.bin Makefile
	zcat linux-$(altkver) > linux-$(altkver).unz
	zcat linux-$(altkver)-sun4u > linux-$(altkver)-sun4u.unz
	tilo linux-$(altkver).unz linux-$(altkver)-sun4u.unz root.bin $@
	rm -f linux-$(altkver).unz linux-$(altkver)-sun4u.unz

# 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=)

silo1440k-sun4u.gz: $(silo_pkg) silo_proto.sh Makefile
	./silo_proto.sh $(archive) $(@:.gz=) sun4u 1440
	rm -f $@
	gzip -9v $(@:.gz=)
endif

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

