#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)

# workaround for https://github.com/atheme/charybdis/issues/27
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

# allow users to build with OpenSSL even if it possibly breaks the
# GPL, as we have no exception from all the authors of ircd
#
# this is not necessary in Wheezy, as there is an OpenSSL
# compatibility layer in GNUTLS
#
# for this to work, you need to manually install libssl-dev
export DEB_USE_OPENSSL

ifdef DEB_USE_OPENSSL
    CONFIGURE_ARGS+=--enable-openssl
else
    CONFIGURE_ARGS+=--disable-openssl --enable-gnutls
endif

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

EXTERNAL_BUILD_TIMESTAMP?=$(shell LC_ALL=C date --utc -d "$$(dpkg-parsechangelog | sed -n -e 's/^Date: //p')")
export EXTERNAL_BUILD_TIMESTAMP

override_dh_auto_configure:
	autoreconf -fi
	cd libratbox ; autoreconf # necessary because we patch configure.ac
	cd libratbox ; automake
	dh_auto_configure -- --prefix=/usr --with-confdir=/etc/charybdis \
		${CONFIGURE_ARGS} \
		--with-program-prefix=charybdis- \
		--libdir=/usr/lib/charybdis \
		--libexecdir=/usr/lib \
		--with-rundir=/var/run \
		--localstatedir=/var/lib \
		--with-logdir=/var/log/charybdis \
		--with-moduledir=/usr/lib/charybdis/modules \
		--with-helpdir=/usr/share/doc/charybdis/help \
		--with-shared-sqlite \
		--enable-ipv6

override_dh_auto_install:
	dh_auto_install --destdir=debian/charybdis
	rmdir $(CURDIR)/debian/charybdis/var/run/charybdis || true
        # remove when it fails, it means upstream stopped shipping .la files, yay
	rm $(CURDIR)/debian/charybdis/usr/lib/charybdis/libratbox.la
        # see https://github.com/atheme/charybdis/issues/23
        # maybe this should be in /usr/share/doc, but then users won't know when to update their configs...
	mv $(CURDIR)/debian/charybdis/etc/charybdis/ircd.conf.example $(CURDIR)/debian/charybdis/etc/charybdis/ircd.conf

override_dh_strip:
	dh_strip --dbg-package=charybdis-dbg

%:
	dh $@ --with autotools_dev
