#!/usr/bin/make -f

# DEB_BUILD_OPTIONS support
INSTALL_PROGRAM=install
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
STRIP_PROGRAM = strip
else
STRIP_PROGRAM = echo not stripping
endif

build: build-stamp
build-stamp:
	dh_testdir
	autoconf
	./configure --prefix=/usr --mandir=/usr/share/man \
		--localstatedir=/var --sysconfdir=/etc
	$(MAKE) STRIP_PROGRAM="$(STRIP_PROGRAM)" INSTALLDIRS=vendor build test
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	-$(MAKE) realclean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/share/lintian/overrides
	$(MAKE) INSTALL_PROGRAM="$(INSTALL_PROGRAM)" \
		NO_SOLINKS=1 PREFIX=`pwd`/debian/mooix install
	cp debian/override debian/mooix/usr/share/lintian/overrides/mooix

	# The postinst will dynamically set up the tree in /var.
	rm -rf debian/mooix/var/lib/mooix/

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs README TODO TODO-LATER UPGRADING WISHLIST \
	               ADDONS NEWS CONTRIBUTING TASKS doc/*.txt
	dh_installexamples
	dh_makeshlibs
	dh_installdebconf -n
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms -X /usr/sbin/mooregister -X /usr/sbin/moopasswd \
	            -X /usr/lib/mooix        -X /usr/sbin/mooix-pty-helper
	dh_python
	dh_perl
	dh_installinit
	dh_installdeb
	
	# Hack up the postrm, adding to it some of the lines from mooix.conf.
	sed 's/@MOOIXCONF@/$(shell grep -v ^# mooix.conf | egrep 'MOOADMIN|MOOENTRYGROUP' | grep -v /)/' \
		< debian/mooix/DEBIAN/postrm > debian/mooix/DEBIAN/postrm.new
	mv -f debian/mooix/DEBIAN/postrm.new debian/mooix/DEBIAN/postrm
	chmod 0755 debian/mooix/DEBIAN/postrm

	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Update the web site. Not intended to be used by anyone except the author.
installhook:
	make FULLTREE=$(shell pwd) -C /home/web/mooix.net/ update release >/home/joey/tmp/mooix.log 2>&1

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