#!/usr/bin/make -f

#export DH_VERBOSE=1

include /usr/share/cdbs/1/rules/buildvars.mk

top_dir      := $(shell pwd)

upstream_dir := $(top_dir)/upstream
build_dir    := $(top_dir)/build_dir
patch_dir    := $(top_dir)/debian/patches
install_dir  := $(top_dir)/debian/tmp
skel_dir  := $(top_dir)/debian/skels
skelout_dir  := $(top_dir)/debian

root_tex_install_target := usr/share/texmf/tex/latex/
root_tex_doc_install_target := usr/share/texmf/doc/latex/
tex_install_target := ${root_tex_install_target}/upmethodology-extensions
bst_install_target := usr/share/texmf/bibtex/bst/upmethodology-extensions
beamer_install_target := ${root_tex_install_target}/beamer/themes
beamer_doc_target := ${root_tex_doc_install_target}/beamer/themes
beamer_bst_install_target := usr/share/texmf/bibtex/bst/beamer

build_src    := $(build_dir)/tex-templates

showvars:
	@echo "top_dir=$(top_dir)"
	@echo "upstream_dir=$(upstream_dir)"
	@echo "build_dir=$(build_dir)"
	@echo "patch_dir=$(patch_dir)"
	@echo "install_dir=$(install_dir)"
	@echo "build_src=$(build_src)"
	@echo "skel_dir=$(skel_dir)"
	@echo "skelout_dir=$(skelout_dir)"

build: build-stamp

configure-stamp: unpack-stamp
	dh_testdir
	touch $@

build-stamp: configure-stamp
	dh_testdir
	cd $(build_src)
	touch $@

prepare-debian-directory:
	rm -rf $(build_dir) 2>/dev/null >/dev/null
	mkdir -p -m=rwx,go=rx $(build_dir)
	mkdir -p -m=rwx,go=rx $(build_src)
	dh_testdir

	# Create Debian files from the skels
	@cp "$(skel_dir)/tex-templates-utbm.templates" "$(skelout_dir)/tex-templates-utbmciadreport-2019.templates"
	@cp "$(skel_dir)/tex-templates-utbm.templates" "$(skelout_dir)/tex-templates-utbmciadreport-2021.templates"
	@cp "$(skel_dir)/tex-templates-utbm.templates" "$(skelout_dir)/tex-templates-utbm-phdthesis.templates"
	@cp "$(skel_dir)/tex-templates-utbm.templates" "$(skelout_dir)/tex-templates-ciad-beamer.templates"
	@cp "$(skel_dir)/tex-templates-ufc.templates" "$(skelout_dir)/tex-templates-ufc-phdthesis.templates"
	@cp "$(skel_dir)/tex-templates-ub.templates" "$(skelout_dir)/tex-templates-ub-phdthesis.templates"
	@cp "$(skel_dir)/tex-templates-spimhdr.templates" "$(skelout_dir)/tex-templates-spimhdr.templates"

	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-utbmciadreport-2019.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-utbmciadreport-2021.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-phdthesis-base.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-utbm-phdthesis.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-ufc-phdthesis.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-ub-phdthesis.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-spimhdr.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-ciad-beamer.postinst"
	@cp "$(skel_dir)/tex-templates.postinst" "$(skelout_dir)/tex-templates-ingedoc.postinst"

	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-utbmciadreport-2019.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-utbmciadreport-2021.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-phdthesis-base.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-utbm-phdthesis.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-ufc-phdthesis.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-ub-phdthesis.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-spimhdr.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-ciad-beamer.postrm"
	@cp "$(skel_dir)/tex-templates.postrm" "$(skelout_dir)/tex-templates-ingedoc.postrm"

	@perl "$(top_dir)/debian/mktemplate.perl" "Template for reports of the CIAD lab 2019" "$(skel_dir)/tex-templates-utbm.preinst" "$(skelout_dir)/tex-templates-utbmciadreport-2019.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for reports of the CIAD lab 2021" "$(skel_dir)/tex-templates-utbm.preinst" "$(skelout_dir)/tex-templates-utbmciadreport-2021.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for PhD thesis of the UTBM" "$(skel_dir)/tex-templates-utbm.preinst" "$(skelout_dir)/tex-templates-utbm-phdthesis.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for Beamer presentation of the CIAd lab" "$(skel_dir)/tex-templates-utbm.preinst" "$(skelout_dir)/tex-templates-ciad-beamer.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for PhD thesis of the UFC" "$(skel_dir)/tex-templates-ufc.preinst" "$(skelout_dir)/tex-templates-ufc-phdthesis.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for PhD thesis of the UB" "$(skel_dir)/tex-templates-ub.preinst" "$(skelout_dir)/tex-templates-ub-phdthesis.preinst"
	@perl "$(top_dir)/debian/mktemplate.perl" "Template for HDR of SPIM" "$(skel_dir)/tex-templates-spimhdr.preinst" "$(skelout_dir)/tex-templates-spimhdr.preinst"

	# Force updating of DebConf internationalization
	@debconf-updatepo

unpack-stamp: prepare-debian-directory
	# unpack upstream tarballs
	@cd $(build_dir)\
		&& for f in $(upstream_dir)/*.tar.gz ; do\
			echo "unpacking $$f"; \
			tar zxfp "$$f"; \
		   done;

	# Check file permissions for ensuring that the input archive files
	# have an expected permission rights
	@cd $(build_dir) \
		&& for f in `find -type d` ; do \
			PERM=`stat --printf "%a" "$$f"`; \
			if test "$$PERM" -ne 755; then \
				echo "Invalid file permission $$PERM for $(build_dir)/$$f"; \
				false; \
			fi; \
		done
	@cd $(build_dir) \
		&& for f in `find -type f` ; do \
			PERM=`stat --printf "%a" "$$f"`; \
			if test "$$PERM" -ne 755 -a "$$PERM" -ne 644; then \
				echo "Invalid file permission $$PERM for $(build_dir)/$$f"; \
				false; \
			fi; \
		done

	# apply any patches
	@cd $(build_src) \
		&& for p in $(wildcard $(patch_dir)/*.patch); do \
			patch -p0 < $$p; \
		done;
	
	# Remove obsolete files for the sources
	find "$(build_src)" '(' -name "Makefile" -o -name "*-doc.pdf" ')' -a -exec 'rm' '{}' ';'
	${RM} -f "$(build_src)/phd_thesis/"*.lyx
	${RM} -f "$(build_src)/phd_thesis/"*.pdf

	touch $@

binary-indep:
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install --sourcedir=debian/tmp --autodest --fail-missing
	dh_installdocs
	dh_installchangelogs
	dh_installdebconf
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb -- -Zgzip

binary: binary-indep binary-arch

install: install-stamp

install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	# Creating directories
	install -d -m 0755 "$(install_dir)/$(tex_install_target)"
	install -d -m 0755 "$(install_dir)/$(bst_install_target)"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/base"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/outer"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/inner"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/font"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/color"
	install -d -m 0755 "$(install_dir)/$(beamer_install_target)/theme"
	install -d -m 0755 "$(install_dir)/$(beamer_doc_target)/ciad/docfigs"
	install -d -m 0755 "$(install_dir)/$(beamer_bst_install_target)"
	install -d -m 0755 "$(install_dir)/$(root_tex_install_target)/ingedoc-conf"
	install -d -m 0755 "$(install_dir)/$(root_tex_doc_install_target)/ingedoc-conf"
	# Installing Beamer files for CIAD template
	install -m 0644 "$(build_src)/presentations/ciad-2021/"*.cls "$(install_dir)/$(beamer_install_target)"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamerbasetheme*.sty "$(install_dir)/$(beamer_install_target)/base"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamerhandouttheme*.sty "$(install_dir)/$(beamer_install_target)/base"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beameroutertheme*.sty "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamerinnertheme*.sty "$(install_dir)/$(beamer_install_target)/inner"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamerfonttheme*.sty "$(install_dir)/$(beamer_install_target)/font"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamercolortheme*.sty "$(install_dir)/$(beamer_install_target)/color"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"beamertheme*.sty "$(install_dir)/$(beamer_install_target)/theme"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad.bst "$(install_dir)/$(beamer_bst_install_target)"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"*logo.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"*logointitle.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"*logoontitleslide.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/frenchrepublic.pdf" "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-background*.png "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-background*.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-background*.jpg "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-*-background.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-*-background-*.png "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-*-background-*.pdf "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"ciad-title-hands.png "$(install_dir)/$(beamer_install_target)/outer"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"documentation.tex "$(install_dir)/$(beamer_doc_target)/ciad"
	install -m 0644 "$(build_src)/presentations/ciad-2021/"documentation.pdf "$(install_dir)/$(beamer_doc_target)/ciad"
	install -m 0644 "$(build_src)/presentations/ciad-2021/docfigs/"* "$(install_dir)/$(beamer_doc_target)/ciad/docfigs"
	# Installing IngeDoc files
	install -m 0644 "$(build_src)/papers/ingedoc/"*.cls "$(install_dir)/$(root_tex_install_target)/ingedoc-conf"
	install -m 0644 "$(build_src)/papers/ingedoc/IngeDocGuidelines.pdf" "$(install_dir)/$(root_tex_doc_install_target)/ingedoc-conf"
	# Removing the obsolete templates to avoid a copy during the next step
	${RM} -r "$(build_src)/presentations"
	${RM} -r "$(build_src)/papers"
	${RM} -r "$(build_src)/reports/"*-deprecated
	${RM} -r "$(build_src)/hdr"
	# Installing tex-templates files
	cd $(build_src) && for fff in ./*; do \
		if test -d "$$fff"; then \
			cp -Rf "$$fff" "$(install_dir)/$(tex_install_target)/"; \
			chmod -R ugo+rX,u+w "$(install_dir)/$(tex_install_target)/$$fff"; \
		fi; \
	done
	# Remove the base directory for PhD thesis, which must be treated in a separate process
	${RM} -r "$(install_dir)/$(tex_install_target)/phd_thesis/share"
	# Remove the LyX layouts that will be treated later
	find "$(install_dir)/$(tex_install_target)" -name "*.layout" -exec 'rm' '{}' ';'
	# Installing shared STYs and resources
	install -d -m 0755 "$(install_dir)/$(tex_install_target)/phd_thesis/base"; \
	install -m 0644 "$(build_src)/phd_thesis/share/sty/"*.sty "$(install_dir)/$(tex_install_target)/phd_thesis/base/"; \
	install -m 0644 "$(build_src)/phd_thesis/share/sty/"*.pdf "$(install_dir)/$(tex_install_target)/phd_thesis/base/"; \
	install -d -m 0755 "$(install_dir)/$(bst_install_target)/phd_thesis/base"; \
	install -m 0644 "$(build_src)/phd_thesis/share/bst/"*.bst "$(install_dir)/$(bst_install_target)/phd_thesis/base/"; \
	install -d -m 0755 "$(install_dir)/$(bst_install_target)/reports/utbmciad-2019"; \
	install -d -m 0755 "$(install_dir)/$(bst_install_target)/reports/utbmciad-2021"; \
	install -m 0644 "$(build_src)/reports/utbmciad-2019/"*.bst "$(install_dir)/$(bst_install_target)/reports/utbmciad-2019/"; \
	install -m 0644 "$(build_src)/reports/utbmciad-2021/"*.bst "$(install_dir)/$(bst_install_target)/reports/utbmciad-2021/"; \
	${RM} "$(install_dir)/$(tex_install_target)/reports/utbmciad-2019/"*.bst; \
	${RM} "$(install_dir)/$(tex_install_target)/reports/utbmciad-2021/"*.bst; \
	touch $@

clean:
	dh_testdir
	dh_testroot
	@${RM} -rf $(build_dir) *-stamp
	dh_clean
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2019.templates"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2021.templates"
	@${RM} "$(skelout_dir)/tex-templates-utbm-phdthesis.templates"
	@${RM} "$(skelout_dir)/tex-templates-ufc-phdthesis.templates"
	@${RM} "$(skelout_dir)/tex-templates-ub-phdthesis.templates"
	@${RM} "$(skelout_dir)/tex-templates-spimhdr.templates"
	@${RM} "$(skelout_dir)/tex-templates-ciad-beamer.templates"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2019.postinst"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2021.postinst"
	@${RM} "$(skelout_dir)/tex-templates-utbm-phdthesis.postinst"
	@${RM} "$(skelout_dir)/tex-templates-ufc-phdthesis.postinst"
	@${RM} "$(skelout_dir)/tex-templates-ub-phdthesis.postinst"
	@${RM} "$(skelout_dir)/tex-templates-spimhdr.postinst"
	@${RM} "$(skelout_dir)/tex-templates-ciad-beamer.postinst"
	@${RM} "$(skelout_dir)/tex-templates-ingedoc.postinst"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2019.postrm"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2021.postrm"
	@${RM} "$(skelout_dir)/tex-templates-utbm-phdthesis.postrm"
	@${RM} "$(skelout_dir)/tex-templates-ufc-phdthesis.postrm"
	@${RM} "$(skelout_dir)/tex-templates-ub-phdthesis.postrm"
	@${RM} "$(skelout_dir)/tex-templates-spimhdr.postrm"
	@${RM} "$(skelout_dir)/tex-templates-ciad-beamer.postrm"
	@${RM} "$(skelout_dir)/tex-templates-ingedoc.postrm"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2019.preinst"
	@${RM} "$(skelout_dir)/tex-templates-utbmciadreport-2021.preinst"
	@${RM} "$(skelout_dir)/tex-templates-utbm-phdthesis.preinst"
	@${RM} "$(skelout_dir)/tex-templates-ufc-phdthesis.preinst"
	@${RM} "$(skelout_dir)/tex-templates-ub-phdthesis.preinst"
	@${RM} "$(skelout_dir)/tex-templates-spimhdr.preinst"
	@${RM} "$(skelout_dir)/tex-templates-ciad-beamer.preinst"


.PHONY: binary-indep binary-arch binary clean build install prepare-debian-directory

