#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# DEB_BUILD_OPTIONS=nocheck

DEBPKGNAME := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
DOCPKGNAME := $(DEBPKGNAME)-doc
DATPKGNAME := $(DEBPKGNAME)-data

%:
	dh $@ --with autotools_dev

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --sourcedirectory=doc
	rm -f doc/presentation/presentation*.pdf
	find doc -name "*.aux" -delete
	find doc -name "*.log" -delete
	rm -f doc/theory/ELF/wf_elecden_kinden_elf.pdf doc/theory/ELF/test_report/test_report_elf.pdf
	# clean up test suite
	rm -rf tests/Test_suite
	rm -f  tests/test_suite.cpkl
	find tests -name "*.pyc" -delete

override_dh_auto_configure:
	dh_auto_configure -- --with-linalg-ldflags="-lblas -llapack"
	dh_auto_configure --sourcedirectory=doc -- --prefix=/usr

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --sourcedirectory=doc
	cd doc/theory/ELF && pdflatex -interaction=nonstopmode --shell-escape wf_elecden_kinden_elf.tex
	cd doc/theory/ELF/test_report && pdflatex -interaction=nonstopmode --shell-escape test_report_elf.tex
	cd doc/theory/KDEN && pdflatex -interaction=nonstopmode --shell-escape test_report_kden.tex
	cd doc/theory/MGGA && pdflatex -interaction=nonstopmode --shell-escape report_MGGA.tex

override_dh_auto_install:
	dh_auto_install
	dh_auto_install --sourcedirectory=doc
	# Installing the LaTeX source inside the abinit-doc package does not
	# make any sense
	find debian -type f -name "*.tex" -delete
	# remove documentation for building the software from user docs
	rm -rf debian/$(DOCPKGNAME)/usr/share/doc/$(DEBPKGNAME)/help_make
	# change example input files psp location to /usr/share/abinit/psp
	for i in `ls debian/$(DATPKGNAME)/usr/share/doc/abinit/examples/*.files`; do	\
		sed -i -e s#../../../Psps_for_tests/HGH/#/usr/share/abinit/psp/#g	\
		       -e s#../../../Psps_for_tests/#/usr/share/abinit/psp/#g		\
		       -e s#../../Psps_for_tests/#/usr/share/abinit/psp/#g $$i;		\
	done
	# remove HGH subdirectory as we install the files in there directly to
	# /usr/share/abinit/psp
	rm -rf debian/$(DATPKGNAME)/usr/share/abinit/psp/HGH

override_dh_compress:
	dh_compress -X.pdf -X.in -X.in_freq

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	(cd tests; ./runtests.py fast)
endif
