# The codename of the Debian release that should be installed by default.
DEBIAN_RELEASE = unstable

KEYRING = /usr/share/keyrings/debian-ports-archive-keyring.gpg

# Filesystem type for the initrd.
INITRD_FS = ufs1

# 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/kfreebsd/grub-kfreebsd-cdrom.cfg
GRUB_CFG_PXE=boot/kfreebsd/grub-kfreebsd-pxe.cfg

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

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

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

	# win32-loader
	if [ -z "$(OMIT_WIN32_LOADER)" ]; then \
		cp /usr/share/win32/win32-loader.exe		$(TEMP_CD_INFO_DIR)/setup.exe; \
		cp /usr/share/win32/win32-loader/g2ldr		$(TEMP_CD_INFO_DIR)/g2ldr; \
		cp /usr/share/win32/win32-loader/g2ldr.mbr	$(TEMP_CD_INFO_DIR)/g2ldr.mbr; \
		\
		(printf "[installer]\n"; \
		printf "kernel=kfreebsd\n"; \
		printf "arch=$(subst kfreebsd-,,$(ARCH))\n"; \
		#if [ -n "$(INITRD_GTK)" ]; then \
		#	printf "$(ARCH)/kfreebsd=boot/kernel/kfreebsd.gz\n$(ARCH)/kfreebsd_module=boot/mfsroot.gz\n"; \
		#	printf "$(ARCH)/gtk/kfreebsd=boot/kernel/kfreebsd.gz\n$(ARCH)/gtk/kfreebsd_module=boot/gtk/mfsroot.gz\n"; \
		#else \
			printf "user_interface=text\n"; \
			printf "$(ARCH)/kfreebsd=boot/kernel/kfreebsd.gz\n$(ARCH)/kfreebsd_module=boot/mfsroot.gz\n"; \
		#fi; \
		printf "\n[grub]\ng2ldr=g2ldr\ng2ldr.mbr=g2ldr.mbr\n") \
			| todos > $(TEMP_CD_INFO_DIR)/win32-loader.ini; \
	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/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 \
		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

	# win32-loader
	if [ -z "$(OMIT_WIN32_LOADER)" ]; then \
		cp /usr/share/win32/win32-loader.exe		$(TEMP_CD_TREE)/setup.exe; \
		cp /usr/share/win32/win32-loader/g2ldr		$(TEMP_CD_TREE)/g2ldr; \
		cp /usr/share/win32/win32-loader/g2ldr.mbr	$(TEMP_CD_TREE)/g2ldr.mbr; \
		\
		(printf "[installer]\n"; \
		printf "kernel=kfreebsd\n"; \
		printf "arch=$(subst kfreebsd-,,$(ARCH))\n"; \
		if [ "$(TYPE)" = "netboot/gtk" ]; then \
			printf "user_interface=graphical\n"; \
			printf "$(ARCH)/gtk/kfreebsd=boot/kernel/kfreebsd.gz\n$(ARCH)/gtk/kfreebsd_module=boot/mfsroot.gz\n"; \
		else \
			printf "user_interface=text\n"; \
			printf "$(ARCH)/kfreebsd=boot/kernel/kfreebsd.gz\n$(ARCH)/kfreebsd_module=boot/mfsroot.gz\n"; \
		fi; \
		printf "\n[grub]\ng2ldr=g2ldr\ng2ldr.mbr=g2ldr.mbr\n") \
			| todos > $(TEMP_CD_TREE)/win32-loader.ini; \
	fi

	# Create the ISO with Joliet extensions, needed for win32-loader.ini
	grub-mkrescue --output=$(TEMP_MINIISO) $(TEMP_CD_TREE) -J

# 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" \
	    -e "s/@KERNEL@/$(notdir $(TEMP_KERNEL))/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)
	sed -e "s/@ARCH@/$(ARCH)/g" \
	    -e "s/@XEN_INSTALL_METHOD@/$(XEN_INSTALL_METHOD)/g" \
	    -e "s/@DEBIAN_RELEASE@/$(DEBIAN_RELEASE)/g" \
	    <boot/kfreebsd/xen/debian.cfg >$(XENCFG)
	chmod 644 $(XENCFG)
	update-manifest $(XENCFG) $(MANIFEST-XENCFG)
