MEDIA_TYPE = floppy image

TYPE = floppy

# initramfs takes up way less space
INITRD_FS = initramfs

# Drop all languages from the floppy to save space. If you need a localised
# floppy, modify the list below, adding your language(s) in addition to en.
# for example: DROP_LANG = '(?!en|es|pt_BR)'
DROP_LANG = '(?!en)'

EXTRANAME = floppy/

include config/m68k/tools.cfg

MANIFEST-BOOT-BVME6000 = "m68k floppy bvme6000 boot image"
MANIFEST-ROOT-BVME6000 = "m68k floppy bvme6000 root image"
MANIFEST-BOOT-MVME147  = "m68k floppy mvme147  boot image"
MANIFEST-ROOT-MVME147  = "m68k floppy mvme147  root image"
MANIFEST-BOOT-MVME16X  = "m68k floppy mvme16x  boot image"
MANIFEST-ROOT-MVME16X  = "m68k floppy mvme16x  root image"

MANIFEST-BOOT-ATARI    = "m68k floppy atari    boot image"
MANIFEST-ROOT-ATARI    = "m68k floppy atari    root image"

MANIFEST-CDROM-BVME6000 = "(used to make cdrom bvme6000 bootable)"


# sudo is required for vmelilo, see bts #250578.
ROOTCMD = sudo

# gzip the floppies
GZIPPED = .gz


define vme_boot
	mkdir -p $(TEMP)/$(SUB)boot/etc
	mkdir -p $(TEMP)/$(SUB)boot/boot
	mkdir -p $(TEMP)/mount
	cp $(shell echo $(SUB_KERNEL) | cut -d' ' -f 1) $(TEMP)/$(SUB)boot/linux
	if [ "$(FLOPPY_SIZE)" = "2880" ]; then \
		cp $(TEMP_INITRD) $(TEMP)/$(SUB)boot/root.bin ; \
		cp boot/m68k/vme-vmelilo.conf-cdfloppy $(TEMP)/$(SUB)boot/etc/vmelilo.conf ; \
	else \
		cp boot/m68k/vme-vmelilo.conf-floppy $(TEMP)/$(SUB)boot/etc/vmelilo.conf ; \
	fi ;
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TEMP)/$(SUB)boot/disk.lbl
	genext2fs -d $(TEMP)/$(SUB)boot -b $(FLOPPY_SIZE) -m 0 $(TEMP)/$(SUB)boot.img
	$(ROOTCMD) mount -t ext2 -o loop=/dev/loop3 $(TEMP)/$(SUB)boot.img $(TEMP)/mount
	@echo loopdev $(shell grep $(TEMP)/mount /proc/mounts | cut -d' ' -f 1)
	if $(ROOTCMD) vmelilo -f -a $(VMEARCH) -b /dev/null -w $(TEMP)/mount \
			-d /dev/loop3 ; then \
		$(ROOTCMD) umount $(TEMP)/mount ; \
		$(e2fsck) $(TEMP)/$(SUB)boot.img || true ; \
		$(if $(GZIPPED),gzip -v9f $(TEMP)/$(SUB)boot.img) ; \
	else \
		echo "vmelilo failed, moving $(SUB)boot.img ..." ; \
		$(ROOTCMD) umount $(TEMP)/mount ; \
		mv $(TEMP)/$(SUB)boot.img $(TEMP)/$(SUB)boot.img.failed ; \
	fi ;
	install -m 644 -D $(TEMP)/$(SUB)boot.img$(GZIPPED) $(FINAL_IMAGE)
	update-manifest $(FINAL_IMAGE) $(MANIFEST-INITRD) $(UDEB_LISTS)
endef

#
# vme root
#

define vme_root
	mkdir -p $(TEMP)/$(SUB)root/etc
	cp $(TEMP_INITRD) $(TEMP)/$(SUB)root/root.bin
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TEMP)/$(SUB)root/disk.lbl
	genext2fs -d $(TEMP)/$(SUB)root -b $(FLOPPY_SIZE) -r 0 $(TEMP)/$(SUB)root.img
	$(e2fsck) $(TEMP)/$(SUB)root.img || true
	$(if $(GZIPPED),gzip -v9f $(TEMP)/$(SUB)root.img)
	install -m 644 -D $(TEMP)/$(SUB)root.img$(GZIPPED) $(FINAL_IMAGE)
	update-manifest $(FINAL_IMAGE) $(MANIFEST-INITRD) $(UDEB_LISTS)
endef

#
# atari boot
#

# The DOS volume id to use for DOS floppies: 32 bit hexidecimal number.
DOS_VOLUME_ID = $(shell printf "%08x" $$((${SOURCE_DATE_EPOCH}%4294967296)))
# The DOS volume label to use for DOS floppies: 11 character string.
DOS_VOLUME_LABEL = "Debian Inst"

# Create a bootable image.
#	msdos fat 12, auto/bootstra.prg, bootargs, linux

define atari_boot
	mkfs.msdos --invariant -i $(DOS_VOLUME_ID) -n $(DOS_VOLUME_LABEL) -r 112 -F 12 -C $(TEMP)/$(SUB)boot.img $(FLOPPY_SIZE)
	# mkfs.msdos gets the mode wrong (bug filed)
	chmod 644 $(TEMP)/$(SUB)boot.img

	# generate a disk label
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TEMP)/disk.lbl
	ls -l $(TEMP)/disk.lbl $(shell echo $(TEMP_KERNEL) | cut -d' ' -f 1)

	mcopy -i$(TEMP)/$(SUB)boot.img $(TEMP)/disk.lbl ::disk.lbl
	mcopy -i$(TEMP)/$(SUB)boot.img $(shell echo $(TEMP_KERNEL) | cut -d' ' -f 1) ::/linux

	# atari boot stuff
	mmd -i$(TEMP)/$(SUB)boot.img ::/auto
	mcopy -i$(TEMP)/$(SUB)boot.img $(TEMP)/bootstra.tos ::/auto
	mcopy -i$(TEMP)/$(SUB)boot.img boot/m68k/atari-bootargs-floppy ::/bootargs

	$(if $(GZIPPED),gzip -v9f $(TEMP)/$(SUB)boot.img)

	install -m 644 -D $(TEMP)/$(SUB)boot.img$(GZIPPED) $(FINAL_IMAGE)
	update-manifest $(FINAL_IMAGE) $(MANIFEST-INITRD) $(UDEB_LISTS)
endef

#
# atari root
#

define atari_root
	mkfs.msdos --invariant -i $(DOS_VOLUME_ID) -n $(DOS_VOLUME_LABEL) -r 112 -F 12 -C $(TEMP)/$(SUB)root.img $(FLOPPY_SIZE)
	# mkfs.msdos gets the mode wrong (bug filed)
	chmod 644 $(TEMP)/$(SUB)root.img

	# generate a disk label
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TEMP)/disk.lbl
	ls -l $(TEMP)/disk.lbl $(TEMP_INITRD)

	mcopy -i$(TEMP)/$(SUB)root.img $(TEMP)/disk.lbl ::disk.lbl
	mcopy -i$(TEMP)/$(SUB)root.img $(TEMP_INITRD) ::initrd.gz

	$(if $(GZIPPED),gzip -v9f $(TEMP)/$(SUB)root.img)

	install -m 644 -D $(TEMP)/$(SUB)root.img$(GZIPPED) $(FINAL_IMAGE)
	update-manifest $(FINAL_IMAGE) $(MANIFEST-INITRD) $(UDEB_LISTS)
endef


# there has got to be a better way!

BVMEBOOT = $(SOME_DEST)/$(EXTRANAME)bvme6000-boot.img$(GZIPPED)
BVMEROOT = $(SOME_DEST)/$(EXTRANAME)bvme6000-root.img$(GZIPPED)

$(BVMEBOOT): FLOPPY_SIZE = 1440
$(BVMEBOOT): VMEARCH = bvme
$(BVMEBOOT): SUB = bvme6000-
$(BVMEBOOT): SUB_KERNEL = $(TEMP)/$(KERNELNAME)
$(BVMEBOOT): MANIFEST-INITRD = $(MANIFEST-BOOT-BVME6000)
$(BVMEBOOT): FINAL_IMAGE = $(BVMEBOOT)
$(BVMEBOOT):
	$(vme_boot)

$(BVMEROOT): FLOPPY_SIZE = 1440
$(BVMEROOT): SUB = bvme6000-
$(BVMEROOT): MANIFEST-INITRD = $(MANIFEST-ROOT-BVME6000)
$(BVMEROOT): FINAL_IMAGE = $(BVMEROOT)
$(BVMEROOT):
	$(vme_root)

MVME147BOOT = $(SOME_DEST)/$(EXTRANAME)mvme147-boot.img$(GZIPPED)
MVME147ROOT = $(SOME_DEST)/$(EXTRANAME)mvme147-root.img$(GZIPPED)

$(MVME147BOOT): FLOPPY_SIZE = 1440
$(MVME147BOOT): VMEARCH = mvme
$(MVME147BOOT): SUB = mvme147-
$(MVME147BOOT): SUB_KERNEL = $(TEMP)/$(KERNELNAME)
$(MVME147BOOT): MANIFEST-INITRD = $(MANIFEST-BOOT-MVME147)
$(MVME147BOOT): FINAL_IMAGE = $(MVME147BOOT)
$(MVME147BOOT):
	$(vme_boot)

$(MVME147ROOT): FLOPPY_SIZE = 1440
$(MVME147ROOT): SUB = mvme147-
$(MVME147ROOT): MANIFEST-INITRD = $(MANIFEST-ROOT-MVME147)
$(MVME147ROOT): FINAL_IMAGE = $(MVME147ROOT)
$(MVME147ROOT):
	$(vme_root)

MVME16XBOOT = $(SOME_DEST)/$(EXTRANAME)mvme16x-boot.img$(GZIPPED)
MVME16XROOT = $(SOME_DEST)/$(EXTRANAME)mvme16x-root.img$(GZIPPED)

$(MVME16XBOOT): FLOPPY_SIZE = 1440
$(MVME16XBOOT): VMEARCH = mvme
$(MVME16XBOOT): SUB = mvme16x-
$(MVME16XBOOT): SUB_KERNEL = $(TEMP)/$(KERNELNAME)
$(MVME16XBOOT): MANIFEST-INITRD = $(MANIFEST-BOOT-MVME16X)
$(MVME16XBOOT): FINAL_IMAGE = $(MVME16XBOOT)
$(MVME16XBOOT):
	$(vme_boot)

$(MVME16XROOT): FLOPPY_SIZE = 1440
$(MVME16XROOT): SUB = mvme16x-
$(MVME16XROOT): MANIFEST-INITRD = $(MANIFEST-ROOT-MVME16X)
$(MVME16XROOT): FINAL_IMAGE = $(MVME16XROOT)
$(MVME16XROOT):
	$(vme_root)


#
# Note, we're putting this image in the cdrom directory.
#

BVMECDROM = $(SOME_DEST)/cdrom/bvme6000-boot.img$(GZIPPED)

$(BVMECDROM): EXTRANAME = cdrom/
$(BVMECDROM): FLOPPY_SIZE = 2880
$(BVMECDROM): VMEARCH = bvme
$(BVMECDROM): SUB = bvme6000-cdrom-
$(BVMECDROM): SUB_KERNEL = $(TEMP)/$(KERNELNAME)
$(BVMECDROM): MANIFEST-INITRD = $(MANIFEST-CDROM-BVME6000)
$(BVMECDROM): FINAL_IMAGE = $(BVMECDROM)
$(BVMECDROM):
	$(vme_boot)


ATARIBOOT = $(SOME_DEST)/$(EXTRANAME)atari-boot.img$(GZIPPED)
ATARIROOT = $(SOME_DEST)/$(EXTRANAME)atari-root.img$(GZIPPED)

$(ATARIBOOT): FLOPPY_SIZE = 1440
$(ATARIBOOT): SUB = atari-
$(ATARIBOOT): SUB_KERNEL = $(TEMP)/$(KERNELNAME)
$(ATARIBOOT): MANIFEST-INITRD = $(MANIFEST-BOOT-ATARI)
$(ATARIBOOT): FINAL_IMAGE = $(ATARIBOOT)
$(ATARIBOOT): $(TEMP)/bootstra.tos
	$(atari_boot)

$(ATARIROOT): FLOPPY_SIZE = 1440
$(ATARIROOT): SUB = atari-
$(ATARIROOT): MANIFEST-INITRD = $(MANIFEST-ROOT-ATARI)
$(ATARIROOT): FINAL_IMAGE = $(ATARIROOT)
$(ATARIROOT):
	$(atari_root)


TARGET = $(TEMP_INITRD) $(ATARIBOOT) $(ATARIROOT)
# vme boot floppies need root to build, so cannot be built by default
# $(BVMECDROM) $(BVMEROOT) $(BVMEBOOT) $(MVME147BOOT) $(MVME147ROOT) $(MVME16XBOOT) $(MVME16XROOT)
