#!/usr/bin/make -f

SHELL := sh -e

%:
	dh ${@}

override_dh_auto_clean:
	dh_auto_clean

	rm -rf build

override_dh_auto_build:
	cp -a themes build

	$(MAKE) -C build/openSUSE/help-install
	$(MAKE) -C build/openSUSE

	for _THEME in $$(cd build && ls); \
	do \
		if [ "$${_THEME}" != "openSUSE" ]; \
		then \
			$(MAKE) -C build/$${_THEME}; \
		fi; \
	done

override_dh_install:
	dh_install

	mkdir -p debian/gfxboot-themes-opensuse/usr/share/gfxboot/themes
	cp -a themes/openSUSE debian/gfxboot-themes-opensuse/usr/share/gfxboot/themes

	for _THEME in $$(cd themes && ls); \
	do \
		if [ "$${_THEME}" != "openSUSE" ]; \
		then \
			_PACKAGE="gfxboot-themes-`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 -; \
			rm -f debian/$${_PACKAGE}/usr/share/gfxboot/themes/$${_THEME}/Makefile; \
			cp -a themes/openSUSE/Makefile debian/$${_PACKAGE}/usr/share/gfxboot/themes/$${_THEME}/Makefile; \
		fi; \
	done

	for _THEME in $$(cd build && ls); \
	do \
		_PACKAGE="gfxboot-themes-`echo $${_THEME} | tr [A-Z] [a-z]`"; \
		install -D -m 0644 build/$${_THEME}/bootlogo debian/$${_PACKAGE}/etc/bootsplash/themes/$${_THEME}/cdrom/bootlogo; \
		/usr/share/gfxboot/bin/unpack_bootlogo debian/$${_PACKAGE}/etc/bootsplash/themes/$${_THEME}/cdrom; \
		mkdir -p debian/$${_PACKAGE}/etc/bootsplash/themes/$${_THEME}/bootloader; \
		cp -a build/$${_THEME}/message build/$${_THEME}/help-boot/*.hlp build/$${_THEME}/po/*.tr debian/$${_PACKAGE}/etc/bootsplash/themes/$${_THEME}/bootloader; \
	done

override_dh_fixperms:
	dh_fixperms

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