#!/usr/bin/make -f

SHELL=/bin/sh -e

PECL_NAME    := $(shell dpkg-parsechangelog -SSource | sed -e s/php-//)
INSTALL_ROOT := $(CURDIR)/debian/php-$(PECL_NAME)

PHP_VERSIONS := $(shell /usr/sbin/phpquery -V)

CONFIGURE_TARGETS := $(addprefix configure-,$(addsuffix -stamp,$(PHP_VERSIONS)))
BUILD_TARGETS     := $(addprefix build-,$(addsuffix -stamp,$(PHP_VERSIONS)))
INSTALL_TARGETS   := $(addprefix install-,$(addsuffix -stamp,$(PHP_VERSIONS)))
CLEAN_TARGETS     := $(addprefix clean-,$(addsuffix -stamp,$(PHP_VERSIONS)))

%:
	dh $@ --with php

override_dh_auto_configure: $(CONFIGURE_TARGETS)
override_dh_auto_build: $(BUILD_TARGETS)
override_dh_auto_install: $(INSTALL_TARGETS)

override_dh_auto_clean: $(CLEAN_TARGETS)
	-rm $(CONFIGURE_TARGETS) $(BUILD_TARGETS) $(INSTALL_TARGETS) $(CLEAN_TARGETS)

configure-%-stamp: SOURCE_DIR = $(shell ls -1d php-$*/$(PECL_NAME)-*)
configure-%-stamp:
	cd $(SOURCE_DIR) && phpize$*
	dh_auto_configure --sourcedirectory=$(SOURCE_DIR) -- --enable-$(PECL_NAME) --with-php-config=/usr/bin/php-config$*
	touch configure-$*-stamp

build-%-stamp: SOURCE_DIR = $(shell ls -1d php-$*/$(PECL_NAME)-*)
build-%-stamp:
	dh_auto_build --sourcedirectory=$(SOURCE_DIR)
	touch build-$*-stamp

install-%-stamp: SOURCE_DIR = $(shell ls -1d php-$*/$(PECL_NAME)-*)
install-%-stamp:
	dh_auto_install --sourcedirectory=$(SOURCE_DIR) -- INSTALL_ROOT=$(INSTALL_ROOT)
	touch install-$*-stamp

clean-%-stamp: SOURCE_DIR = $(shell ls -1d php-$*/$(PECL_NAME)-*)
clean-%-stamp:
	cd $(SOURCE_DIR) && phpize --clean
	dh_auto_clean --sourcedirectory=php-$*
	touch clean-$*-stamp
