#!/usr/bin/make -f

#export DH_VERBOSE=1
export DH_OPTIONS

# needed for quilt:
export QUILT_PATCHES=./debian/patches

SAVEDGMO=po/nl.gmo po/ro.gmo po/tr.gmo po/de.gmo

CFLAGS := -g -Wall
ifneq "$(findstring noopt,$(DEB_BUILD_OPTIONS))" ""
CFLAGS += -O0
else
CFLAGS += -O2
endif

texinfo := $(CURDIR)/debian/texinfo
tmpdir := $(CURDIR)/debian/tmp

build: build-stamp
build-stamp: patch-stamp
	# texi2dvi has been touched by the patches, so the builder
	# will try to rebuild the man page from texi2dvi but that
	# spits out only a warning that no tex system is installed
	touch doc/texi2dvi.1
	dh_testdir
	CFLAGS="$(CFLAGS)" ./configure --prefix=/usr \
          --infodir='$${prefix}/share/info' --mandir='$${prefix}/share/man'
	touch po/texinfo.pot
	$(MAKE)
	touch $@

patch-stamp:
	quilt push -a
	touch $@

unpatch:
	# unapply patches, not giving an error when quilt says "No patch removed"
	quilt pop -a || [ $$? = 2 ]
	rm -f patch-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	# remove binary gmo files
	rm -f po/*.gmo
	dh_clean install-stamp build-stamp intl/libintl.h config.log

install: install-stamp

install-stamp: DH_OPTIONS=
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install install-tex prefix=$(tmpdir)/usr \
                                    TEXMF=$(tmpdir)/usr/share/texmf
	find $(tmpdir) -type f -name dir | xargs rm -f
	# pdfcolor and epsf.tex are in tetex and texlive
	rm -rf $(tmpdir)/usr/share/texmf/tex/plain
	rm -rf $(tmpdir)/usr/share/texmf/tex/generic
	rm -rf $(tmpdir)/usr/share/texmf/pdftex/plain/misc/pdfcolor.tex
	#mkdir -p $(tmpdir)/usr/share/texmf/tex/texinfo/base
	#mv $(tmpdir)/usr/share/texmf/tex/texinfo/*.tex $(tmpdir)/usr/share/texmf/tex/texinfo/base
	dh_install --list-missing --sourcedir=debian/tmp -X share/info
	#
	# install updated texinfo.tex
	#cp debian/texinfo.tex $(texinfo)/usr/share/texmf/tex/texinfo/texinfo.tex
	cp debian/texi2pdf.man $(texinfo)/usr/share/man/man1/texi2pdf.1
	dh_link
	mv $(texinfo)/usr/bin/install-info $(texinfo)/usr/bin/ginstall-info
	sed -e "s/install-info/g&/g" \
		< $(texinfo)/usr/share/man/man1/install-info.1 \
		> $(texinfo)/usr/share/man/man1/ginstall-info.1
	rm -f $(texinfo)/usr/share/man/man1/install-info.1
	cp debian/conf/50cyrtexinfo.cnf $(texinfo)/etc/texmf/fmt.d
	echo "50cyrtexinfo" > $(texinfo)/var/lib/tex-common/fmtutil-cnf/texinfo.list
	touch install-stamp

binary-indep:
# There aren't any architecture independent packages here.

binary-arch: DH_OPTIONS=-a
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdocs -A AUTHORS INTRODUCTION README NEWS TODO
	dh_installdocs -p texinfo debian/README.cyrtexinfo
	dh_installmenu
	dh_installchangelogs ChangeLog
ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	dh_strip
endif
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

# other targets
update-texinfo.tex:
	wget -O $(CURDIR)/debian/texinfo.tex ftp://tug.org/tex/texinfo.tex

.PHONY: build clean binary-indep binary-arch binary install
