#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEBPKGNAME:=$(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
DATADIR=/usr/lib/paml/data

%:
	dh $@ --sourcedirectory=src

override_dh_installexamples:
	dh_installexamples
	for d in `find debian -name examples -a -type d`; do \
		find $$d \! -type d  | xargs -r chmod -x ; \
	done

override_dh_install:
	dh_install
	set -x ; \
	for datfile in debian/$(DEBPKGNAME)/$(DATADIR)/*.ctl ; do \
	    for item in seqfile treefile ; do \
		sed -i -e "s?\($${item} =\).*[^a-z^A-Z^0-9^.^ ]\+\([a-zA-Z0-9]\+\.[a-z]\+\)?\1 $(DATADIR)/\2?" \
		       -e "s?\($${item} =\) \([a-zA-Z0-9]\+\.[a-z]\+\)?\1 $(DATADIR)/\2?" \
		       $${datfile} ; \
	    done ; \
	done
	sed -i 's?\(aaRatefile = \)\(dat/jones.dat\)?\1$(DATADIR)/\2?' debian/$(DEBPKGNAME)/$(DATADIR)/codeml.ctl

override_dh_link:
	dh_link
	mkdir -p debian/$(DEBPKGNAME)/etc/$(DEBPKGNAME)/
	for datfile in debian/$(DEBPKGNAME)/$(DATADIR)/*.ctl ; do \
	    ln -s $(DATADIR)/`basename $${datfile}` debian/$(DEBPKGNAME)/etc/$(DEBPKGNAME) ; \
	done

override_dh_fixperms:
	dh_fixperms
	find debian/*/usr/lib/paml/data -type f -exec chmod -x \{\} \;
