FLAVOUR_SUPPORTED = ""
FLOPPY_SIZE = 150000

GZIPPED = .gz
EXTRANAME = hd-media/

CONCATENATEABLE_SUFFIX =

TARGET = $(KERNEL) $(INITRD) hd-media_bootscript hd-media_tarball hd-media_images_concatenateable

MANIFEST-INITRD = "Initrd for use on USB memory sticks"
MANIFEST-KERNEL = "Kernel for use on USB memory sticks"

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

.PHONY: hd-media_bootscript
hd-media_bootscript:
	mkimage -T script -A arm -d boot/arm/bootscr.mainline_common $(SOME_DEST)/$(EXTRANAME)boot.scr
	update-manifest $(SOME_DEST)/$(EXTRANAME)boot.scr "Universal boot script for mainline u-boot (>= v2014.10)"

.PHONY: hd-media_tarball
hd-media_tarball: $(KERNEL) $(INITRD) $(TEMP_DTBS) hd-media_bootscript
	-rm -rf $(TEMP)/hd-media
	mkdir $(TEMP)/hd-media
	cp $(KERNEL) $(TEMP)/hd-media/vmlinuz
	cp $(INITRD) $(TEMP)/hd-media/initrd.gz
	cp $(SOME_DEST)/$(EXTRANAME)boot.scr $(TEMP)/hd-media/boot.scr
	cp -r $(TEMP_DTBS) $(TEMP)/hd-media/dtbs/
	cp boot/README.device-tree $(TEMP)/hd-media/dtbs/README
	gen-tarball $(TEMP)/hd-media $(SOME_DEST)/$(EXTRANAME)/hd-media.tar.gz

.PHONY: hd-media_image_olpc_xo
hd-media_image_olpc_xo: boot/arm/olpc.fth
	dd if=/dev/zero bs=512 seek=1985 count=0 of=$(TEMP)/OLPC_XO.boot
	mkfs.msdos -v --invariant -i $(DOS_VOLUME_ID) -n $(DOS_VOLUME_LABEL) $(TEMP)/OLPC_XO.boot
	mmd -i $(TEMP)/OLPC_XO.boot boot
	mcopy -s -i $(TEMP)/OLPC_XO.boot boot/arm/olpc.fth ::boot
	mkdir -p $(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)
	gen-hd-image -v -z -p 32768 -b firmware -d 0x0e -s "$(FLOPPY_SIZE)" -o "$(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)/firmware.OLPC_XO.img" $(TEMP)/OLPC_XO.boot 63

.PHONY: hd-media_images_concatenateable
hd-media_images_concatenateable: $(KERNEL) $(INITRD) $(TEMP_DTBS) hd-media_bootscript hd-media_image_olpc_xo
	-rm -rf $(TEMP)/hd-media_images_concatenateable
	mkdir $(TEMP)/hd-media_images_concatenateable
	cp $(KERNEL) $(TEMP)/hd-media_images_concatenateable/vmlinuz
	cp $(INITRD) $(TEMP)/hd-media_images_concatenateable/initrd.gz
	cp $(SOME_DEST)/$(EXTRANAME)boot.scr $(TEMP)/hd-media_images_concatenateable/boot.scr
	cp -r $(TEMP_DTBS) $(TEMP)/hd-media_images_concatenateable/dtbs/
	cp boot/README.device-tree $(TEMP)/hd-media_images_concatenateable/dtbs/README
	mkdir -p $(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)
	set -e; while read LINE; \
	do \
	  if [ -n "$${LINE}" ] && ! echo $${LINE}|grep -q -e "^#"; then \
	    set -- $${LINE} ;\
	    gen-hd-image -v -z -p 32768 -b firmware -s "$(FLOPPY_SIZE)" -o "$(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)/firmware.$${1}.img" "$$2" "$$3" "$$4" "$$5" ;\
	  fi ;\
	done < boot/arm/u-boot-image-config
	gen-hd-image -v -z -p 32768 -b firmware -s "$(FLOPPY_SIZE)" -o "$(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)/firmware.none.img"
	gen-hd-image -v -z -p 32768 -b partition -s "$(FLOPPY_SIZE)" -i "$(TEMP)/hd-media_images_concatenateable" -o "$(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)/partition.img"
	cp boot/README.concatenateable_images "$(SOME_DEST)/$(EXTRANAME)/SD-card-images/$(CONCATENATEABLE_SUFFIX)/"
