# Filesystem type for the initrd.
INITRD_FS = ufs1

# The image to use for a syslinux splash screen.
SPLASH_PNG=boot/kfreebsd/pics/klowner-kbsd.png

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

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

# GRUB modules
GRUB_MODDIR=/usr/lib/grub/i386-pc
GRUB_MODULES=bsd cpuid echo gfxterm minicmd normal png vbe
GRUB_MODULES_PXE=pxe pxecmd
GRUB_MODULES_CDROM=biosdisk chain iso9660

# Unsupported targets
arch_boot_screens:
arch_tree:

# 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.
.PHONY: arch_cd_info_dir
arch_cd_info_dir:
	-rm -f $(TEMP_CD_INFO_DIR)/*
	mkdir -p $(TEMP_CD_INFO_DIR)

	mkdir -p $(TEMP_CD_INFO_DIR)/boot/grub
	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 \
		cp $(SPLASH_PNG) $(TEMP_CD_INFO_DIR)/boot/grub/splash.png; \
	fi

	grub-mkimage -O i386-pc -o $(TEMP_CD_INFO_DIR)/boot/grub/core.img $(GRUB_MODULES) $(GRUB_MODULES_CDROM)
	cat $(GRUB_MODDIR)/cdboot.img $(TEMP_CD_INFO_DIR)/boot/grub/core.img \
		> $(TEMP_CD_INFO_DIR)/boot/grub/grub_eltorito
	rm $(TEMP_CD_INFO_DIR)/boot/grub/core.img

# 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/mfsroot.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 \
		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 \
		cp $(SPLASH_PNG) $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/splash.png; \
	fi

	# Generate grub2pxe
	grub-mkimage -O i386-pc --prefix="(pxe)/$(NETBOOT_PATH)" \
		-o $(TEMP_NETBOOT_DIR)/core.img \
		$(GRUB_MODULES) $(GRUB_MODULES_PXE)
	# workaround a gPXE bug
	sed -i -e 's/\x02\xb0\xad\x1b/\x03\xb0\xad\x1b/' $(TEMP_NETBOOT_DIR)/core.img
	cat $(GRUB_MODDIR)/pxeboot.img $(TEMP_NETBOOT_DIR)/core.img \
		> $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/grub2pxe
	rm $(TEMP_NETBOOT_DIR)/core.img
        
	# 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


