#!/usr/bin/make -f
# -*- makefile -*-

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

# This has to be exported to make some magic below work.
DH_OPTIONS = -v

DAEMON_USER = bareos
DAEMON_GROUP = bareos
DIRECTOR_DAEMON_USER = $(DAEMON_USER)
STORAGE_DAEMON_USER = $(DAEMON_USER)
FILE_DAEMON_USER = root
STORAGE_DAEMON_GROUP = $(DAEMON_GROUP)
BAREOS_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | sed 's/Version: //g')
DIST_ID_REL := $(shell lsb_release --id --release --short | tr -d '\n')

define CONFIGURE_COMMON
  -Dsbindir=/usr/sbin \
  -Dbindir=/usr/bin \
  -Dlibdir=/usr/lib/bareos \
  -Dsysconfdir=/etc \
  -Dconfdir=/etc/bareos \
  -Darchivedir=/var/lib/bareos/storage \
  -Dbackenddir=/usr/lib/bareos/backends \
  -Dconfigtemplatedir=/usr/lib/bareos/defaultconfigs \
  -Dscriptdir=/usr/lib/bareos/scripts \
  -Dplugindir=/usr/lib/bareos/plugins \
  -Dworkingdir=/var/lib/bareos \
  -Dlogdir=/var/log/bareos \
  -Dsubsysdir=/var/lock \
  -DSHARE_INSTALL_PREFIX:PATH=/usr/share \
  -Dwebuiconfdir=/etc/bareos-webui \
  -Dscsi-crypto=yes \
  -Dndmp=yes \
  -Ddir-user=$(DIRECTOR_DAEMON_USER) \
  -Ddir-group=$(DAEMON_GROUP) \
  -Dsd-user=$(STORAGE_DAEMON_USER) \
  -Dsd-group=$(STORAGE_DAEMON_GROUP) \
  -Dfd-user=$(FILE_DAEMON_USER) \
  -Dfd-group=$(DAEMON_GROUP) \
  -Ddir-password="XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX" \
  -Dfd-password="XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX" \
  -Dsd-password="XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX" \
  -Dmon-dir-password="XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX" \
  -Dmon-fd-password="XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX" \
  -Dmon-sd-password="XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX" \
  -Dbasename="XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX" \
  -Dhostname="XXX_REPLACE_WITH_LOCAL_HOSTNAME_XXX" \
  -Drpath=no \
  -DVERSION_STRING=$(BAREOS_VERSION)
endef



ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
CONFIGURE_COMMON       += -Dacl=no -Dxattr=no -Dscsi-crypto=no -Dndmp=no
endif
ifeq ($(DEB_HOST_ARCH_OS),hurd)
CONFIGURE_COMMON       += -Dscsi-crypto=no -Dndmp=no
endif


# no gprc for universal client, Ubuntu20.04 and Debian11
ifneq (build-universal-client,$(filter build-universal-client,$(DEB_BUILD_OPTIONS)))
	ifneq ($(DIST_ID_REL),Ubuntu20.04)
		ifneq ($(DIST_ID_REL),Debian11)
	CONFIGURE_COMMON       += -DENABLE_GRPC=yes
		endif
	endif
endif


override_dh_auto_configure:
ifeq (build-universal-client,$(filter build-universal-client,$(DEB_BUILD_OPTIONS)))
	echo "configuring universal client"
	dh_auto_configure -- $(CONFIGURE_COMMON) -DBUILD_UNIVERSAL_CLIENT=yes
else
	echo "configure full configuration"
	dh_auto_configure -- $(CONFIGURE_COMMON) -Dtraymonitor=yes
endif
	cmake --build cmake-build/ -- generate-debian-control


override_dh_fixperms:
	# all files in /etc/bareos
	dh_fixperms \
		--exclude etc/bareos/ \
		--exclude var/lib/bareos/ \
		--exclude var/log/bareos/


override_dh_strip:
ifeq (build-universal-client,$(filter build-universal-client,$(DEB_BUILD_OPTIONS)))
	dh_strip --dbg-package=bareos-universal-client-dbg
else
	dh_strip --dbg-package=bareos-dbg
endif


override_dh_systemd_enable:
ifeq (build-universal-client,$(filter build-universal-client,$(DEB_BUILD_OPTIONS)))
	dh_systemd_enable --name bareos-filedaemon
else
	dh_systemd_enable
endif


override_dh_installinit:
	# Skipped. Only use systemd configuration.


override_dh_systemd_start:
	dh_systemd_start bareos-filedaemon.service --no-start --restart-after-upgrade


override_dh_makeshlibs:
	dh_makeshlibs -n


override_dh_auto_test:
# Only run tests,
# if DEB_BUILD_OPTIONS does not contain nocheck,
# see https://www.debian.org/doc/debian-policy/ch-source.html#s-debianrules-options
# No tabs allowed before ifeq.
ifneq (nocheck,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd cmake-build && ls Testing/*/Test.xml || REGRESS_DEBUG=1 ctest -V -S CTestScript.cmake || echo "ctest result:$?"
else
	@echo "ctest: skipped"
endif


override_dh_shlibdeps:
	dh_shlibdeps -l$(shell pwd)/debian/tmp/usr/lib/bareos


%:
	dh $@ --builddirectory=cmake-build


gen-debian-copyright:
	(cd debian; make copyright)
