#!/usr/bin/make -f

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

# Enable Debian hardening at build time
# https://wiki.debian.org/Hardening#dpkg-buildflags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

# To make this package Deterministic, we parse the last Debian release date
# from the debian/changelog. This means that we must do regular releases! :)
# To read more about this process and other details, please refer to this bug:
# https://github.com/ioerror/tlsdate/issues/142
export COMPILE_DATE ?= $(shell date -u --date="`dpkg-parsechangelog|grep ^Date|head -1|sed -e 's/^Date: //'`" +%s)

DEB_BUILD_ARCH_OS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

%:
	dh $@ -with autotools_dev,autoreconf

override_dh_auto_configure:
	./autogen.sh
	dh_auto_configure

override_dh_auto_install:
	dh_auto_install
	# Debian Policy rightfully suggest to not ship LICENSE files as the information is also part of debian/copyright
	rm debian/tlsdate/usr/share/doc/tlsdate/LICENSE
	# Remove .la file
	rm -f debian/tlsdate/usr/lib/libtlsdate_compat.la

ifeq ($(DEB_BUILD_ARCH_OS),linux)
override_dh_systemd_enable:
	dh_systemd_enable tlsdated.service
endif
		
ifeq ($(DEB_BUILD_ARCH_OS),linux)
override_dh_install:
	cp apparmor-profile debian/tlsdate/etc/apparmor.d/usr.bin.tlsdate
	# use copy from debian/tlsdated.service as long as upstream file is broken:
	cp debian/tlsdated.service debian/tlsdate/lib/systemd/system/
	dh_apparmor --profile-name=usr.bin.tlsdate -ptlsdate
override_dh_installinit:
	dh_installinit --name=tlsdated
endif
	

override_dh_installchangelogs:
	dh_installchangelogs
	# Debian Policy requires that the upstream changelog is called changelog...
	mv debian/tlsdate/usr/share/doc/tlsdate/CHANGELOG debian/tlsdate/usr/share/doc/tlsdate/changelog

override_dh_auto_clean:
	dh_auto_clean
	rm -f src/configmake.h
