#!/usr/bin/make -f

#export DH_VERBOSE=1

# enable bindnow
# https://wiki.debian.org/HardeningWalkthrough
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Bacula binaries break if linked with symbolic-functions
# https://bugs.bacula.org/view.php?id=2557
# Debian does not (yet) use this, but Ubuntu does. Help them out by
# stripping the flag from LDFLAGS
# https://bugs.launchpad.net/ubuntu/+source/bacula/+bug/1898006
export DEB_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions

# Make qtchooser select Qt5
export QT_SELECT=qt5

DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

VARIANTS := pgsql mysql sqlite3
VERSION := $(shell sed -n -e 's/^\#define VERSION.*"\(.*\)"$$/\1/p' src/version.h)

######################################################################

DBC := usr/share/dbconfig-common/data/

CONF_ALL	= \
	--enable-smartalloc \
	--with-tcp-wrappers \
	--with-openssl \
	--with-libiconv-prefix=/usr/include \
	--with-readline=/usr/include/readline \
	--disable-conio \
	--with-libintl-prefix=/usr/include \
	--docdir=\$${prefix}/share/doc/bacula-common \
	--htmldir=\$${prefix}/share/doc/bacula-common/html \
	--libdir=\$${prefix}/lib/bacula \
	--enable-batch-insert \
	--enable-ipv6 \
	--with-dir-password=XXX_DIRPASSWORD_XXX \
	--with-fd-password=XXX_FDPASSWORD_XXX \
	--with-sd-password=XXX_SDPASSWORD_XXX \
	--with-mon-dir-password=XXX_MONDIRPASSWORD_XXX \
	--with-mon-fd-password=XXX_MONFDPASSWORD_XXX \
	--with-mon-sd-password=XXX_MONSDPASSWORD_XXX \
	--with-db-name=XXX_DBNAME_XXX \
	--with-db-user=XXX_DBUSER_XXX \
	--with-db-password=XXX_DBPASSWORD_XXX \
	--with-hostname=localhost \
	--config-cache \
	--with-archivedir=/nonexistent/path/to/file/archive/dir \
	--sysconfdir=/etc/bacula \
	--with-scriptdir=/etc/bacula/scripts \
	--sharedstatedir=/var/lib/bacula \
	--localstatedir=/var/lib/bacula \
	--with-logdir=/var/log/bacula \
	--with-pid-dir=/run/bacula \
	--with-smtp-host=localhost \
	--with-working-dir=/var/lib/bacula \
	--with-subsys-dir=/run/lock \
	--with-dump-email=root \
	--with-job-email=root \
	--with-mysql --with-postgresql --with-sqlite3 \
	--enable-bat --with-x \
	--disable-s3

ifeq ($(DEB_HOST_ARCH_OS),linux)
# Set systemd directories, needed to properly support usr-merge
SYSTEMDUNITDIR = $(shell pkg-config --variable=systemdsystemunitdir systemd | sed s,^/,,)
TMPFILESDIR = $(shell pkg-config --variable=tmpfilesdir systemd | sed s,^/,,)
CONF_ALL	+= --with-systemd=/$(SYSTEMDUNITDIR)
endif

ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
CONF_ALL	+= --disable-acl --disable-xattr
endif

ifeq ($(DEB_HOST_ARCH_OS),hurd)
CONF_ALL       += --disable-acl --disable-xattr
endif

#set to 1 if configured with systemd
HAVE_SYSTEMD = $(findstring 1,$(shell egrep "^\#define HAVE_SYSTEMD" config.log))

LONGNAME_mysql		= mysql
LONGNAME_pgsql		= postgresql
LONGNAME_sqlite3	= sqlite3

###########################################################################

# Macro for generate file for specified database from specified template
# args: 1 -- template file
#       2 -- db type
define template-subst
sed -e 's/XX_DB_XX/$(2)/' -e 's/XX_DBLONGNAME_XX/$(LONGNAME_$(2))/' -e 's/XX_VERSION_XX/$(VERSION)/' '$(1)' >'$(patsubst %.in,%,$(subst -db.,-$(2).,$(1)))';
endef

# Macro for cleanup file generated for specified database from specified template
# args: 1 -- template file
#       2 -- db type
define template-cleanup
rm -f '$(patsubst %.in,%,$(subst -db.,-$(2).,$(1)))';
endef

# Macro for generate from all templates files for specified database
# args: 1 -- db type
define db-templates-subst
$(foreach template,$(wildcard debian/*-db.*.in),$(call template-subst,$(template),$(1)))
endef

# Macro for cleanup all files generated for specified database from templates
# args: 1 -- db type
define db-templates-cleanup
$(foreach template,$(wildcard debian/*-db.*.in),$(call template-cleanup,$(template),$(1)))
endef

%:
	dh $@

# For packages depending on bacula-director-DBTYPE, we handle the
# dependencies on that ourselves, because dpkg-shlibdeps doesn't
# understand the variants
override_dh_shlibdeps:
	dh_shlibdeps -pbacula-bscan -pbacula-director -- -xbacula-common-mysql -xbacula-common-pgsql -xbacula-common-sqlite3
	dh_shlibdeps -a --remaining-packages

override_dh_auto_build-arch:
	dh_auto_build -a
# also build the monitoring plugin from the examples
	make -C examples/nagios/check_bacula

override_dh_auto_install-arch:
	dh_auto_install -a

override_dh_install-arch:
# sd plugins aren't installed automatically
#	make DESTDIR=$(CURDIR)/debian/tmp -C src/stored install-aligned install-cloud
	dh_install -a
	# create a reference of which database type is installed on the system
	# and create a link to the dbconfig information that is dbtype independent
	$(foreach db,$(VARIANTS), \
		mkdir -p debian/bacula-common-$(db)/usr/share/bacula-common; \
		echo $(db) > debian/bacula-common-$(db)/usr/share/bacula-common/dbtype; \
		ln -s /etc/dbconfig-common/bacula-director-$(db).conf debian/bacula-common-$(db)/usr/share/bacula-common/dbc_database.conf;)
ifeq ($(HAVE_SYSTEMD),1)
	# only install systemd related files if systemd is enabled during build
	install -m 644 platforms/systemd/bacula.conf -D debian/bacula-common/$(TMPFILESDIR)/bacula.conf
	install -m 644 platforms/systemd/bacula-fd.service -D debian/bacula-fd/$(SYSTEMDUNITDIR)/bacula-fd.service
	install -m 644 platforms/systemd/bacula-sd.service -D debian/bacula-sd/$(SYSTEMDUNITDIR)/bacula-sd.service
	install -m 644 platforms/systemd/bacula-dir.service -D debian/bacula-director/$(SYSTEMDUNITDIR)/bacula-director.service
endif

override_dh_installexamples:
# The monitoring plugin in the examples needs to be cleaned, otherwise
# builds will not be reproducible. But only if the makefile exists,
# which it doesn't if we only build-indep.
	@[ -f examples/nagios/check_bacula/Makefile ] && make -C examples/nagios/check_bacula libtool-clean distclean || true
	dh_installexamples

override_dh_installdocs:
# upstream release notes should be installed as "NEWS" (Debian policy 12.7)
	cp -a ReleaseNotes NEWS
	dh_installdocs -pbacula --link-doc=bacula-server
	dh_installdocs -Nbacula-common -Nbacula-client -Nbacula-server -Nbacula-director-mysql -Nbacula-director-sqlite3 -Nbacula-director-pgsql --remaining-packages --link-doc=bacula-common
	dh_installdocs --remaining-packages

# We only need the scripts from src/cats for the arch:all packages.
# Compiling and installing is easier than picking out the scripts to
# install manually
override_dh_auto_build-indep:
	make DESTDIR=$(CURDIR)/debian/tmp -C src/cats
override_dh_auto_install-indep:
	mkdir -p debian/tmp/etc/bacula/scripts
	make DESTDIR=$(CURDIR)/debian/tmp -C src/cats install
	######### dbconfig-common stuff
	# The script install-dbconfig needs to be adapted when upstream changes the database format!
	debian/scripts/install-dbconfig # handles install of all upgrade/install scripts to db version 11 and greater

override_dh_autoreconf:
# save the original files if not already saved
	@[ -f configure -a ! -f debian/configure.dh_autoreconf.save ] && mv configure debian/configure.dh_autoreconf.save || true
	@[ -f autoconf/aclocal.m4 -a ! -f debian/aclocal.m4.dh_autoreconf.save ] && mv autoconf/aclocal.m4 debian/aclocal.m4.dh_autoreconf.save || true
	dh_autoreconf debian/autogen.sh

override_dh_autoreconf_clean:
	dh_autoreconf_clean
# restore the original files if saved
	@[ -f debian/configure.dh_autoreconf.save ] && mv debian/configure.dh_autoreconf.save configure || true
	@[ -f debian/aclocal.m4.dh_autoreconf.save ] && mv debian/aclocal.m4.dh_autoreconf.save autoconf/aclocal.m4 || true

override_dh_auto_configure:
	$(foreach db,$(VARIANTS),$(call db-templates-subst,$(db)))
	# Strange workaround for strange binary path problems in Qt5.9
	# https://gitweb.gentoo.org/repo/gentoo.git/tree/app-backup/bacula/bacula-9.0.6-r2.ebuild?id=9b8f311cb9d88f02bebc4bfb51282db4457f87d7#n166
	mkdir -p src/qt-console/.libs
	touch src/qt-console/.libs/bat
	chmod 755 src/qt-console/.libs/bat
#
	mkdir -p src/qt-console/tray-monitor/.libs
	touch src/qt-console/tray-monitor/.libs/bacula-tray-monitor
	chmod 755 src/qt-console/tray-monitor/.libs/bacula-tray-monitor
#
	dh_auto_configure -- $(CONF_ALL)
	debian/scripts/generate-doc-dir-to-symlink-migration create
	debian/scripts/autopostrm create

override_dh_makeshlibs:
	dh_makeshlibs -n -Xbpipe-fd.so
	# fix shlibs for bacula-common-*
	$(foreach db,$(VARIANTS), sed -i 's/libbaccats-[a-z0-9]*/libbaccats/' debian/bacula-common-$(db)/DEBIAN/shlibs;)

override_dh_clean:
	$(foreach db,$(VARIANTS),$(call db-templates-cleanup,$(db)))
	dh_clean
	debian/scripts/generate-doc-dir-to-symlink-migration clean
	debian/scripts/autopostrm clean
	rm -f NEWS

override_dh_auto_test: