#! /usr/bin/make -f
#
# makefile rules for the Debian package auctex
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10,
# 11, 12 Davide G. M. Salvetti.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux System you can find a copy of the GNU General Public
# License in "/usr/share/common-licenses/GPL".

SHELL := /bin/bash

EPERL_SRC := $(wildcard debian/*.eperl)
EPERL_OBJ := $(EPERL_SRC:%.eperl=%)

debian/clean: $(EPERL_OBJ)
	echo $(filter-out debian/copyright debian/rules \
		debian/auctex.templates, $(EPERL_OBJ)) \
		debian/auctex.templates.dcobj \
		debian/clean > debian/clean

%:: %.eperl
	eperl -P -o $@ $<

build: debian/clean
	dh $@ --exclude .eperl --with tex

ELCC := $(shell pwd)/debian/elcc
.PHONY: override_dh_auto_build
override_dh_auto_build:
	chmod a+x debian/elcc
	$(MAKE) ELCC=$(ELCC)
	rm --force auto-loads.el
	$(MAKE) auto-loads.el
	$(MAKE) tex-site.el
	cd doc && $(MAKE) auctex.htmls/index.html
	cd doc && $(MAKE) preview-latex.htmls/index.html

%:
	dh $@ --exclude .eperl --with tex

.PHONY: override_dh_install
override_dh_install:
	find debian/tmp/usr/share/emacs/site-lisp -type f -name \*.elc \
		-print0 | xargs --null --no-run-if-empty rm -f
	dh_install

.PHONY: override_dh_auto_install
override_dh_auto_install:
	dh_auto_install
	cd preview/latex && $(MAKE) DESTDIR=../../debian/tmp install

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --exclude=.pdf

.PHONY: override_dh_installdocs
override_dh_installdocs:
	install -m 755 -d debian/tmp/usr/doc/auctex/src
	install -m 644 doc/tex-ref.pdf debian/tmp/usr/doc/auctex
	install -m 644 doc/*.tex{,i} debian/tmp/usr/doc/auctex/src
	install -m 755 -d \
		debian/tmp/usr/doc/auctex/HTML/{auctex,preview-latex}
	install -m 644 doc/auctex.htmls/* \
		debian/tmp/usr/doc/auctex/HTML/auctex
	install -m 644 doc/preview-latex.htmls/* \
		debian/tmp/usr/doc/auctex/HTML/preview-latex
	cd preview; for i in ChangeLog*; do \
		install -m 644 $$i \
		../debian/tmp/usr/doc/auctex/preview-$$i; \
	done
	dh_installdocs

.PHONY: override_dh_installtex
override_dh_installtex:
	dh_installtex --package=preview-latex-style

debian/po/templates.pot: debian/auctex.templates debian/po/*.po
	umask 0022 && debconf-updatepo --verbose --podir=debian/po
	@touch $@

%.dcobj: % debian/po/templates.pot
	po2debconf --verbose --podir debian/po $< > $@

.PHONY: debconf-test
debconf-test: debian/auctex.templates.dcobj debian/auctex.config
	debconf-loadtemplate auctex \
		debian/auctex.templates.dcobj
	DEBCONF_DEBUG=developer debconf --owner=auctex \
		--frontend=dialog --priority=low \
		$(SHELL) -e debian/auctex.config configure

.PHONY: debconf-ask-for-translations
debconf-ask-for-translations: debian/auctex.templates.dcobj
	podebconf-report-po --verbose --gzip \
		 --call --withtranslators --languageteam \
		--package=auctex --deadline='+7days' \
		--from='"Davide G. M. Salvetti (Debian auctex maintainer)" <salve@debian.org>' \
		--postpone=../+RFT.mbox

.PHONY: cvs-import
cvs-import: VERSION := 11.86+cvs.$(shell date +%Y.%m.%d.%H.%M)
cvs-import:
	cd .. && cvs -z3 \
		-d:ext:salve@cvs.savannah.gnu.org:/sources/auctex \
		export -d auctex_$(VERSION) -D $$(date +%F) auctex
	find ../auctex_$(VERSION) -type f -name .cvsignore -print0 \
		| xargs --null --no-run-if-empty rm --force
	cd ../auctex_$(VERSION) && ./autogen.sh
	tar --create --verbose --auto-compress \
		--file=../auctex_$(VERSION).orig.tar.gz \
		--directory=.. auctex_$(VERSION)
	rm --recursive --force ../auctex_$(VERSION)
	git-import-orig --verbose \
		--upstream-branch=cvs/upstream \
		--debian-branch=cvs/master \
		../auctex_$(VERSION).orig.tar.gz
	rm --force ../auctex_$(VERSION).orig.tar.gz

.PHONY: snapshot
snapshot: BRANCH := $(shell git branch | perl -ne 'print if s/^\* //')
snapshot:
	git-dch --debian-branch=$(BRANCH) --snapshot --auto --commit
	git-buildpackage -tc -us -uc --git-pbuilder --git-tag \
		--git-debian-branch=$(BRANCH)

.PHONY: release
release:
	git checkout master
	gbp-pq rebase
	gbp-pq export
	git-dch --release --commit
	git-buildpackage -tc --git-pbuilder --git-tag
