#!/usr/bin/make -f

# Name and version are pulled from the changelog automatically
PACKAGE  := $(shell dpkg-parsechangelog -S Source)
VERSION  := $(shell dpkg-parsechangelog -S Version | cut -d- -f1)
TARBALL  := $(wildcard upstream/*.tar.gz)

%:
	dh $@ --with python3

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

override_dh_auto_clean:
	rm -rf build/
	dh_auto_clean

override_dh_auto_configure:
	@echo "Unpacking tarball: $(TARBALL)"
	mkdir -p build
	tar -xzf $(TARBALL) --strip-components=1 -C build/
	dh_auto_configure

override_dh_auto_build:
	cd build && python3 setup.py build
	dh_auto_build

override_dh_auto_install:
	dh_testdir
	dh_testroot
	# Standard python install
	cd build && python3 setup.py install \
		--root=$(CURDIR)/debian/tmp \
		--install-layout=deb \
		--no-compile
	# Install rtupdate files
	@install -d "$(CURDIR)/debian/tmp/usr/share/python3/runtime.d"
	@for rtfile in "$(CURDIR)/debian/"*.rtupdate; do \
		rtbase=`basename "$${rtfile}"`; \
		echo "Copying $${rtbase}"; \
		install -m 0755 "$${rtfile}" "$(CURDIR)/debian/tmp/usr/share/python3/runtime.d/$${rtbase}"; \
	done
	# Install autolatex.sty in the TeX folder
	@echo "Copying autolatex.sty in TeX folder"; \
	install -d "$(CURDIR)/debian/tmp/usr/share/texmf/tex/latex/autolatex"; \
	install "$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/autolatex2/tex/autolatex.sty" "$(CURDIR)/debian/tmp/usr/share/texmf/tex/latex/autolatex/autolatex.sty"
	install "$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/autolatex2/tex/autolatex-beamer.sty" "$(CURDIR)/debian/tmp/usr/share/texmf/tex/latex/autolatex/autolatex-beamer.sty"
	#
	dh_auto_install


