#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

PACKAGE  := $(firstword $(shell dh_listpackages))
VERSION  := $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }')
UPSTREAM := $(shell echo "$(VERSION)"   | sed 's/-.\+$$//')

# Compile with -fPIC on all platforms (fix for #636204, #654240)
export DEB_CFLAGS_MAINT_APPEND   = -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
export DEB_CPPFLAGS_MAINT_APPEND = -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
export DEB_CXXFLAGS_MAINT_APPEND = -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
export DEB_FFLAGS_MAINT_APPEND   = -fPIC

%:
	dh $@ --with python2 --with autotools_dev

override_dh_auto_clean:
	( cd pdb2pka && \
	  if [ -e pMC_mult_wrap.cpp.saved ]; then mv pMC_mult_wrap.cpp.saved pMC_mult_wrap.cpp; fi && \
	  if [ -e pMC_mult.py.saved ]; then mv pMC_mult.py.saved pMC_mult.py; fi )
	dh_auto_clean

override_dh_auto_configure:
	dh_auto_configure
	( cd pdb2pka && \
	  cp -a pMC_mult_wrap.cpp pMC_mult_wrap.cpp.saved && \
	  cp -a pMC_mult.py pMC_mult.py.saved && \
	  swig -c++ -python -o pMC_mult_wrap.cpp pMC_mult.i )

# Disable tests, as they need an internet connection to download files
override_dh_auto_test:

override_dh_auto_install:
# Install Debian-provided stuff
#   Install wrappers
	for app in /usr/share/pdb2pqr/pdb2pqr.py /usr/share/pdb2pqr/src/psize.py; do \
		sed -e "s,@SCRIPT@,$${app}," debian/wrapper.sh \
			>debian/pdb2pqr/usr/bin/$$(basename $${app} .py) && \
		chmod 0755 debian/pdb2pqr/usr/bin/$$(basename $${app} .py); \
	done
	for app in /usr/share/pdb2pqr/propka30/propka.py; do \
		sed -e "s,@SCRIPT@,$${app}," debian/wrapper.sh \
			>debian/pdb2pqr/usr/bin/$$(basename $${app} .py) && \
		chmod 0755 debian/pdb2pqr/usr/bin/$$(basename $${app} .py); \
	done
#   Install man pages
	xmlto -o debian/pdb2pqr/usr/share/man/man1/ man debian/pdb2pqr.1.xml
	xmlto -o debian/pdb2pqr/usr/share/man/man1/ man debian/psize.1.xml
	xmlto -o debian/pdb2pqr/usr/share/man/man1/  man debian/propka.1.xml
#	dh_auto_install

override_dh_fixperms:
	dh_fixperms
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/main.py
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/main_cgi.py
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/pdb2pka/ligandclean/__init__.py
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/pdb2pka/pka_routines.py
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/pdb2pka/pKaIO_compat.py
	chmod 0644 debian/pdb2pqr/usr/share/pdb2pqr/pdb2pka/pKa_utility_functions_compat.py
	chmod 0755 debian/pdb2pqr/usr/share/pdb2pqr/propka30/propka.py

# Remark: The following uscan command requires devscripts > 2.12.4 which is not
#         yet released at the time of this package release.  The code can be obtained
#         via
#   git clone git://tille@git.debian.org/git/users/tille/devscripts.git
#         and then use scripts/uscan.pl
get-orig-source:
	uscan --verbose --force-download --repack-compression xz
