#!/usr/bin/make -f

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CXXFLAGS = `dpkg-buildflags --get CXXFLAGS`
CXXFLAGS += -Wall
LDFLAGS = `dpkg-buildflags --get LDFLAGS`
CPPFLAGS = `dpkg-buildflags --get CPPFLAGS`

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	CXXFLAGS="$(CXXFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure $(confflags) \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-cgi-bin-dir=/usr/lib/cgi-bin/lurker
	touch $@

build-stamp: configure-stamp
	touch $@

build-indep: build-stamp
	dh_testdir
	pod2man --section=8 --center="Administrative commands" --release=" " debian/mailman2lurker debian/mailman2lurker.8

build-arch: build-stamp
	dh_testdir
	$(MAKE)

build: build-indep build-arch

clean:
	dh_testdir
	dh_testroot
	if test -f Makefile; then $(MAKE) distclean; fi
	# remove splitted configs and generated manpage
	rm -f lurker.conf.local lurker.conf.template debian/mailman2lurker.8
	# update po files
	debconf-updatepo
	dh_clean configure-stamp build-stamp install-stamp

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/lurker install
	awk 'BEGIN { X=1 } { if ( X ) { print $$0'\n' } } /'"^# Mailing list configuration.$$"'/ { X=0 }' < lurker.conf | head -n -2 > lurker.conf.template
	echo "# Include the configuration for local mailinglists" >> lurker.conf.template
	echo "include = lurker.conf.local" >> lurker.conf.template
	echo "#############################################################################" > lurker.conf.local
	echo "# Mailing list configuration." >> lurker.conf.local
	awk 'BEGIN { X=0 } { if ( X ) { print $$0'\n' } } /'"^# Mailing list configuration.$$"'/ { X=1 }' < lurker.conf >> lurker.conf.local
	touch $@

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf	
	dh_install
	dh_installdocs
	dh_installcron
	dh_installchangelogs ChangeLog
	dh_installman debian/lurker-index-lc.1 debian/mailman2lurker.8
	dh_installexamples lurker.conf
	dh_lintian
	mv debian/lurker/etc/apache2/conf-available/apache.conf debian/lurker/etc/apache2/conf-available/lurker.conf
	mv debian/lurker/usr/share/lurker/www/ui debian/lurker/etc/lurker
	dh_link etc/lurker/ui usr/share/lurker/www/ui
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: configure build-indep build-arch build install binary-indep binary-arch binary clean
