#!/usr/bin/make -f
# -*- makefile -*-

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

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

autoreconf:
	aclocal
	autoreconf -f -i

override_dh_autoreconf:
	dh_autoreconf debian/rules -- autoreconf

override_dh_auto_configure:
	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess . 
	dh_auto_configure -- --cache-file=config.cache --includedir=/usr/include/ossp --without-perl --without-dce --with-cxx

override_dh_auto_build:
	dh_auto_build

	# rebuild man pages
	make man

	# build perl bindings
	cd perl; \
	/usr/bin/perl Makefile.PL INSTALLDIRS=vendor COMPAT=1; \
	make CC="$(CC)" OPTIMIZE="$(CFLAGS)"; \
	make test

	# build php bindings
	#cd php; \
	#phpize5; \
	#mkdir build-php5; \
	#cd build-php5; \
	#CC="$(CC)" CFLAGS="$(CFLAGS) -I../.. -L../.. -L../../.libs" \
	#../configure \
	#    --cache-file=../config.cache \
	#    --enable-uuid \
	#    --disable-static \
	#    --with-php-config=php-config5; \
	#make; \
	#cd ..; \
	#LD_LIBRARY_PATH=`pwd`/../.libs \
	#php5 -q -d "safe_mode=0" -d "extension=uuid.so" -d "extension_dir=./build-php5/modules" uuid.ts 5

override_dh_auto_install:
	dh_auto_install

	cd perl; make install PREFIX=$(CURDIR)/debian/libossp-uuid-perl/usr
	chrpath debian/libossp-uuid-perl/usr/lib/perl5/auto/OSSP/uuid/uuid.so && \
	chrpath -d debian/libossp-uuid-perl/usr/lib/perl5/auto/OSSP/uuid/uuid.so

	#cd php/build-php5; make install INSTALL_ROOT=$(CURDIR)/debian/php5-uuid
	#chmod -x debian/php5-uuid/usr/lib/php5/*/uuid.so
	#chrpath debian/php5-uuid/usr/lib/php5/*/uuid.so && \
	#chrpath -d debian/php5-uuid/usr/lib/php5/*/uuid.so

override_dh_install:
	dh_install

	# rename manpages to not conflict with libuuid-dev
	for i in debian/libossp-uuid-dev/usr/share/man/man3/*.3; do mv $$i $${i}ossp; done

	install -D -m 644 debian/tmp/usr/lib/pkgconfig/uuid.pc debian/libossp-uuid-dev/usr/lib/pkgconfig/ossp-uuid.pc

#override_dh_gencontrol:
#	dh_gencontrol -- -Vphp5-uuid:Depends=phpapi-$(shell php-config5 --phpapi)

override_dh_auto_clean:
	cd perl; test -f Makefile && touch Makefile && make distclean || true
	#rm -rf php/build-php5
	cd php; phpize --clean || true
	dh_auto_clean
	rm -f config.sub config.guess
	rm -f uuid.1 uuid.3 uuid++.3 uuid-config.1

%:
	dh  $@ --with autoreconf
