#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DH_OPTIONS
export LC_ALL=C.UTF-8

VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }')
EXPECTED_AC_VERSION := $(shell echo "$(VERSION)" | cut -d. -f1,2)
AC_VERSION := $(shell grep -w '^AC_INIT' configure.ac | cut -d' ' -f2 | \
			sed 's/[][,]//g')
MANGLED_VERSION := $(subst ~,-,$(VERSION))

ifneq ($(EXPECTED_AC_VERSION),$(AC_VERSION))
$(warning Version $(VERSION) in debian/changelog does not match $(AC_VERSION) in configure.ac!)
endif

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

console-setup-depends := console-setup (>= $(shell cd d-i/source/console-setup && dpkg-parsechangelog | awk '/Version:/ {print $$2}'))

update:
	$(MAKE) -C d-i update

update-local:
	$(MAKE) -C d-i update UPDATE_LOCAL=1

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp:
	dh_testdir

	dh_autoreconf -- ./autogen.sh
	dh_auto_configure -- --libexecdir=\$${prefix}/lib
	dh_auto_build
	$(MAKE) -C d-i build
	convert -resize 32x32 data/ubiquity.svg pixmaps/ubiquity.xpm

	touch build-stamp

check:
	# Sanity-check before upload.
	find -name debian -prune -o -name \*.py -print | xargs py3compile
	find -type f \( -name \*.pyc -o -name \*.pyo \) -print0 | xargs -0r rm -f
	find -name __pycache__ -print0 | xargs -0r rm -rf
	# Check the syntax of any shell scripts.
	set -e; for x in $$(find -type f \! -name \*.po \! -name \*.pot -print0 | xargs -0 file -i | grep "text/x-shellscript" | cut -d':' -f1); do \
		sh -n $$x; \
	done
	# Check the syntax of any Python scripts.
	./tests/run-pyflakes
	./tests/run-pep8

pychecker:
	./run-pychecker

clean: check
	dh_testdir
	dh_testroot

	$(MAKE) -C d-i clean
	dh_auto_clean
	dh_autoreconf_clean
	dh_clean
	rm -rf build-locale tests/partman-tree pixmaps/ubiquity.xpm

	$(MAKE) -C d-i check

d-i/source/console-setup/Keyboard/KeyboardNames.pl:
	$(MAKE) -C d-i/source/console-setup/Keyboard $(CURDIR)/d-i/source/console-setup/Keyboard/KeyboardNames.pl
		xkbdir=/usr/share/X11/xkb

debian/kbdnames.gz: d-i/source/console-setup/Keyboard/KeyboardNames.pl
	d-i/source/console-setup/Keyboard/kbdnames-maker ./$< | gzip -9c > $@

install: install-stamp

install-stamp: DH_OPTIONS=
install-stamp: build-stamp debian/kbdnames.gz
	dh_testdir
	dh_testroot
	dh_prep
	cp debian/ubiquity.dirs-any debian/ubiquity.dirs
ifneq (,$(wildcard debian/ubiquity.dirs-$(DEB_HOST_ARCH)))
	cat debian/ubiquity.dirs-$(DEB_HOST_ARCH) >> debian/ubiquity.dirs
endif
	dh_installdirs
	dh_installmenu

	$(MAKE) install DESTDIR=`pwd`/debian/tmp
	# Would probably be better to do this in the upstream buildsystem

	# help.ubuntu.com/rst
	qrencode -m 0 -s 5 -t PNG -l H -o debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-rst-input.png "https://help.ubuntu.com/rst"
	convert debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-rst-input.png -transparent white debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-rst.png
	rm debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-rst-input.png

	# help.ubuntu.com/bitlocker
	qrencode -m 0 -s 5 -t PNG -l H -o debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-bitlocker-input.png "https://help.ubuntu.com/bitlocker"
	convert debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-bitlocker-input.png -transparent white debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-bitlocker.png
	rm debian/tmp/usr/share/ubiquity/pixmaps/qr-help-ubuntu-com-bitlocker-input.png

	$(MAKE) -C d-i install

	cp debian/ubiquity.install-any debian/ubiquity.install
ifneq (,$(wildcard debian/ubiquity.install-$(DEB_HOST_ARCH)))
	cat debian/ubiquity.install-$(DEB_HOST_ARCH) >> debian/ubiquity.install
endif
ifdef UBIQUITY_NO_GTK
	dh_install -Nubiquity-frontend-gtk -Noem-config-gtk
else
ifdef UBIQUITY_NO_KDE
	dh_install -Nubiquity-frontend-kde -Noem-config-kde
else
	dh_install
endif
endif
	# We don't need the source files installed
	rm -rf debian/ubiquity-frontend-kde/usr/share/ubiquity/qt/images/source/

	dh_di_numbers

	# Bits of manual installation that can't be done by dh_install

	install bin/ubiquity-wrapper debian/ubiquity/usr/bin/ubiquity
	sed 's/@VERSION@/$(VERSION)/g' bin/ubiquity \
		> debian/ubiquity/usr/lib/ubiquity/bin/ubiquity
	chmod +x debian/ubiquity/usr/lib/ubiquity/bin/ubiquity
	sed 's,/usr/lib/apt-setup,/usr/lib/ubiquity/apt-setup,g' \
		d-i/source/apt-setup/apt-setup \
		> debian/ubiquity/usr/lib/ubiquity/apt-setup/apt-setup
	chmod +x debian/ubiquity/usr/lib/ubiquity/apt-setup/apt-setup

	install d-i/source/apt-setup/finish-install.d/10apt-cdrom-setup \
		debian/ubiquity/usr/lib/ubiquity/apt-setup/finish-install
	if [ -e "d-i/source/base-installer/kernel/$(DEB_HOST_ARCH).sh" ]; then \
		install -m644 d-i/source/base-installer/kernel/$(DEB_HOST_ARCH).sh \
			debian/ubiquity/usr/lib/ubiquity/base-installer/kernel.sh; \
	fi
	install d-i/source/clock-setup/debian/clock-setup.postinst \
		debian/ubiquity/usr/lib/ubiquity/clock-setup/clock-setup
	sed -e '/^# Update target system configuration/ { s/.*/exit 0/; q }' \
		d-i/source/clock-setup/finish-install.d/10clock-setup \
		> debian/ubiquity/usr/lib/ubiquity/clock-setup/finish-install
	chmod +x debian/ubiquity/usr/lib/ubiquity/clock-setup/finish-install
	sed -e 's,/usr/share/console-setup/keyboard-configuration.config,/var/lib/dpkg/info/keyboard-configuration.config,g' \
	    -e 's,^\([[:space:]]*\)update-initramfs,\1: update-initramfs,' \
		d-i/source/console-setup/debian/keyboard-configuration.postinst \
		> debian/ubiquity/usr/lib/ubiquity/console-setup/keyboard-configuration.postinst
	(cd d-i/source/console-setup && \
	 debian/preprocessor $(CURDIR)/debian/ubiquity/usr/lib/ubiquity/console-setup/keyboard-configuration.postinst && \
	 debian/preprocessor $(CURDIR)/debian/ubiquity/usr/share/ubiquity/console-setup-apply)
	chmod +x debian/ubiquity/usr/lib/ubiquity/console-setup/keyboard-configuration.postinst
	sed 's,\(finish_install=\).*,\1/dev/null,' \
		d-i/source/hw-detect/debian/hw-detect/bin/hw-detect \
		> debian/ubiquity/bin/hw-detect
	chmod +x debian/ubiquity/bin/hw-detect
	set -e; for x in languagemap localechooser; do \
		sed 's,/usr/share/localechooser,/usr/lib/ubiquity/localechooser,g' \
			d-i/source/localechooser/$$x \
			> debian/ubiquity/usr/lib/ubiquity/localechooser/$$x; \
		chmod +x debian/ubiquity/usr/lib/ubiquity/localechooser/$$x; \
	done
	cp -a d-i/source/localechooser/post-base-installer.d/05localechooser \
		debian/ubiquity/usr/lib/ubiquity/localechooser/post-base-installer
	patch debian/ubiquity/usr/lib/ubiquity/localechooser/post-base-installer \
		< d-i/patches/localechooser-post-base-installer.patch
	chmod +x debian/ubiquity/usr/lib/ubiquity/localechooser/post-base-installer
	sed 's/\\\$${!TAB}/ /g' \
		debian/ubiquity/usr/lib/ubiquity/localechooser/localechooser \
		> debian/ubiquity/usr/lib/ubiquity/localechooser/localechooser-debconf
	chmod +x debian/ubiquity/usr/lib/ubiquity/localechooser/localechooser-debconf
	install d-i/source/localechooser/finish-install.d/05localechooser \
		debian/ubiquity/usr/lib/ubiquity/localechooser/finish-install
	install d-i/source/tzsetup/post-base-installer.d/*tzsetup \
		debian/ubiquity/usr/lib/ubiquity/tzsetup/post-base-installer
	sed -e 's,/target/,/,g;s,/target,/,g' \
		debian/ubiquity/usr/lib/ubiquity/tzsetup/post-base-installer \
		> debian/oem-config/usr/lib/ubiquity/tzsetup/post-base-installer-oem
	chmod +x debian/oem-config/usr/lib/ubiquity/tzsetup/post-base-installer-oem
	sed -i -e 's,db_input medium tzsetup/selected,db_input high tzsetup/selected,' \
	       -e 's,/usr/share/tzsetup/tzmap,/usr/lib/ubiquity/tzsetup/tzmap,g' \
		debian/ubiquity/usr/lib/ubiquity/tzsetup/tzsetup
	cp debian/ubiquity/usr/lib/ubiquity/user-setup/reserved-usernames \
		debian/ubiquity/usr/lib/ubiquity/user-setup/reserved-usernames-oem
	echo oem >> debian/ubiquity/usr/lib/ubiquity/user-setup/reserved-usernames-oem
	set -e; for x in user-setup user-setup-ask user-setup-apply; do \
		sed -e 's,/usr/lib/user-setup,/usr/lib/ubiquity/user-setup,g' \
		    -e 's,/bin/sh,/bin/bash,g' \
			d-i/source/user-setup/$$x \
			> debian/ubiquity/usr/lib/ubiquity/user-setup/$$x; \
		chmod +x debian/ubiquity/usr/lib/ubiquity/user-setup/$$x; \
	done
	sed -i -e 's,db_input high user-setup/encrypt-home,db_input medium user-setup/encrypt-home,' \
		debian/ubiquity/usr/lib/ubiquity/user-setup/user-setup-ask
	sed -e 's,reserved-usernames,reserved-usernames-oem,g' \
		debian/ubiquity/usr/lib/ubiquity/user-setup/user-setup-ask \
		> debian/ubiquity/usr/lib/ubiquity/user-setup/user-setup-ask-oem
	chmod +x debian/ubiquity/usr/lib/ubiquity/user-setup/user-setup-ask-oem
	install d-i/source/debian-installer-utils/list-devices-$(DEB_HOST_ARCH_OS) \
		debian/ubiquity/bin/list-devices
	install d-i/source/debian-installer-utils/post-base-installer.d/10register-module \
		debian/ubiquity/usr/lib/ubiquity/debian-installer-utils/register-module.post-base-installer
	sed -e 's,/target/,/,g;s,/target,/,g' \
		d-i/source/debian-installer-utils/post-base-installer.d/10register-module \
		> debian/oem-config/usr/lib/ubiquity/debian-installer-utils/register-module.post-base-installer-oem
	chmod +x debian/oem-config/usr/lib/ubiquity/debian-installer-utils/register-module.post-base-installer-oem
	touch install-stamp

tests: install-stamp
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	CODE=0; env -i LANG=C.UTF-8 ./tests/run --xvfb-log debian/xvfb.log || CODE=$$?; \
	echo "xauth/Xrdb output:"; \
	cat debian/xvfb.log; \
	rm -f debian/xvfb.log; \
	exit $$CODE
endif

BINARY_INDEP_OPTIONS := -i
ifdef UBIQUITY_NO_GTK
BINARY_INDEP_OPTIONS += -Noem-config-gtk
endif
ifdef UBIQUITY_NO_KDE
BINARY_INDEP_OPTIONS += -Nubiquity-frontend-kde -Noem-config-kde
endif

# Build architecture-independent files here.
binary-indep: DH_OPTIONS=$(BINARY_INDEP_OPTIONS)
binary-indep: install-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installdebconf
	# oem-config
	dh_systemd_enable --name=oem-config
	dh_installinit -n --name=oem-config
	dh_installinit -n --name=oem-config-debconf
	dh_installman
	dh_lintian
	dh_link
	dh_scour
	dh_strip
	dh_compress
	dh_fixperms
	dh_python3 /usr/lib/ubiquity
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -Vmangled-version='$(MANGLED_VERSION)'
	dh_md5sums
	NO_PNG_PKG_MANGLE=1 dh_builddeb -- -Zxz

BINARY_ARCH_OPTIONS := -a
ifdef UBIQUITY_NO_GTK
BINARY_ARCH_OPTIONS += -Nubiquity-frontend-gtk
endif

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS=$(BINARY_ARCH_OPTIONS)
binary-arch: tests install-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installdebconf
	echo >> debian/ubiquity/DEBIAN/templates
	po2debconf --podir=debian/imported-po \
		debian/ubiquity.templates-imported \
		>> debian/ubiquity/DEBIAN/templates
	echo >> debian/ubiquity/DEBIAN/templates
	grep-dctrl -XFTemplate base-installer/kernel/linux/link_in_boot \
		d-i/templates \
		>> debian/ubiquity/DEBIAN/templates
	if [ "$$(tail -n1 debian/ubiquity/DEBIAN/templates)" ]; then \
		echo >> debian/ubiquity/DEBIAN/templates; \
	fi
	grep-dctrl -v -FTemplate base-installer/ d-i/templates \
		>> debian/ubiquity/DEBIAN/templates
	# ubiquity
	dh_systemd_enable --name=ubiquity
	dh_installinit --no-start --name=ubiquity
	dh_installman
	dh_lintian
	dh_link
	dh_scour
	dh_strip
	dh_compress
	dh_fixperms
	dh_python3 /usr/lib/ubiquity
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -V'console-setup-depends=$(console-setup-depends)' -Vmangled-version='$(MANGLED_VERSION)'
	dh_md5sums
	NO_PNG_PKG_MANGLE=1 dh_builddeb -- -Zxz

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install tests
