#!/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)
CFLAGS   += -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
CPPFLAGS += -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
CXXFLAGS += -fPIC -I/usr/share/pyshared/numpy/core/include/numpy
FFLAGS   += -fPIC

%:
	dh $@ --with python2

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 -- CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" CXXFLAGS="$(CXXFLAGS)" FFLAGS="$(FFLAGS)" LDFLAGS="$(LDFLAGS)"
	( 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

.PHONY: get-orig-source
get-orig-source:
	rm -rf get-orig-source
	mkdir -p get-orig-source
	uscan --no-conf --no-symlink --force-download --download-current-version --destdir=get-orig-source
	tar xzf get-orig-source/$(PACKAGE)-$(UPSTREAM).tar.gz -C get-orig-source \
		--exclude="contrib" \
		--exclude="jmol"
	mv get-orig-source/$(PACKAGE)-$(UPSTREAM) get-orig-source/$(PACKAGE)-$(UPSTREAM).repack
	GZIP='--best --no-name' tar czf ../$(PACKAGE)_$(UPSTREAM).orig.tar.gz -C get-orig-source $(PACKAGE)-$(UPSTREAM).repack
	rm -rf get-orig-source
