#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatability version to use.
export DH_COMPAT=4

# In case we set it anywhere
export DH_OPTIONS

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  archflags += --build $(DEB_HOST_GNU_TYPE)
else
  archflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

confflags = $(archflags) --prefix=/usr --mandir=\$${prefix}/share/man --localstatedir=/var/run/teapop --infodir=\$${prefix}/share/info --sysconfdir=/etc/teapop --exec_prefix=\$${prefix} --libexecdir=\$${exec_prefix}/sbin --enable-lock=dotlock,fcntl

# This code for backportability
# See http://lists.debian.org/debian-i18n/2003/debian-i18n-200307/msg00026.html
# for details
# Thanks Colin Watson
ifeq (,$(wildcard /usr/bin/po2debconf))
PO2DEBCONF := no
MINDEBCONFVER := 0.5
else
PO2DEBCONF := yes
MINDEBCONFVER := 1.2.0
endif


build:


build-plain: config-plain
	dh_testdir
	$(MAKE)

config-plain: config-plain-stamp
config-plain-stamp:
	@echo "*****Configuring plain teapop*****" 
	dh_testdir
	rm -f config-*-stamp

	./configure $(confflags)
	touch config-plain-stamp


build-mysql: config-mysql
	dh_testdir
	$(MAKE)

config-mysql: config-mysql-stamp
config-mysql-stamp:
	@echo "*****Configuring mysql teapop*****" 
	dh_testdir
	rm -f config-*-stamp
	./configure $(confflags) --with-mysql=/usr
	touch config-mysql-stamp


build-pgsql: config-pgsql
	dh_testdir
	$(MAKE)

config-pgsql: config-pgsql-stamp
config-pgsql-stamp:
	@echo "*****Configuring pgsql teapop*****" 
	dh_testdir
	rm -f config-*-stamp
	./configure $(confflags) --with-pgsql=/usr
	touch config-pgsql-stamp

build-ldap: config-ldap
	dh_testdir
	$(MAKE)

config-ldap: config-ldap-stamp
config-ldap-stamp:
	@echo "*****Configuring ldap teapop*****" 
	dh_testdir
	rm -f config-*-stamp
	./configure $(confflags) --with-ldap=openldap # --with-ldapinc=/usr
	touch config-ldap-stamp


clean: DH_OPTIONS=
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f config-*-stamp
	rm -f install-*-stamp
ifeq ($(PO2DEBCONF),yes)
	# Hack for woody compatibility. This makes sure that the
	# debian/templates file shipped in the source package doesn't
	# specify encodings, which woody's debconf can't handle. If building
	# on a system with po-debconf installed (conveniently debhelper (>=
	# 4.1.16) depends on it), the binary-arch target will generate a
	# better version for sarge.
	echo 1 > debian/po/output
	# Change the source file if they ever need different templates
	po2debconf debian/templates.master > debian/templates
	po2debconf debian/templates.master > debian/teapop-mysql.templates
	po2debconf debian/templates.master > debian/teapop-pgsql.templates
	po2debconf debian/templates.master > debian/teapop-ldap.templates
	rm -f debian/po/output
endif	

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean
	rm -f teapop/pop_mysql.o
	rm -f teapop/pop_pgsql.o
	rm -f teapop/pop_ldap.o
	dh_clean


install: install-plain install-mysql install-pgsql install-ldap

install-prep: DH_OPTIONS=
install-prep:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -A

install-plain: install-plain-stamp
install-plain-stamp: install-prep build-plain
	@echo "*****Installing plain teapop*****" 
	# Add here commands to install the package into debian/teapop.
	$(MAKE) install DESTDIR=`pwd`/debian/teapop
	touch install-plain-stamp


install-mysql: install-mysql-stamp
install-mysql-stamp: install-prep build-mysql
	@echo "*****Installing mysql teapop*****" 
	# Add here commands to install the package into debian/teapop-mysql.
	$(MAKE) install DESTDIR=`pwd`/debian/teapop-mysql
	touch install-mysql-stamp


install-pgsql: install-pgsql-stamp
install-pgsql-stamp: install-prep build-pgsql
	@echo "*****Installing pgsql teapop*****" 
	# Add here commands to install the package into debian/teapop-pgsql.
	$(MAKE) install DESTDIR=`pwd`/debian/teapop-pgsql
	touch install-pgsql-stamp


install-ldap: install-ldap-stamp
install-ldap-stamp: install-prep build-ldap
	@echo "*****Installing ldap teapop*****" 
	# Add here commands to install the package into debian/teapop-ldap.
	$(MAKE) install DESTDIR=`pwd`/debian/teapop-ldap
	touch install-ldap-stamp


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

# Build architecture-dependent files here.
binary-arch: DH_OPTIONS=-a
binary-arch: binary-arch-plain binary-arch-mysql binary-arch-pgsql binary-arch-ldap
#	dh_testversion
	dh_testdir
	dh_testroot

	# For woody compatibility
ifeq ($(PO2DEBCONF),yes)
	po2debconf -e utf8 debian/templates.master > debian/templates
	po2debconf -e utf8 debian/templates.master > debian/teapop-mysql.templates
	po2debconf -e utf8 debian/templates.master > debian/teapop-pgsql.templates
	po2debconf -e utf8 debian/templates.master > debian/teapop-ldap.templates
endif

	dh_installdebconf	
	dh_installdocs -XCVS
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
	dh_installinit
	dh_installcron
#	dh_installman -A
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs doc/ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
# -- -V'debconf-depends=debconf (>= $(MINDEBCONFVER))'
	dh_md5sums
	dh_builddeb

binary-arch-plain: build-plain install-plain

binary-arch-mysql: build-mysql install-mysql

binary-arch-pgsql: build-pgsql install-pgsql

binary-arch-ldap: build-ldap install-ldap

binary: binary-indep binary-arch

.PHONY: build build-plain build-mysql build-pgsql build-ldap config-plain config-mysql config-pgsql config-ldap clean install install-prep install-plain install-mysql install-pgsql install-ldap binary-indep binary-arch binary-arch-plain binary-arch-mysql binary-arch-pgsql binary-arch-ldap binary
