#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Ensure at build time that the library has no dependencies on undefined
# symbols, and speed up loading.
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,defs

# Build with -Werror
DEB_CFLAGS_MAINT_APPEND  := -Werror -Wno-error=unused-result \
       -Wno-error=sign-compare

# Disable optimisations if noopt found in $DEB_BUILD_OPTIONS
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEB_LDFLAGS_MAINT_APPEND += -Wl,-O0
else
	DEB_LDFLAGS_MAINT_APPEND += -Wl,-O1
endif

export systemdsystemunitdir=$(shell pkg-config --variable=systemdsystemunitdir systemd | sed s,^/,,)

%:
	dh ${@}

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dbackground-color=0x000000 \
		-Dbackground-start-color-stop=0x000000 \
		-Dbackground-end-color-stop=0x000000 \
		-Dlogo=/usr/share/plymouth/trisquel-logo.png \
		-Drelease-file=/etc/os-release

override_dh_install:
	# remove non alternative-based theme policy files and tools
	rm -f debian/tmp/etc/plymouth/plymouthd.conf
	rm -f debian/tmp/usr/share/plymouth/plymouthd.defaults
	rm -f debian/tmp/usr/sbin/plymouth-set-default-theme
	rm -f debian/tmp/usr/share/man/man1/plymouth-set-default-theme.1

	dh_install

	# Adding other debian specific files
	install -D -m 0755 debian/local/plymouth-update-initrd debian/plymouth/usr/libexec/plymouth/plymouth-update-initrd
	install -D -m 0644 debian/local/plymouth.lsb debian/plymouth/usr/lib/lsb/init-functions.d/99-plymouth

	# Adding apport file (from Ubuntu)
	mkdir -p debian/plymouth/usr/share/apport/package-hooks
	install -m 644 debian/source_plymouth.apport debian/plymouth/usr/share/apport/package-hooks/source_plymouth.py

	# Install the ubuntu spinner assets
	cp debian/local/spinner/* debian/plymouth-theme-spinner/usr/share/plymouth/themes/spinner
	cp debian/local/entry.png debian/plymouth-theme-spinner/usr/share/plymouth/themes/spinner
	cp debian/local/lock.png debian/plymouth-theme-spinner/usr/share/plymouth/themes/spinner
	cp debian/local/keyboard.png debian/plymouth-theme-spinner/usr/share/plymouth/themes/spinner
	cp debian/local/bgrt-fallback.png debian/plymouth-theme-spinner/usr/share/plymouth/themes/spinner


override_dh_auto_install:

	# Workaround a but in meson, it cannot create a symlink if the target doesn't exist
	# https://github.com/mesonbuild/meson/issues/12253
	install -D -m 0644 debian/local/trisquel-logo.png debian/plymouth/usr/share/plymouth/trisquel-logo.png
	install -D -m 0644 debian/local/trisquel-logo.png debian/plymouth/usr/share/plymouth/themes/spinner/watermark.png
	install -D -m 0644 debian/local/trisquel-logo.png debian/plymouth-themes/usr/share/plymouth/themes/spinfinity/header-image.png
	dh_auto_install

# Disable dh_installinitramfs as we are managing this ourself
override_dh_installinitramfs:
	# Adding initramfs-tools integration
	install -D -m 0755 debian/local/plymouth.hook debian/plymouth/usr/share/initramfs-tools/hooks/plymouth
	sed -i -e 's|@DEB_HOST_MULTIARCH@|$(DEB_HOST_MULTIARCH)|g' debian/plymouth/usr/share/initramfs-tools/hooks/plymouth
	install -D -m 0644 debian/local/plymouth.conf debian/plymouth/usr/share/initramfs-tools/conf-hooks.d/plymouth
	install -D -m 0755 debian/local/plymouth.init-premount debian/plymouth/usr/share/initramfs-tools/scripts/init-premount/plymouth
	install -D -m 0755 debian/local/plymouth.init-bottom debian/plymouth/usr/share/initramfs-tools/scripts/init-bottom/plymouth
	install -D -m 0755 debian/local/plymouth.panic debian/plymouth/usr/share/initramfs-tools/scripts/panic/plymouth

override_dh_installsystemd:
	dh_installsystemd --no-start --no-restart-after-upgrade

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_installinit:
	dh_installinit --no-start -- start . 2 3 4 5 . stop . 0 6 .
	dh_installinit --no-start --name=plymouth-log -- start . S .

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/$(DEB_HOST_MULTIARCH)/plymouth
