#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

PHP_PREFIX=/usr
phpapiver=$(shell grep '\#define PHP_API_VERSION ' $(PHP_PREFIX)/include/php4/main/php.h |sed 's/\#define PHP_API_VERSION //')
extdir=$(shell $(PHP_PREFIX)/bin/php-config --extension-dir)

CFLAGS = -Wall

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0 -g
else
	CFLAGS += -O3
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

patch: patch-stamp
patch-stamp:
	dh_testdir
	for patch in `ls debian/patches/*.diff | sort`; do \
	        echo '->'`basename $$patch`:; \
		if ! patch -p0 --ignore-whitespace --dry-run < $$patch; \
		then \
			exit 1; \
		fi; \
	        patch -p0 --ignore-whitespace < $$patch; \
	done
	touch patch-stamp

unpatch:
	dh_testdir
	if [ -f patch-stamp ]; then \
		for patch in `ls debian/patches/*.diff | sort -r`; do \
			patch -p0 -R --ignore-whitespace < $$patch; \
		done; \
	fi
	rm -f patch-stamp



configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	# Check for existence of build/shtool and set perms if it exists
	if test -d build; then chmod u+x build/shtool; fi
	$(PHP_PREFIX)/bin/phpize
	./configure --enable-eaccelerator=shared --with-php-config=$(PHP_PREFIX)/bin/php-config
	cd ./eLoader
	./autogen.sh
	./configure --with-php-config=$(PHP_PREFIX)/bin/php-config
	cd ..
	touch configure-stamp


build: build-stamp

build-stamp: patch-stamp configure-stamp 
	dh_testdir

	$(MAKE)
	cd eLoader
	$(MAKE)
	cd ..

	touch build-stamp

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

	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	rm -f config.log config.status
	phpize --clean
	cd eLoader
	-$(MAKE) clean
	rm -f config.log config.status
	phpize --clean
	cd ..
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	mkdir -p debian/php4-eaccelerator/$(extdir)
	install -m 755 modules/eaccelerator.so debian/php4-eaccelerator$(extdir)/eaccelerator.so
	install -m 755 eLoader/modules/eloader.so debian/php4-eaccelerator$(extdir)/eloader.so

# 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_installchangelogs 
	dh_installdocs
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	echo "php:Depends=phpapi-$(phpapiver)" >> debian/php4-eaccelerator.substvars
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

