MEDIA_TYPE = netboot image

CONCATENATEABLE_SUFFIX =
TFTP_INSTALLER_PATH = debian-installer/armhf/

TARGET = $(KERNEL) $(INITRD) $(MINIISO) netboot_bootscript_sd netboot_bootscript_tftp netboot_tarball netboot_images_concatenateable

EXTRANAME = $(MEDIUM)/

MANIFEST-INITRD = "netboot initrd"
MANIFEST-KERNEL = "kernel image to netboot"
MANIFEST-MINIISO = "tiny CD image that boots the netboot installer"

FLOPPY_SIZE = 150000

GZIPPED = .gz

.PHONY: netboot_bootscript_sd
netboot_bootscript_sd:
	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: netboot_bootscript_tftp
netboot_bootscript_tftp:
	mkimage -T script -A arm -d boot/arm/bootscr.tftpboot $(SOME_DEST)/$(EXTRANAME)tftpboot.scr
	update-manifest $(SOME_DEST)/$(EXTRANAME)tftpboot.scr "TFTP boot script for mainline u-boot (>= v2014.10)"

.PHONY: netboot_tarball
netboot_tarball: $(KERNEL) $(INITRD) $(TEMP_DTBS) netboot_bootscript_tftp
	rm -rf $(TEMP)/netboot_tarball
	mkdir -p $(TEMP)/netboot_tarball/$(TFTP_INSTALLER_PATH)
	echo 'Debian version:  $(DEBIAN_VERSION)' > $(TEMP)/netboot_tarball/version.info
	echo 'Installer build: $(BUILD_DATE)' >> $(TEMP)/netboot_tarball/version.info
	cp $(KERNEL) $(TEMP)/netboot_tarball/$(TFTP_INSTALLER_PATH)vmlinuz
	cp $(INITRD) $(TEMP)/netboot_tarball/$(TFTP_INSTALLER_PATH)initrd.gz
	cp -r $(TEMP_DTBS) $(TEMP)/netboot_tarball/$(TFTP_INSTALLER_PATH)dtbs/
	cp $(SOME_DEST)/$(EXTRANAME)tftpboot.scr $(TEMP)/netboot_tarball/$(TFTP_INSTALLER_PATH)
	( cd $(TEMP)/netboot_tarball; ln -s $(TFTP_INSTALLER_PATH)tftpboot.scr boot.scr.uimg )
	tar -C $(TEMP)/netboot_tarball/ -zcf $(SOME_DEST)/$(EXTRANAME)netboot.tar.gz ./$(TFTP_INSTALLER_PATH)tftpboot.scr ./$(TFTP_INSTALLER_PATH)initrd.gz ./$(TFTP_INSTALLER_PATH)vmlinuz ./$(TFTP_INSTALLER_PATH)dtbs/ ./boot.scr.uimg ./version.info

.PHONY: netboot_images_concatenateable
netboot_images_concatenateable: $(KERNEL) $(INITRD) $(TEMP_DTBS) netboot_bootscript_sd
	-rm -rf $(TEMP)/netboot_images_concatenateable
	mkdir $(TEMP)/netboot_images_concatenateable
	cp $(KERNEL) $(TEMP)/netboot_images_concatenateable/vmlinuz
	cp $(INITRD) $(TEMP)/netboot_images_concatenateable/initrd.gz
	cp $(SOME_DEST)/$(EXTRANAME)boot.scr $(TEMP)/netboot_images_concatenateable/boot.scr
	cp -r $(TEMP_DTBS) $(TEMP)/netboot_images_concatenateable/dtbs/
	cp boot/README.device-tree $(TEMP)/netboot_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)/netboot_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)/"
