# Filesystem type for the initrd.
INITRD_FS = ext2

ifeq ($(targetstring),monolithic)
# Keep just 16MiB for monolithic builds
  extra_sz=4000
else
# Need a lot more room for udebs for network and CD builds
# TODO: fix & use tmpfs instead
  extra_sz=18000
endif

# We have to redefine genext2fs to use 4k blocks and leave some space
define genext2fs
  nb=$$(find $(TREE) | wc -l); \
  sz=$$(du -s $(TREE) | cut -f 1); \
  genext2fs -d $(TREE) -B 4096 -o hurd -m 0 \
	-b $$(expr $$sz / 4 + $$nb / 2 + $(extra_sz)) \
	-N $$(expr $$nb + 1024)
endef

# The image to use for a syslinux splash screen.
SPLASH_PNG=boot/artwork/11-homeworld/homeworld.png
SPLASH_SVG=$(patsubst %.png,%.svg,$(SPLASH_PNG))

# The font to load in GRUB
GRUB_FONT=/usr/share/grub/ascii.pf2

# GRUB configuration files
GRUB_CFG_CDROM=boot/hurd/grub-hurd-cdrom.cfg
GRUB_CFG_PXE=boot/hurd/grub-hurd-pxe.cfg

# GRUB modules
GRUB_MODDIR=/usr/lib/grub/$(GRUB_PLATFORM)
GRUB_MODULES_PXE=pxe tftp multiboot cpuid echo play gfxterm gfxterm_background gzio minicmd normal png vbe

# Location for Xen example configuration.
XENCFG = $(SOME_DEST)/$(EXTRANAME)debian.cfg

# fakeroot-hurd does not support -s and -i
ifneq ($(shell id -u),0)
  ROOTCMD = fakeroot-tcp
endif

# Extract GRUB EFI files.
.PHONY: x86_grub_efi
x86_grub_efi:
ifeq ($(GRUB_EFI),y)
	efi-image -o $(TEMP_GRUB_EFI) -g $(GRUB_EFI_PLATFORM) \
		-e $(GRUB_EFI_NAME) -n $(NETBOOT_PATH) \
		-s $(EFI_SIGNED)
endif

# Unsupported targets
arch_tree:
arch_boot:
arch_boot_screens:

# genisoimage CD info directory, including grub and configuration files
# Note that the configuration is extensively manipulated by debian-cd to
# support all the available CD/DVD variants.
# Supply GRUB EFI configuration.
.PHONY: arch_cd_info_dir
arch_cd_info_dir: x86_grub_efi
	x86-image $(TEMP_CD_INFO_DIR) $(GRUB_PLATFORM)

	sed -e "s/@ARCH@/$(ARCH)/g" $(GRUB_CFG_CDROM) \
		> $(TEMP_CD_INFO_DIR)/boot/grub/grub.cfg
	if [ -n "$(GRUB_FONT)" ] ; then \
		cp $(GRUB_FONT) $(TEMP_CD_INFO_DIR)/boot/grub/font.pf2; \
	fi
	if [ -n "$(SPLASH_PNG)" ]; then \
		if [ -n "$(SPLASH_SVG)" -a -f "$(SPLASH_SVG)" ]; then \
			rsvg-convert $(SPLASH_SVG) > $(SPLASH_PNG); \
		fi; \
		cp $(SPLASH_PNG) $(TEMP_CD_INFO_DIR)/boot/grub/splash.png; \
	fi

	if [ "$(GRUB_EFI)" = y ]; then \
		set -e; \
		mkdir -p $(TEMP_CD_INFO_DIR)/grub/$(GRUB_EFI_PLATFORM); \
		cp -a $(TEMP_GRUB_EFI)/efi.img $(TEMP_CD_INFO_DIR)/grub/; \
		cp boot/hurd/grub-hurd-efi.cfg \
		  $(TEMP_CD_INFO_DIR)/grub/grub.cfg; \
		cp -a $(GRUB_FONT) $(TEMP_CD_INFO_DIR)/grub/font.pf2; \
		cp -a $(TEMP_GRUB_EFI)/boot/grub/$(GRUB_EFI_PLATFORM)/* \
			$(TEMP_CD_INFO_DIR)/grub/$(GRUB_EFI_PLATFORM)/; \
	fi

# Miniature CD image using GRUB, with only an initrd, no udebs or debs.
.PHONY: arch_miniiso
arch_miniiso: $(TEMP_INITRD) $(TEMP_KERNEL) $(TREE)
	-rm -f $(TEMP_CD_TREE)/*
	mkdir -p $(TEMP_CD_TREE)/boot/kernel
	mkdir -p $(TEMP_CD_TREE)/boot/grub

	cp $(TEMP_KERNEL)		$(TEMP_CD_TREE)/boot/kernel/
	cp $(TEMP_INITRD)		$(TEMP_CD_TREE)/boot/initrd.gz
	sed -e "s/@ARCH@/$(ARCH)/g" $(GRUB_CFG_CDROM) \
		> $(TEMP_CD_TREE)/boot/grub/grub.cfg
	if [ -n "$(GRUB_FONT)" ] ; then \
		cp $(GRUB_FONT) $(TEMP_CD_TREE)/boot/grub/font.pf2; \
	fi
	if [ -n "$(SPLASH_PNG)" ]; then \
		if [ -n "$(SPLASH_SVG)" -a -f "$(SPLASH_SVG)" ]; then \
			rsvg-convert $(SPLASH_SVG) > $(SPLASH_PNG); \
		fi; \
		cp $(SPLASH_PNG) $(TEMP_CD_TREE)/boot/grub/splash.png; \
	fi

	grub-mkrescue --output=$(TEMP_MINIISO) $(TEMP_CD_TREE)

# Netboot files
.PHONY: arch_netboot_dir
arch_netboot_dir:
	-rm -f $(TEMP_NETBOOT_DIR)
	mkdir -p $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)
	cp $(TEMP_INITRD) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)
	cp $(TEMP_KERNEL) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)
	
	sed -e "s/@ARCH@/$(ARCH)/g" $(GRUB_CFG_PXE) \
		> $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub.cfg
	if [ -n "$(GRUB_FONT)" ] ; then \
		cp $(GRUB_FONT) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/font.pf2; \
	fi
	if [ -n "$(SPLASH_PNG)" ]; then \
		if [ -n "$(SPLASH_SVG)" -a -f "$(SPLASH_SVG)" ]; then \
			rsvg-convert $(SPLASH_SVG) > $(SPLASH_PNG); \
		fi; \
		cp $(SPLASH_PNG) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/splash.png; \
	fi

	# Generate grub2pxe
	grub-mkimage -O $(GRUB_PLATFORM)-pxe --prefix="(pxe)/$(NETBOOT_PATH)" \
		-o $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub2pxe \
		$(GRUB_MODULES_PXE)
        
	# This symlink is not strictly required, but it allows more usual
	# dhcp server setups to work without modification.
	rm -f $(TEMP_NETBOOT_DIR)/grub2pxe
	ln -sf $(NETBOOT_PATH)/grub2pxe $(TEMP_NETBOOT_DIR)/grub2pxe


.PHONY: xen_config
xen_config: $(TEMP_INITRD)
	util/xen/build_modules $(SOME_DEST)/$(EXTRANAME)/modules $(TEMP_INITRD)  'initrd $$(ramdisk-create)' $(TEMP)/ext2fs.static 'ext2fs -A --multiboot-command-line=$${kernel-command-line} --host-priv-port=$${host-port} --device-master-port=$${device-port} --exec-server-task=$${exec-task} -T typed $${root} $$(task-create) $$(task-resume)' $(TEMP)/exec.static 'exec $$(exec-task=task-create)'
	sed -e "s/@ARCH@/$(ARCH)/g" \
	    -e "s/@XEN_INSTALL_METHOD@/$(XEN_INSTALL_METHOD)/g" \
	    -e "s/@DEBIAN_RELEASE@/$(DEBIAN_RELEASE)/g" \
	    <boot/hurd/xen/debian.cfg >$(XENCFG)
	chmod 644 $(XENCFG)
	update-manifest $(XENCFG) $(MANIFEST-XENCFG)

