#!/usr/bin/make -f

export LC_ALL=C.UTF-8
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export GOPATH=/usr/share/gocode
export GO111MODULE=off
export GOCACHE=$(CURDIR)/.gocache
export PKGSELECT = "common/modules/pkgselect"
export USRDIR = "debian/calamares-settings-ubuntu-common/usr"
export DATA_USRDIR = "debian/calamares-settings-ubuntu-common-data/usr"
export MODULES_DIR = "debian/calamares-settings-ubuntu-common/usr/lib/$(DEB_HOST_MULTIARCH)/calamares/modules"
export DATA_MODULES_DIR = "debian/calamares-settings-ubuntu-common-data/usr/lib/calamares/modules"

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

# Go builds on riscv64 oftentimes fail if using CGO, see
# https://github.com/golang/go/issues/72840.
ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH),riscv64)
	export CGO_ENABLED = 0
endif

%:
	dh $@ --with=builtusing

override_dh_auto_configure:
	(cd $(PKGSELECT) && mkdir build && cd build && cmake ..)

override_dh_auto_build:
	make;
	(cd $(PKGSELECT)/build && $(MAKE))
	(cd common/snap-seed-glue-emb && go build -gcflags="all=-N -l" -ldflags="-compressdwarf=false" -o snap-seed-glue-emb main.go)

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(GOCACHE)

override_dh_auto_install:
	(cd $(PKGSELECT)/build && $(MAKE) DESTDIR=$(CURDIR)/debian/calamares-settings-ubuntu-common/ install)

override_dh_missing:
	dh_missing
	mkdir -pv $(MODULES_DIR) $(DATA_MODULES_DIR)
	cp -vr common/modules/automirror $(DATA_MODULES_DIR)
	mkdir -pv debian/calamares-settings-ubuntu-common-data/etc/calamares/modules
	cp -vr common/modules/*.conf debian/calamares-settings-ubuntu-common-data/etc/calamares/modules
	chmod 644 $(DATA_MODULES_DIR)/automirror/automirror.conf
	chmod 644 $(DATA_MODULES_DIR)/automirror/module.desc
	chmod 644 $(MODULES_DIR)/pkgselect/libcalamares_viewmodule_pkgselect.so
	chmod 644 $(MODULES_DIR)/pkgselect/module.desc
	mkdir -pv $(USRDIR)/bin/
	cp -v common/snap-seed-glue-emb/snap-seed-glue-emb $(USRDIR)/bin/snap-seed-glue-emb
	mkdir -pv $(DATA_USRDIR)/libexec/
	cp -v common/fixconkeys-part1 $(DATA_USRDIR)/libexec/fixconkeys-part1
	cp -v common/fixconkeys-part2 $(DATA_USRDIR)/libexec/fixconkeys-part2
