#!/usr/bin/make -f
# -*- makefile -*-
#
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

ifeq ($(DEB_HOST_ARCH_OS), linux)
    CONFIGURE_ARGS+=--enable-epoll
else ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
    CONFIGURE_ARGS+=--enable-kqueue
else ifeq ($(DEB_HOST_ARCH_OS), solaris)
    CONFIGURE_ARGS+=--enable-ports
else
    CONFIGURE_ARGS+=--enable-poll
endif

ifdef DEB_DEBUG
   CONFIGURE_ARGS+=--enable-warnings --enable-iodebug
endif

ifneq ($(filter pkg.solanum.mbedtls,$(DEB_BUILD_PROFILES)),)
	CONFIGURE_ARGS+=--disable-openssl --enable-mbedtls
else ifneq ($(filter pkg.solanum.gnutls,$(DEB_BUILD_PROFILES)),)
	CONFIGURE_ARGS+=--disable-openssl --enable-gnutls
else
	CONFIGURE_ARGS+=--enable-openssl
endif

ifdef DEB_NICKLEN
    CONFIGURE_ARGS+=--with-nicklen=$(DEB_NICKLEN)
endif

EXTERNAL_BUILD_TIMESTAMP?=$(shell LC_ALL=C date --utc -d @$(SOURCE_DATE_EPOCH))
export EXTERNAL_BUILD_TIMESTAMP

override_dh_autoreconf:
	mkdir -p libltdl/config
	dh_autoreconf

override_dh_auto_configure:
	[ ! -f include/serno.h ] && \
		echo '#define SERNO "'$(DEB_VERSION_UPSTREAM)'"' > include/serno.h && \
		echo '#define DATECODE $(SOURCE_DATE_EPOCH)UL' >> include/serno.h
	dh_auto_configure -- --with-program-prefix=solanum- \
		--enable-fhs-paths \
		--libdir=/usr/lib/solanum \
		--localstatedir=/var/lib \
		--sysconfdir=/etc/solanum \
		--with-helpdir=/usr/share/doc/solanum/help \
		--with-logdir=/var/log/solanum \
		--with-moduledir=/usr/lib/solanum/modules \
		--with-rundir=/run \
		${CONFIGURE_ARGS}

override_dh_auto_install:
	dh_auto_install --destdir=debian/solanum
	# maybe this should be in /usr/share/doc, but then users won't know when to update their configs...
	mv $(CURDIR)/debian/solanum/etc/solanum/ircd.conf.example $(CURDIR)/debian/solanum/etc/solanum/ircd.conf

# Don't restart the ircd
override_dh_installinit:
	dh_installinit --no-stop-on-upgrade
override_dh_installsystemd:
	dh_installsystemd --no-stop-on-upgrade

override_dh_shlibdeps:
	dh_shlibdeps -X/usr/lib/solanum/modules/extensions -- \
    -dRecommends debian/solanum/usr/lib/solanum/modules/extensions/* -dDepends

%:
	dh $@
