#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
export CROSS_COMPILE ?= $(DEB_HOST_GNU_TYPE)-
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -I/usr/include/p11-kit-1
export LIBDIR=usr/lib/${DEB_HOST_MULTIARCH}
export HAVE_LUA=n
ifeq (,$(filter nolua,$(DEB_BUILD_PROFILES)))
export LUA_VERSION=5.3
export LUA_MODNAME=lua_swupdate
export PKG_NAME=swupdate
export DH_WITH=--with lua
export HAVE_LUA=y
endif

BU_SWU=
ifeq (linux,$(DEB_HOST_ARCH_OS))
	BU_SWU += -VBuilt-Using="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W libmtd-dev libubi-dev)"
endif
ifeq (,$(filter pkg.swupdate.bpo,$(DEB_BUILD_PROFILES)))
	# Bootstrap's scss file is derived from
	BU_SWU += -VB-U-www="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W libjs-bootstrap4)"
endif

override_dh_auto_configure:
	cp debian/configs/defconfig  configs/debian_defconfig
ifeq (linux,$(DEB_HOST_ARCH_OS))
	echo CONFIG_UBOOT=y                >> configs/debian_defconfig
	echo CONFIG_UCFWHANDLER=y          >> configs/debian_defconfig
	echo CONFIG_CFI=y                  >> configs/debian_defconfig
	echo CONFIG_CFIHAMMING1=y          >> configs/debian_defconfig
	echo CONFIG_UBIVOL=y               >> configs/debian_defconfig
	echo CONFIG_SSBLSWITCH=y           >> configs/debian_defconfig
else
	echo "# CONFIG_MTD is not set"     >> configs/debian_defconfig
endif
ifeq (,$(filter pkg.swupdate.bpo,$(DEB_BUILD_PROFILES)))
	echo CONFIG_DELTA=y >> configs/debian_defconfig
endif
ifneq (,$(findstring $(DEB_HOST_ARCH),amd64 i386 arm64 armhf riscv64 ia64))
	echo CONFIG_BOOTLOADER_EBG=y                    >> configs/debian_defconfig
endif
ifeq (,$(filter pkg.swupdate.nohwcompat,$(DEB_BUILD_PROFILES)))
	echo CONFIG_HW_COMPATIBILITY=y                   >> configs/debian_defconfig
endif
ifeq (,$(filter pkg.swupdate.nosigning,$(DEB_BUILD_PROFILES)))
	echo CONFIG_SIGNED_IMAGES=y >> configs/debian_defconfig
endif
ifeq (,$(filter pkg.swupdate.noswuforwarder,$(DEB_BUILD_PROFILES)))
	echo CONFIG_SWUFORWARDER_HANDLER=y       >> configs/debian_defconfig
endif
ifneq (,$(filter pkg.swupdate.p11,$(DEB_BUILD_PROFILES)))
	echo CONFIG_PKCS11=y >> configs/debian_defconfig
endif
ifneq (,$(LUA_VERSION))
	echo CONFIG_LUAPKG=\"lua$(LUA_VERSION)\" >> configs/debian_defconfig
	echo CONFIG_LUASCRIPTHANDLER=y           >> configs/debian_defconfig
	echo CONFIG_HANDLER_IN_LUA=y             >> configs/debian_defconfig
	echo CONFIG_SURICATTA_LUA=y              >> configs/debian_defconfig
endif
	echo CONFIG_EXTRA_CFLAGS=\"$(CFLAGS) $(CPPFLAGS)\" >> configs/debian_defconfig
	echo CONFIG_EXTRA_LDFLAGS=\"$(LDFLAGS)\"           >> configs/debian_defconfig
	echo CONFIG_EXTRA_LDLIBS=\"$(LDLIBS)\"             >> configs/debian_defconfig
	$(MAKE) debian_defconfig

override_dh_auto_build:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	$(MAKE) latexpdf man
	mv doc/build/man/client.1 doc/build/man/swupdate-client.1
	mv doc/build/man/ipc.1 doc/build/man/swupdate-ipc.1
	mv doc/build/man/progress.1 doc/build/man/swupdate-progress.1
endif
	dh_auto_build -- V=1

	# Build web-app without gulp
	cd web-app && pkgjs-install-minimal
ifeq (,$(filter pkg.swupdate.bpo,$(DEB_BUILD_PROFILES)))
	sassc -Iweb-app web-app/scss/bootstrap.scss > web-app/css/bootstrap.css
	# replace rule from gulp's minify-html
	sed 's|node_modules/bootstrap/dist/css|css|' web-app/index.html > index.html
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
ifeq (,$(findstring mips,$(DEB_HOST_ARCH)))
	dh_auto_test -- V=1
endif
endif

override_dh_auto_install:
	dh_auto_install -- V=1

override_dh_installsystemd:
	dh_installsystemd --no-start
	dh_installsystemd --name=swupdate-progress
	dh_installsystemd --no-start --name=swupdate-usb@

override_dh_gencontrol:
	dh_gencontrol -- $(BU_SWU)

override_dh_dwz:
	dh_dwz --no-dwz-multifile

override_dh_missing:
	dh_missing --fail-missing

%:
	dh $@ $(DH_WITH)
