#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

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

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

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

PHP4_SOURCE_VERSION   = $(shell dpkg-parsechangelog | grep ^Version | sed "s/Version: //")
PHP4_UPSTREAM_VERSION = $(shell echo $(PHP4_SOURCE_VERSION) | sed -e "s/-.*//" -e "s/.*://")
PHP4_DEBIAN_REVISION  = $(shell echo $(PHP4_SOURCE_VERSION) | sed "s/.*-//")

PROG_SENDMAIL = /usr/sbin/sendmail
CFLAGS = -O2 -Wall -fsigned-char -fno-strict-aliasing
LFSFLAGS = -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64

# Enable IEEE-conformant floating point math on alphas (not the default)
ifeq (alpha-linux,$(DEB_HOST_GNU_TYPE))
  CFLAGS += -mieee
endif

ifeq (ia64-linux,$(DEB_HOST_GNU_TYPE))
  CFLAGS += -g
else
  CFLAGS += -gstabs
endif

php4ver=$(shell head -1 debian/changelog | cut -d\  -f 2 | sed 's/[()]//g')
zendapiver=$(shell egrep '\#define ZEND_MODULE_API_NO ' /usr/include/php4/Zend/zend_modules.h|sed 's/\#define ZEND_MODULE_API_NO //')
phpapiver=$(shell grep '\#define PHP_API_VERSION ' /usr/include/php4/main/php.h|sed 's/\#define PHP_API_VERSION //')

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	cd build-imap && $(MAKE)

	touch $@

configure: configure-stamp
configure-stamp:
	dh_testdir
	if [ -d build-imap ]; then rm -rf build-imap; fi
	-mkdir build-imap
	cd build-imap && \
        CFLAGS="$(CFLAGS)" PROG_SENDMAIL="$(PROG_SENDMAIL)" ../configure \
		--prefix="$(shell php-config --prefix)" \
		--with-imap --with-kerberos

	touch configure-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-stamp
	rm -f install-stamp

	rm -rf build-imap
	rm -f debian/copyright

	dh_clean
	cat debian/modulelist | while read package extname dsoname; do \
		rm -f debian/php4-$$package.postinst \
		   debian/php4-$$package.prerm \
		   debian/php4-$$package.config \
		   debian/php4-$$package.templates; \
	done

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd build-imap && $(MAKE) install INSTALL_ROOT=$(CURDIR)/debian/php4-imap

	touch install-stamp

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	# Need this version of debhelper for DH_OPTIONS to work.
	dh_testdir
	dh_testroot
	dh_installdebconf
	cat debian/copyright.header LICENSE Zend/LICENSE > debian/copyright

#	dh_installdocs
#	dh_installexamples

#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
#	dh_installmanpages
#	dh_installinfo
#	dh_undocumented
#	dh_installchangelogs
	dh_link
	dh_compress -Xphp.ini
	dh_fixperms
#	# You may want to make some executables suid here.
#	dh_suidregister
	dh_installdeb
#	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
#binary-arch: DH_OPTIONS=-a
binary-arch: build install
	# Need this version of debhelper for DH_OPTIONS to work.
	dh_testdir
	dh_testroot
	# Do this first so we don't overwrite any debhelper-generated files
	cat debian/modulelist | while read package extname dsoname; do \
		if [ -z "$$dsoname" ]; then dsoname=$$package; fi; \
		sed -e"s/@extname@/$$extname/g; s/@dsoname@/$$dsoname/g" \
		  < debian/php4-module.postinst > debian/php4-$$package.postinst; \
		sed -e"s/@extname@/$$extname/g; s/@dsoname@/$$dsoname/g" \
		  < debian/php4-module.prerm > debian/php4-$$package.prerm; \
		sed -e"s/@extname@/$$extname/g; s/@dsoname@/$$dsoname/g" \
		  < debian/php4-module.config > debian/php4-$$package.config; \
		cp debian/php4-module.templates debian/php4-$$package.templates; \
	done
	dh_installdebconf -a
	cat debian/copyright.header LICENSE Zend/LICENSE > debian/copyright
	dh_installdocs -a

	cat debian/modulelist | while read package extname dsoname; do \
		rm -rf debian/php4-$$package/usr/share/doc/php4-$$package; \
		ln -s php4-common debian/php4-$$package/usr/share/doc/php4-$$package; \
	done

#	dh_installexamples
#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron -pphp4-common --name=php4
#	dh_installmanpages
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs
	dh_strip -a
	dh_link -a
	dh_compress -a -Xphp.ini
	dh_fixperms -a -X /var/lib/php4
#	# You may want to make some executables suid here.
#	dh_suidregister
	dh_installdeb -a
#	dh_makeshlibs
#	dh_perl
	dh_shlibdeps -a

	cat debian/modulelist | while read package extname dsoname; do \
		echo "php:Depends=phpapi-$(phpapiver)" >> debian/php4-$$package.substvars; \
		echo "php:UpstreamVersion=$(PHP4_UPSTREAM_VERSION)" >> debian/php4-$$package.substvars; \
	done

	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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