#!/usr/bin/make -f
# DH_VERBOSE=1

SHELL := sh -e

%:
	dh ${@} --with quilt

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build

override_dh_auto_build:
	### grub ###
	for _THEME in $$(cd grub && ls); do \
	    inkscape -z --export-width=800 --export-height=600 --export-dpi=72 \
	        --export-png=grub/$${_THEME}/background.png \
	        grub/$${_THEME}/background.svg; \
	    pngtopnm grub/$${_THEME}/background.png | cjpeg -optimize -quality 95 > grub/$${_THEME}/background.jpg; \
	    $(RM) grub/$${_THEME}/background.png; \
	    $(RM) grub/$${_THEME}/background.svg; \
	done
	cp -a themes build
	$(MAKE) -C build/openSUSE/help-install
	for _THEME in $$(cd build && ls); do \
		if [ "$${_THEME}" != "openSUSE" ]; then \
			inkscape -z \
				--export-width=800 \
				--export-height=600 \
				--export-dpi=72 \
				--export-png=build/$${_THEME}/data-install/welcome.png \
					build/$${_THEME}/data-install/welcome.svg; \
			pngtopnm build/$${_THEME}/data-install/welcome.png | cjpeg -optimize -quality 95 > build/$${_THEME}/data-install/welcome.jpg; \
			inkscape -z \
				--export-width=146 \
				--export-height=240 \
				--export-dpi=72 \
				--export-png=build/$${_THEME}/data-install/text.png \
					build/$${_THEME}/data-install/text.svg; \
			pngtopnm build/$${_THEME}/data-install/text.png | cjpeg -optimize -quality 95 > build/$${_THEME}/data-install/text.jpg; \
			inkscape -z \
				--export-width=800 \
				--export-height=600 \
				--export-dpi=72 \
				--export-png=build/$${_THEME}/data-install/back.png \
					build/$${_THEME}/data-install/back.svg; \
			pngtopnm build/$${_THEME}/data-install/back.png | cjpeg -optimize -quality 95 > build/$${_THEME}/data-install/back.jpg; \
			find build/$${_THEME} -name welcome.png | xargs rm -fv; \
			find build/$${_THEME} -name text.png | xargs rm -fv; \
			find build/$${_THEME} -name back.png | xargs rm -fv; \
			find build/$${_THEME} -name background.jpg | xargs rm -fv ;\
			find build/$${_THEME} -name \*.svg | xargs rm -fv; \
			$(MAKE) -C build/$${_THEME}; \
		fi; \
	done

override_dh_install:
	dh_install
	for _THEME in $$(cd build && ls); do \
		if [ "$${_THEME}" != "openSUSE" ]; then \
			PACKAGE="paintitblack-`echo $${_THEME} | tr [A-Z] [a-z]`"; \
			mkdir -p debian/$${PACKAGE}/usr/share/gfxboot/themes; \
			cp -a themes/openSUSE debian/$${PACKAGE}/usr/share/gfxboot/themes/$${_THEME}; \
			cd themes/$${_THEME}; \
			find . | cpio -dmpu --no-preserve-owner ../../debian/$${PACKAGE}/usr/share/gfxboot/themes/$${_THEME}; \
			cd -; \
			install -D -m 0644 build/$${_THEME}/bootlogo debian/$${PACKAGE}-core/etc/bootsplash/themes/$${_THEME}/cdrom/bootlogo; \
			/usr/share/gfxboot/bin/unpack_bootlogo debian/$${PACKAGE}-core/etc/bootsplash/themes/$${_THEME}/cdrom; \
			mkdir -p debian/$${PACKAGE}-core/etc/bootsplash/themes/$${_THEME}/bootloader; \
			cp -a build/$${_THEME}/message build/$${_THEME}/help-boot/*.hlp build/$${_THEME}/po/*.tr debian/$${PACKAGE}-core/etc/bootsplash/themes/$${_THEME}/bootloader; \
		fi; \
	done

override_dh_fixperms:
	dh_fixperms
	find . debian -name "*.xcf" | xargs chmod 0644

override_dh_builddeb:
	dh_builddeb  -- -Zxz -z9
