#!/usr/bin/make -f

DEB_TAR_SRCDIR := $(shell basename $(wildcard *.tgz) .tgz)

include /usr/share/cdbs/1/rules/tarball.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/simple-patchsys.mk

PHP_EX5=$(shell /usr/bin/php-config5 --extension-dir)

DEB_INSTALL_EXAMPLES_php5-memcache = $(DEB_SRCDIR)/example.php

#
# local hacks
#

patch: apply-patches

clean::
	rm -f php[5]-memcache.postrm php[5]-memcache.postinst
	rm -f *cdbs*
	rm -rf build-tree

configure_for_php%:
	cd $(DEB_SRCDIR) && phpize --clean && phpize && \
	    ./configure --with-memcache --with-php-config=/usr/bin/php-config$*
	sed -e 's/phpX/php$*/g' < debian/phpX-memcache.postinst > debian/php$*-memcache.postinst
	sed -e 's/phpX/php$*/g' < debian/phpX-memcache.postrm   > debian/php$*-memcache.postrm

#
# cdbs things
#

define install_rule
	chmod a-x $(DEB_SRCDIR)/example.php
	$(MAKE) -C $(DEB_SRCDIR)
	mkdir -p debian/php$1-memcache$(PHP_EX$1)
	install -m 644 -o root -g root $(DEB_SRCDIR)/modules/memcache.so debian/php$1-memcache$(PHP_EX$1)/memcache.so
	mkdir -p debian/php$1-memcache/usr/share/lintian/overrides
	echo "php5-memcache: no-shlibs-control-file $(PHP_EX$1)/memcache.so" > debian/php$1-memcache/usr/share/lintian/overrides/php$1-memcache
	echo "php$1:Depends=phpapi-`php-config$1 --phpapi`, php$1-common" >> debian/php$1-memcache.substvars
	mkdir -p debian/php$1-memcache/usr/share/php$1-memcache/
	cp debian/memcache.ini  debian/php$1-memcache/usr/share/php$1-memcache/memcache.ini-dist
endef

install/php5-memcache:: configure_for_php5
	$(call install_rule,5)
