# Common configuration and targets for i386 and amd64 systems.

# May be needed in rare cases.
#SYSLINUX_OPTS = -s

# The DOS volume id to use for DOS floppies. This is a 32 bit hexadecimal
# number.
DOS_VOLUME_ID = deb00001
# The DOS volume label to use for DOS floppies. This is a 11 character
# string.
DOS_VOLUME_LABEL := "$(shell echo "$(DISTRIBUTION_NAME) Inst" | cut -b1-11)"

# The image to use for a syslinux splash screen.
#SPLASH_PNG=boot/x86/pics/klowner.png
SPLASH_PNG=boot/x86/pics/ubuntu.png

# The directory boot screens for syslinux will go in.
BOOT_SCREEN_DIR = 

GFXBOOT_LANGS := $(sort en $(basename $(notdir $(wildcard boot/x86/po/*.po))))

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

# Compress binaries to save more space.
# Doesn't really save much since we gzip the image later though.
.PHONY: arch_tree
arch_tree:
ifdef UPX
	for bin in $(UPX_BINS); do \
		$(UPX) -9 $(TREE)/$$bin; \
	done
endif

# Create a bootable image.
.PHONY: arch_boot
arch_boot:
ifdef UPX
	$(UPX) -f -9 $(TEMP_KERNEL) || true
endif
	mkfs.msdos -i $(DOS_VOLUME_ID) -n $(DOS_VOLUME_LABEL) -C $(TEMP_BOOT) $(FLOPPY_SIZE)
	
	# syslinux is used to make the image bootable
	syslinux $(SYSLINUX_OPTS) $(TEMP_BOOT)
	mcopy -i$(TEMP_BOOT) $(TEMP_KERNEL) ::linux
	mcopy -i$(TEMP_BOOT) $(TEMP_INITRD) ::initrd.gz
	
	if [ -n "$(INITRD_GTK)" ]; then \
		mcopy -i$(TEMP_BOOT) $(INITRD_GTK) ::initrdg.gz; \
	fi

	# generate a disk label
	makelabel $(DISK_LABEL) $(BUILD_DATE) > $(TEMP)/disk.lbl
	mcopy -i$(TEMP_BOOT) $(TEMP)/disk.lbl ::disk.lbl

	# syslinux configs
	$(foreach file,$(shell syslinux-cfgs "$(INITRD_GTK)"), \
		outfile=`basename $(file)`; \
		if [ "$(NO_VESAMENU)" ]; then \
			if [ $$outfile = prompt.cfg ]; then \
				outfile=syslinux.cfg; \
			elif [ $$outfile != menu.cfg ] && [ $$outfile != text.cfg ] && [ $$outfile != adtext.cfg ]; then \
				outfile="" ; \
			fi; \
		fi; \
		if [ "$$outfile" ]; then \
			[ "$(BOOTMENU_BEEP)" = y ] && beep="$$(printf '\a')"; \
			cat $(file) \
			| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
				DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
				DEBIAN_VERSION "$(DEBIAN_VERSION)" \
				BUILD_DATE "$(BUILD_DATE)" \
				KERNEL linux INITRD initrd.gz \
				KERNEL_26 linux26 INITRD_26 initrd26.gz \
				INITRD_GTK initrdg.gz \
				VIDEO_MODE $(VIDEO_MODE) \
				VIDEO_MODE_GTK $(VIDEO_MODE_GTK) \
				BEEP "$$beep" \
			| todos | mcopy -i$(TEMP_BOOT) - ::$$outfile; \
		fi; \
	)

	# syslinux help screens
	if [ -z "$(OMIT_BOOT_HELP)" ]; then \
		set -e; \
		$(foreach file,$(wildcard $(TEMP_BOOT_SCREENS)/*.txt), \
			if [ -e $(file).withgtk ] && [ -n "$(INITRD_GTK)" ]; then \
				mcopy -i$(TEMP_BOOT) $(file).withgtk ::`basename $(file)`; \
			else \
				mcopy -i$(TEMP_BOOT) $(file) ::`basename $(file)`; \
			fi; \
		) \
	fi
	
	if [ ! "$(NO_VESAMENU)" ]; then \
		mcopy -i$(TEMP_BOOT) /usr/lib/syslinux/vesamenu.c32 ::vesamenu.c32; \
		if [ -e $(TEMP_BOOT_SCREENS)/splash.png ]; then \
			mcopy -i$(TEMP_BOOT) $(TEMP_BOOT_SCREENS)/splash.png ::splash.png; \
		fi; \
	fi
	
	# win32-loader
	if [ -z "$(OMIT_WIN32_LOADER)" ]; then \
		(mcopy -i$(TEMP_BOOT) /usr/lib/win32-loader/win32-loader.exe	::setup.exe ; \
		mcopy -i$(TEMP_BOOT) /usr/lib/win32-loader/g2ldr		::g2ldr ; \
		mcopy -i$(TEMP_BOOT) /usr/lib/win32-loader/g2ldr.mbr		::g2ldr.mbr ; \
		\
		(echo -en "[installer]\n"; \
		echo -en "arch=$(ARCH)\n"; \
		echo -en "default_desktop=gnome\n"; \
		if [ -n "$(INITRD_GTK)" ]; then \
			echo -en "$(ARCH)/linux=linux\n$(ARCH)/initrd=initrd.gz\n"; \
			echo -en "$(ARCH)/gtk/linux=linux\n$(ARCH)/gtk/initrd=initrdg.gz\n"; \
		else \
			echo -en "user_interface=text\n"; \
			echo -en "$(ARCH)/linux=linux\n$(ARCH)/initrd=initrd.gz\n"; \
		fi; \
		echo -en "\n[grub]\ng2ldr=g2ldr\ng2ldr.mbr=g2ldr.mbr\n") \
			| todos | mcopy -i$(TEMP_BOOT) - ::win32-loader.ini) \
		|| (mdel -i$(TEMP_BOOT) ::setup.exe ::g2ldr ::g2ldr.mbr ::win32-loader.ini ; \
			echo "*** Failed to add win32-loader, probably out of space." >&2) ; \
	fi

	$(if $(GZIPPED),gzip -v9f $(TEMP_BOOT))

# A non-bootable image, as used by the root floppy, is just a bare initrd.
.PHONY: arch_root
arch_root:
	# Pad the initrd to the floppy size.
	dd if=$(TEMP_INITRD) bs=$(shell expr $(FLOPPY_SIZE) \* 1024) count=1 of=$(TEMP_ROOT) conv=sync

# Set up syslinux/elilo help screens.
.PHONY: arch_boot_screens
arch_boot_screens:
	-rm -f $(TEMP_BOOT_SCREENS)/*
	mkdir -p $(TEMP_BOOT_SCREENS)
	$(foreach SCREEN,$(wildcard boot/x86/*.txt boot/x86/*.txt.with* boot/x86/*.txt.live boot/x86/*.msg boot/x86/*.msg.live), \
		bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
			DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
			DEBIAN_VERSION "$(DEBIAN_VERSION)" \
			BUILD_DATE "$(BUILD_DATE)" \
			SYSDIR "$(BOOT_SCREEN_DIR)" \
		       < $(SCREEN) > $(TEMP_BOOT_SCREENS)/`basename $(SCREEN)`\
	;)
	if [ -n "$(SPLASH_PNG)" ]; then \
		cp $(SPLASH_PNG) $(TEMP_BOOT_SCREENS)/splash.png; \
	fi

# isolinux CD info directory, including help screens and an isolinux.cfg.
# Note that the paths in the isolinux.cfg must match the paths used on the
# CD by debian-cd.
# Supply elilo configuration too for Intel-based Macs.
.PHONY: arch_cd_info_dir
arch_cd_info_dir:
	-rm -f $(TEMP_CD_INFO_DIR)/*
	mkdir -p $(TEMP_CD_INFO_DIR)
	
	cp -a $(TEMP_BOOT_SCREENS)/* $(TEMP_CD_INFO_DIR)

	$(foreach file,$(shell syslinux-cfgs "$(INITRD_GTK)"), \
		outfile=`basename $(file)`; \
		if [ $$outfile = syslinux.cfg ]; then \
			outfile=isolinux.cfg; \
		fi; \
		[ "$(BOOTMENU_BEEP)" = y ] && beep="$$(printf '\a')"; \
		cat $(file) \
		| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
			DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
			DEBIAN_VERSION "$(DEBIAN_VERSION)" \
			BUILD_DATE "$(BUILD_DATE)" \
			KERNEL /install/vmlinuz \
			INITRD /install/initrd.gz \
			INITRD_GTK /install/gtk/initrd.gz \
			VIDEO_MODE $(VIDEO_MODE) \
			VIDEO_MODE_GTK $(VIDEO_MODE_GTK) \
			BEEP "$$beep" \
		> $(TEMP_CD_INFO_DIR)/$$outfile; \
	)

	# win32-loader
	if [ -z "$(OMIT_WIN32_LOADER)" ]; then \
		cp /usr/lib/win32-loader/win32-loader.exe	$(TEMP_CD_INFO_DIR)/setup.exe; \
		cp /usr/lib/win32-loader/g2ldr			$(TEMP_CD_INFO_DIR)/g2ldr; \
		cp /usr/lib/win32-loader/g2ldr.mbr		$(TEMP_CD_INFO_DIR)/g2ldr.mbr; \
		\
		(echo -en "[installer]\n"; \
		echo -en "arch=$(ARCH)\n"; \
		echo -en "default_desktop=gnome\n"; \
		if [ -n "$(INITRD_GTK)" ]; then \
			echo -en "$(ARCH)/linux=install/vmlinuz\n$(ARCH)/initrd=install/initrd.gz\n"; \
			echo -en "$(ARCH)/gtk/linux=install/vmlinuz\n$(ARCH)/gtk/initrd=install/gtk/initrd.gz\n"; \
		else \
			echo -en "user_interface=text\n"; \
			echo -en "$(ARCH)/linux=install/vmlinuz\n$(ARCH)/initrd=install/initrd.gz\n"; \
		fi; \
		echo -en "\n[grub]\ng2ldr=g2ldr\ng2ldr.mbr=g2ldr.mbr\n") \
			| todos > $(TEMP_CD_INFO_DIR)/win32-loader.ini; \
	fi

	set -e; for lang in $(GFXBOOT_LANGS); do \
		if [ "$$lang" != en ]; then \
			po4a-translate -f docbook -m boot/x86/help.xml \
				-p boot/x86/po/$$lang.po \
				-l $(TEMP)/help-$$lang.xml; \
			src=$(TEMP)/help-$$lang.xml; \
		else \
			src=boot/x86/help.xml; \
		fi; \
		if [ -e "$$src" ]; then \
			help-to-gfxboot.py $$src install \
			| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
				DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
				DISTRIBUTION_VERSION "$(DEBIAN_VERSION)" \
				BUILD_DATE "$(BUILD_DATE)" \
			> $(TEMP_CD_INFO_DIR)/$$lang.hlp; \
			help-to-gfxboot.py $$src live \
			| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
				DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
				DISTRIBUTION_VERSION "$(DEBIAN_VERSION)" \
				BUILD_DATE "$(BUILD_DATE)" \
			> $(TEMP_CD_INFO_DIR)/$$lang.hlp.live; \
		fi; \
	done

	cat boot/x86/elilo.conf \
	| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
		DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
		DEBIAN_VERSION "$(DEBIAN_VERSION)" \
		BUILD_DATE "$(BUILD_DATE)" \
		SYSDIR "/elilo/" \
		KERNEL /install/vmlinuz INITRD /install/initrd.gz \
	> $(TEMP_CD_INFO_DIR)/elilo.conf

.PHONY: arch_miniiso
arch_miniiso:
	-rm -f $(TEMP_CD_TREE)/*
	mkdir -p $(TEMP_CD_TREE)
	cp /usr/lib/syslinux/isolinux.bin $(TEMP_CD_TREE)
	cp /usr/lib/syslinux/vesamenu.c32 $(TEMP_CD_TREE)

	$(foreach file,$(wildcard boot/x86/*.txt), \
		cat $(file) | \
		bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
			DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
			DEBIAN_VERSION "$(DEBIAN_VERSION)" \
			BUILD_DATE "$(BUILD_DATE)" "" \
		       > $(TEMP_CD_TREE)/`basename $(file)`; \
	)

	if [ -e $(TEMP_BOOT_SCREENS)/splash.png ]; then \
		cp $(TEMP_BOOT_SCREENS)/splash.png $(TEMP_CD_TREE); \
	fi
	ln -f $(TEMP_KERNEL) $(TEMP_CD_TREE)/linux
	ln -f $(TEMP_INITRD) $(TEMP_CD_TREE)/initrd.gz
	$(foreach file,$(shell syslinux-cfgs "$(INITRD_GTK)"), \
		outfile=`basename $(file)`; \
		if [ $$outfile = syslinux.cfg ]; then \
			outfile=isolinux.cfg; \
		fi; \
		[ "$(BOOTMENU_BEEP)" = y ] && beep="$$(printf '\a')"; \
		cat $(file) \
		| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
			DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
			DEBIAN_VERSION "$(DEBIAN_VERSION)" \
			BUILD_DATE "$(BUILD_DATE)" \
			KERNEL linux \
			INITRD initrd.gz \
			INITRD_GTK /install/gtk/initrd.gz \
			VIDEO_MODE $(VIDEO_MODE) \
			VIDEO_MODE_GTK $(VIDEO_MODE_GTK) \
			BEEP "$$beep" \
		| todos > $(TEMP_CD_TREE)/$$outfile; \
	)
	
	# win32-loader
	if [ -z "$(OMIT_WIN32_LOADER)" ]; then \
		cp /usr/lib/win32-loader/win32-loader.exe	$(TEMP_CD_TREE)/setup.exe; \
		cp /usr/lib/win32-loader/g2ldr			$(TEMP_CD_TREE)/g2ldr; \
		cp /usr/lib/win32-loader/g2ldr.mbr		$(TEMP_CD_TREE)/g2ldr.mbr; \
		\
		(echo -en "[installer]\n"; \
		echo -en "arch=$(ARCH)\n"; \
		echo -en "default_desktop=gnome\n"; \
		if [ "$(TYPE)" = "netboot/gtk" ]; then \
			echo -en "user_interface=graphical\n"; \
			echo -en "$(ARCH)/gtk/linux=linux\n$(ARCH)/gtk/initrd=initrd.gz\n"; \
		else \
			echo -en "user_interface=text\n"; \
			echo -en "$(ARCH)/linux=linux\n$(ARCH)/initrd=initrd.gz\n"; \
		fi; \
		echo -en "\n[grub]\ng2ldr=g2ldr\ng2ldr.mbr=g2ldr.mbr\n") \
			| todos > $(TEMP_CD_TREE)/win32-loader.ini; \
	fi

	genisoimage -r -J -b isolinux.bin -c boot.cat \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		-o $(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)/linux
	cp /usr/lib/syslinux/pxelinux.0 $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)
	mkdir -p $(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/pxelinux.cfg
	mkdir -p $(TEMP_NETBOOT_DIR)/$(BOOT_SCREEN_DIR)
	cp /usr/lib/syslinux/vesamenu.c32 $(TEMP_NETBOOT_DIR)/$(BOOT_SCREEN_DIR)

	$(foreach file,$(shell syslinux-cfgs "$(INITRD_GTK)"), \
		outfile=$(TEMP_NETBOOT_DIR)/$(BOOT_SCREEN_DIR)/`basename $(file)`; \
		if [ `basename $(file)` = syslinux.cfg ]; then \
			outfile=$(TEMP_NETBOOT_DIR)/$(NETBOOT_PATH)/pxelinux.cfg/default; \
		fi; \
		[ "$(BOOTMENU_BEEP)" = y ] && beep="$$(printf '\a')"; \
		cat $(file) \
		| bootvars-subst MEDIA_TYPE "$(MEDIA_TYPE)" \
			DISTRIBUTION_NAME "$(DISTRIBUTION_NAME)" \
			DEBIAN_VERSION "$(DEBIAN_VERSION)" \
			BUILD_DATE "$(BUILD_DATE)" \
			SYSDIR "$(BOOT_SCREEN_DIR)" \
			KERNEL $(NETBOOT_PATH)/linux \
			INITRD $(NETBOOT_PATH)/initrd.gz \
			VIDEO_MODE $(VIDEO_MODE) \
			BEEP "$$beep" \
		> $$outfile; \
	)
	
	# This symlink is required as pxelinux always looks in the root
	# of the tftp server for its config.
	rm -f $(TEMP_NETBOOT_DIR)/pxelinux.cfg
	ln -sf $(NETBOOT_PATH)/pxelinux.cfg $(TEMP_NETBOOT_DIR)/pxelinux.cfg
	# This symlink is not strictly required, but it allows more usual
	# dhcp server setups to work without modification.
	rm -f $(TEMP_NETBOOT_DIR)/pxelinux.0
	ln -sf $(NETBOOT_PATH)/pxelinux.0 $(TEMP_NETBOOT_DIR)/pxelinux.0

	set -e; \
	$(foreach file,$(wildcard $(TEMP_BOOT_SCREENS)/*.txt), \
		cp $(file) $(TEMP_NETBOOT_DIR)/$(BOOT_SCREEN_DIR); \
	)

	if [ -n "$(SPLASH_PNG)" ]; then \
		cp $(SPLASH_PNG) $(TEMP_NETBOOT_DIR)/$(BOOT_SCREEN_DIR)/splash.png; \
	fi

.PHONY: xen_config
xen_config:
	sed -e "s/@ARCH@/$(ARCH)/g" \
		<boot/x86/xen/xm-debian.cfg >$(XENCFG)
	chmod 644 $(XENCFG)
	update-manifest $(XENCFG) $(MANIFEST-XENCFG)

