#!/usr/bin/make -f

theme := debian/grub-theme-whitesur/boot/grub/themes/whitesur
build := $(CURDIR)/build
# upstream's render-assets.sh copies these icons under a second name after rendering
aliases := archlinux:arch gnu-linux:linux gnu-linux:unknown gnu-linux:lfs \
	manjaro:Manjaro.i686 manjaro:Manjaro.x86_64 manjaro:manjarolinux \
	pop-os:pop driver:memtest

# inkscape writes its preferences under HOME, which a build may not own
export HOME := $(CURDIR)/debian/.home

%:
	dh $@

# upstream's render-assets.sh and install.sh at the 1080p setting: the icons,
# the selection pixmaps and the info bar at 96 dpi, with one inkscape per
# source file, and the background from its vector source at 1920x1080. The
# macosx icon is Apple's logo and is not rendered.
override_dh_auto_build:
	mkdir -p $(build)/icons $(HOME)
	actions=; for id in $$(grep -v -x -e macosx assets/logos.txt); do \
		actions="$$actions export-id:$$id;export-id-only;export-dpi:96;export-filename:$(build)/icons/$$id.png;export-do;"; \
	done; \
	inkscape --actions="$$actions" assets/logos-whitesur.svg
	for alias in $(aliases); do \
		cp "$(build)/icons/$${alias%%:*}.png" "$(build)/icons/$${alias##*:}.png" || exit 1; \
	done
	actions=; for id in $$(cat assets/select.txt); do \
		actions="$$actions export-id:$$id;export-id-only;export-dpi:96;export-filename:$(build)/$$id.png;export-do;"; \
	done; \
	inkscape --actions="$$actions" assets/select.svg
	inkscape --export-dpi=96 --export-filename=$(build)/info.png assets/info.svg
	inkscape --export-width=1920 --export-height=1080 --export-filename=$(build)/background.png backgrounds/WhiteSur.svg
	convert $(build)/background.png -strip -quality 92 $(build)/background.jpg
	for size in 12 14 16 24 32 48; do \
		grub-mkfont --output=$(build)/dejavu_sans_$$size.pf2 --size=$$size common/DejaVuSans.ttf || exit 1; \
	done
	for id in $$(grep -v -x -e macosx assets/logos.txt) $$(cat assets/select.txt); do \
		test -s "$(build)/icons/$$id.png" || test -s "$(build)/$$id.png" || { echo "$$id did not render" >&2; exit 1; }; \
	done

override_dh_auto_install:
	install -d $(theme)/icons
	install -m 0644 config/theme-1080p.txt $(theme)/theme.txt
	install -m 0644 $(build)/background.jpg $(build)/info.png $(build)/select_*.png $(theme)/
	install -m 0644 $(build)/dejavu_sans_*.pf2 common/terminus-*.pf2 $(theme)/
	install -m 0644 $(build)/icons/*.png $(theme)/icons/
