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"

.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
	tar -C $(TEMP)/hd-media -zcf $(TEMP)/hd-media.tar.gz boot.scr initrd.gz vmlinuz dtbs/
	mv  $(TEMP)/hd-media.tar.gz $(SOME_DEST)/$(EXTRANAME)

.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 $(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)/"
