#!/usr/bin/make -f


export DEB_BUILD_MAINT_OPTIONS = hardening=+all

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

DEBPKGNAME     := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
LIBNAME	       := libsquid

%:
	dh $@ --no-parallel

override_dh_auto_clean:
	dh_auto_clean
	rm -f configure

override_dh_auto_configure:
	# autoreconf && true
	autoconf
	dh_auto_configure --  --enable-lfs --enable-pvm
	# avoid duplicated definition of PACKAGE_NAME (basically conflicting with hmmer2 when used together)
	sed -i -e '/^#define PACKAGE_NAME /i #ifndef PACKAGE_NAME' \
	       -e '/^#define PACKAGE_NAME /a #endif' \
	       -e '/^#define PACKAGE_VERSION /i #ifndef PACKAGE_VERSION' \
	       -e '/^#define PACKAGE_VERSION /a #endif' \
	       -e '/^#define _LARGEFILE_SOURCE /i #ifndef _LARGEFILE_SOURCE' \
	       -e '/^#define _LARGEFILE_SOURCE /a #endif' \
	       -e '/^#define _LARGEFILE64_SOURCE /i #ifndef _LARGEFILE64_SOURCE' \
	       -e '/^#define _LARGEFILE64_SOURCE /a #endif' \
	       -e '/^#define VERSION /i #ifndef VERSION' \
	       -e '/^#define VERSION /a #endif' \
	       squidconf.h

override_dh_install:
	mv $(CURDIR)/debian/tmp/usr/bin/translate $(CURDIR)/debian/tmp/usr/bin/stranslate
	dh_install
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-la \
		    --override s/libpvm3-3-dev/pvm-dev/ \
		    --movedev debian/tmp/usr/include/* usr/include \
		    --movedev squid.h usr/include/$(DEBPKGNAME) \
		    --movedev squidconf.h usr/include/$(DEBPKGNAME) \
		    --movedev "debian/tmp/usr/lib/*/pkgconfig/*.pc" usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig \
		    debian/tmp/usr/lib/*/$(LIBNAME).so
	# resolve @LICENSE@ by text in header files
	for HEADER in $(CURDIR)/debian/$(LIBNAME)-dev/usr/include/$(DEBPKGNAME)/*.h ; do \
	  if grep -q '@LICENSE@' $${HEADER} ; then \
	    sed '/@LICENSE@/,$$d' $${HEADER} > $${HEADER}_tmp ; \
	    sed 's/^/ * /' Licenses/gnu     >> $${HEADER}_tmp ; \
	    sed '0,/@LICENSE@/d' $${HEADER} >> $${HEADER}_tmp ; \
	    mv $${HEADER}_tmp $${HEADER} ; \
	  fi ; \
	done

override_dh_installexamples:
	dh_installexamples
	cd $(CURDIR)/debian/biosquid-dev/usr/share/doc/biosquid-dev/examples/Testsuite; make clean
	sed -i 's#/usr/local/bin/perl#/usr/bin/perl#' $(CURDIR)/debian/$(LIBNAME)-dev/usr/share/doc/$(LIBNAME)-dev/examples/Formats/*.pl

override_dh_auto_test:
	chmod u+x Testsuite/x-base-* Testsuite/bug-1-sfetch*
	ln -s .libs/libsquid.a
	dh_auto_test

override_dh_installman:
	dh_installman
	for manpage in Man/*.man; do \
	    cp -a $${manpage} $(CURDIR)/debian/biosquid/usr/share/man/man1/`basename $${manpage} .man`.1 ; \
	done
