#!/usr/bin/make -f
# debian/rules file for ircd-hybrid,
# created by Joshua Kwan <joshk@triplehelix.org> for ircd-hybrid-7.0.x,
# modified by Aurélien GÉRÔME for ircd-hybrid-7.2.2.
# Note that the get-orig-source target depends on wget.

export CFLAGS = -g
DOWNLOAD_URL = http://prdownloads.sourceforge.net/ircd-hybrid/ircd-hybrid-7.2.2.tgz

# We don't want debian/control to be nuked
MAINT_FILES	  =	debian/control \
			debian/changelog \
			$(MAINT_CLEAN_FILES)
MAINT_CLEAN_FILES =	debian/ircd.conf \
			debian/ircd-hybrid.postinst \
			debian/ircd-hybrid.postrm \
			debian/ircd-hybrid.preinst \
			debian/ircd-hybrid.config

# Make my life easier
I = debian/ircd-hybrid
I_LIB = $(I)/usr/lib/ircd-hybrid
I_SBIN = $(I)/usr/sbin
I_DOC = $(I)/usr/share/doc/ircd-hybrid
I_ETC = $(I)/etc/ircd-hybrid

buildtype := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
hosttype := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

arch := $(shell dpkg-architecture -qDEB_HOST_ARCH)

package_version = $(shell dpkg-parsechangelog | sed -n 's/^Version: *//p')

ifeq ($(arch),alpha)
	configure_options := --enable-poll
endif

ifeq ($(arch),kfreebsd-i386)
	configure_options := --enable-poll
endif

ifeq ($(arch),kfreebsd-amd64)
	configure_options := --enable-poll
endif

ifeq ($(arch),hurd-i386)
	configure_options := --enable-poll
endif

ifneq ($(buildtype),$(hosttype))
	crossflags := --build=$(buildtype) --host=$(hosttype)
endif

ifndef USE_OPENSSL
	sslflags := --disable-openssl
endif

configure: configure-stamp

configure-stamp:
ifdef USE_OPENSSL
	dpkg-checkbuilddeps
endif
	dh_testdir

	/bin/echo -ne '#ifndef PATCHLEVEL\n#define PATCHLEVEL "hybrid-' > include/patchlevel-debian.h
	/bin/echo -n $(package_version) >> include/patchlevel-debian.h	
	/bin/echo -ne '"\n#endif' >> include/patchlevel-debian.h

	dh_autoreconf

	./configure	$(crossflags) $(sslflags) \
			--prefix=/usr \
			--sysconfdir=/etc/ircd-hybrid \
			--localstatedir=/var \
			$(configure_options) \
			$(shell dpkg-buildflags --export=configure) \
			--enable-halfops \
			--program-suffix=-hybrid \
			--without-included-ltdl
	touch $@

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: $(MAINT_FILES) configure
	$(MAKE)
	
	touch build-stamp

manpages: manpages-stamp

manpages-stamp:
	mkdir -p debian/doc-pages
	@echo "Processing man pages: "
	@for i in $(wildcard debian/doc/*.sgml); do \
		TARGET="debian/doc-pages/`basename $$i | sed 's/\.sgml//'`"; \
		echo "$$i -> $$TARGET"; \
		docbook-to-man $$i > $$i.tmp; \
		sed -n "`grep -n '^.TH' $$i.tmp | awk -F : '{print $$1}'`~1p" $$i.tmp > $$TARGET; \
		rm -f $$i.tmp; \
	done
	touch $@

clean:
	dh_testroot
	dh_testdir
	rm -rf debian/doc-pages
	rm -f $(MAINT_CLEAN_FILES)
ifneq (,$(wildcard debian/control.orig))
	cp debian/control.orig debian/control
endif
ifneq (,$(wildcard debian/changelog.orig))
	mv debian/changelog.orig debian/changelog
endif
	rm -f build-stamp configure-stamp manpages-stamp config.log changelog-stamp control-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f include/patchlevel-debian.h
	dh_clean

debian/changelog: changelog-stamp

changelog-stamp:
ifdef USE_OPENSSL
	cp debian/changelog debian/changelog.orig
	( sh debian/boguschangelog.sh; cat debian/changelog ) > debian/changelog.new
	mv debian/changelog.new debian/changelog
endif

	touch changelog-stamp


debian/control: control-stamp

control-stamp:
	cp debian/control debian/control.orig
ifdef USE_OPENSSL
	sed -e 's^@SSL_BUILD_DEPENDS@^, libssl-dev^' \
	    -e 's^@SSL_HYBRID_DEPENDS@^, openssl^' \
	    -e 's^@SSL_CRYPTLINK_DESC@^o SSL server-to-server connections (cryptlinks)`printf \\n`^' \
	  debian/control.in > debian/control
else
	sed -e 's^@SSL_BUILD_DEPENDS@^^' \
	    -e 's^@SSL_HYBRID_DEPENDS@^^' \
	    -e 's^@SSL_CRYPTLINK_DESC@^^' debian/control.in > debian/control
endif
	
	touch $@

debian/ircd.conf:
ifdef USE_OPENSSL
	ln -sf ircd.conf.ssl debian/ircd.conf
else
	ln -sf ircd.conf.nossl debian/ircd.conf
endif

debian/ircd-hybrid.preinst:
ifdef USE_OPENSSL
	cp debian/ircd-hybrid.preinst.ssl debian/ircd-hybrid.preinst
else
	cp debian/ircd-hybrid.preinst.nossl debian/ircd-hybrid.preinst
endif

debian/ircd-hybrid.postrm:
ifdef USE_OPENSSL
	sed -e "s^@SSL_REMOVE@^`cat debian/ircd-hybrid.postrm.ssl`^" \
	  debian/ircd-hybrid.postrm.in > debian/ircd-hybrid.postrm
else
	sed -e "s^@SSL_REMOVE@^^" \
	  debian/ircd-hybrid.postrm.in > debian/ircd-hybrid.postrm
endif

debian/ircd-hybrid.postinst:
ifdef USE_OPENSSL
	sed -e "s^@SSL_WORK@^`cat debian/ircd-hybrid.postinst.ssl`^" \
	  debian/ircd-hybrid.postinst.in > debian/ircd-hybrid.postinst
else
	sed -e "s^@SSL_WORK@^^" \
	  debian/ircd-hybrid.postinst.in > debian/ircd-hybrid.postinst
endif

debian/ircd-hybrid.config:
ifdef USE_OPENSSL
	cp debian/ircd-hybrid.config.ssl debian/ircd-hybrid.config
else
	 cp debian/ircd-hybrid.config.nossl debian/ircd-hybrid.config
endif

install: manpages $(MAINT_FILES)

	dh_testdir
	dh_prep
	dh_installdirs -A
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	rm -f $(CURDIR)/debian/tmp/etc/ircd-hybrid/reference.conf

# The makefile installing headers for external modules building has
# disappeared since 7.0.3, so we are now doing what it did...
	mkdir -p $(CURDIR)/debian/tmp/usr/include/ircd-hybrid-8
	(cd include; for header in *.h; do \
		install -m644 $$header $(CURDIR)/debian/tmp/usr/include/ircd-hybrid-8; \
	done)

	dh_install -A --sourcedir=debian/tmp

binary-indep: build install

	dh_testdir
	dh_installchangelogs -i

	chmod +x debian/mbuild-hybrid
	cp -f debian/mbuild-hybrid debian/hybrid-dev/usr/bin

	dh_installdocs -i
	
	dh_installman -i debian/doc-pages/mbuild-hybrid.1

	dh_compress -i

	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install

	dh_testdir
	dh_testroot

	dh_installdocs -a -Xdoc/ircd. -Xline. -XMakefile -Xexample

	dh_installexamples doc/reference.conf

	dh_installman -a

	dh_installchangelogs -a

	dh_compress -a

	cp -Lf debian/ircd.conf $(I_ETC)/ircd.conf
	cp -f debian/ircd.motd $(I_ETC)/ircd.motd

	cp debian/ircd-hybrid.lintian.override $(I)/usr/share/lintian/overrides/ircd-hybrid

	# mkpasswd -Hmd5
	rm -f $(I_SBIN)/mkpasswd
	rm -f $(I_ETC)/.convertconf-example.conf $(I_ETC)/example*
	touch $(I_ETC)/xline.conf $(I_ETC)/nresv.conf $(I_ETC)/cresv.conf $(I_ETC)/kline.conf $(I_ETC)/dline.conf

	# Fixes lintian error non-empty-dependency_libs-in-la-file
	sed -i "/dependency_libs/ s/'.*'/''/" `find . -name '*.la'`

	dh_fixperms -a

	chown irc:irc debian/ircd-hybrid/var/log/ircd
	chown -R irc:irc $(I_ETC)
	chmod -R 660 $(I_ETC)
ifdef USE_OPENSSL
	# Don't care for this to be 660.
	cp debian/cert.cnf $(I_ETC) 
endif
	chmod ug+x $(I_ETC)

	dh_installinit -a -n
	dh_installdebconf -a
	dh_installlogrotate -a

	dh_strip -a
	dh_link -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary:	binary-indep binary-arch

get-orig-source:
	wget $(DOWNLOAD_URL)

.PHONY: binary binary-arch binary-indep clean clean1 install get-orig-source manpages debian/control debian/changelog
