#!/usr/bin/make -f
# 
# debian/rules file for texlive-base

PACKAGES=texlive-base texlive-latex-base texlive-latex-recommended texlive-fonts-recommended texlive-pictures texlive-luatex texlive-metapost texlive-xetex texlive-fonts-recommended-doc texlive-latex-base-doc texlive-latex-recommended-doc texlive-metapost-doc texlive-pictures-doc
METAPACKAGES=texlive texlive-full

SHELL=/bin/bash

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp:

	touch build-stamp

clean: 
	for i in $(PACKAGES) $(METAPACKAGES) ; do \
	  rm -f debian/$$i.{README.Debian,links,links.generated} ; \
	  rm -f debian/$$i.{postinst,postrm,preinst,prerm} ; \
	  rm -f debian/$$i.{maps,hyphens,formats,info} ; \
	  rm -f debian/$$i.{lintian-overrides} ; \
	done

	rm -f debian/texlive-base.cnf


	# texlive contains some .orig files we need to keep otherwise
	# the copy will fail due to the files occurring in the tlpdb
	dh_clean -X.orig -X.bak
	rm -f configure-stamp
	rm -f build-stamp
	rm -f install-stamp

install: install-stamp

install-stamp: build-stamp
	# make sure we have texmf-dist/web2c otherwise the media detection
	# might break (like in texlive-lang)
	mkdir -p texmf-dist/web2c
	perl debian/tpm2deb-bin.pl --nosource $(PACKAGES) $(METAPACKAGES)
	for i in $(PACKAGES) $(METAPACKAGES) ; do \
	  bash debian/generate-license-file $$i ; \
	  bash debian/fix-privacy-breach $$i ; \
	  bash debian/useless-fix-perl-shebang-for-policy $$i ; \
	  find debian/$i/usr/share -name \*.jar -exec chmod ugo-x '{}' \; ; \
	  if [ -r debian/$$i.links.dist ] ; then cat debian/$$i.links.dist >> debian/$$i.links ; fi ; \
	done

	mkdir -p debian/texlive-base/etc/texmf/texdoctk
	mv debian/texlive-base/usr/share/texlive/texmf-dist/texdoctk/texdocrc.defaults \
	  debian/texlive-base/etc/texmf/texdoctk
	rmdir --ignore-fail-on-non-empty debian/texlive-base/usr/share/texlive/texmf-dist/texdoctk
	mkdir -p debian/texlive-base/usr/share/applications
	install debian/texdoctk.desktop debian/texlive-base/usr/share/applications
	# install texdoc data cache
	mkdir -p debian/texlive-base/usr/share/texlive/texmf-dist/scripts/texdoc/
	install debian/Data.tlpdb.lua debian/texlive-base/usr/share/texlive/texmf-dist/scripts/texdoc/Data.tlpdb.lua



	for i in $(PACKAGES) $(METAPACKAGES) ; do \
	  bash debian/create-font-links $$i >> debian/$$i.links ; \
	done
	touch install-stamp

binary-arch:

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install
	dh_installdirs
	#dh_installmenu
	dh_lintian
	dh_bugfiles -A
	dh_installdocs -A debian/README.source debian/README.Debian

	dh_installdocs -p texlive-base README readme-txt.dir readme-html.dir debian/CHANGES.packaging debian/README.tlmgr-on-Debian.md
	# nasty trick
	# mptopdf needs a dump, but is a link to a script
	# we have to trick dh_installtex to accept it
	mv debian/texlive-latex-base/usr/bin/mptopdf	\
		debian/texlive-latex-base/usr/bin/mptopdf.bck
	ln -s pdftex debian/texlive-latex-base/usr/bin/mptopdf
	dh_installtex -Ntexlive-base -A 		\
		$(patsubst %,-N%,$(METAPACKAGES))	\
		--flavor=tree:texlive
	# nasty trick undoing
	rm debian/texlive-latex-base/usr/bin/mptopdf
	mv debian/texlive-latex-base/usr/bin/mptopdf.bck	\
		debian/texlive-latex-base/usr/bin/mptopdf
	# end of nasty trick
	dh_installtex -ptexlive-base --flavor=tree:texlive
	# /usr/bin/mf has been created by dh_installtex as symlink to 
	# mf-nowin, but is also shipped by texlive-binaries, remove it.
	rm debian/texlive-base/usr/bin/mf
	# furthermore we want to have etex and pdfetex as links in 
	# texlive-binaries and not in texlive-base, remove them here
	# and add them via dh_link in texlive-binaries
	# this is just out of history, would be nicer to have changed
	rm debian/texlive-base/usr/bin/etex 
	rm debian/texlive-base/usr/bin/pdfetex
	# for the libpaper hook, install templates and
	# translations. No need to modify postrm. I hope it accepts
	# the missing config script
	dh_installdebconf -n
	cp debian/texlive-base.libpaper debian/texlive-base/etc/libpaper.d/texlive-base
	chmod a+x debian/texlive-base/etc/libpaper.d/texlive-base

	dh_installemacsen
	dh_installchangelogs
	dh_installman
	bash debian/fix-manpages.sh
	# info pages
	bash debian/convert-info-files-to-unix.sh
	dh_installinfo

	# remove info files that are present in texlive-binaries
	# has to be done after the dh_installinfo call
	rm debian/texlive-base/usr/share/info/dvips.info*
	rm debian/texlive-base/usr/share/info/web2c.info*
	rm debian/texlive-base/usr/share/info/kpathsea.info*
	rmdir --ignore-fail-on-non-empty debian/texlive-base/usr/share/info

	dh_installmime
	dh_lintian
	dh_link
	dh_strip_nondeterminism
	dh_compress -X.pdf
	dh_fixperms
	# remove .bat files, nobody needs them
	find debian/texlive-*/usr/share/texlive/texmf-dist/ -name \*.bat -a -type f -exec rm '{}' \;
	# remove x bit from files in the doc hierarchy
	find debian/texlive-*/usr/share/doc/texlive-doc -type f -exec chmod a-x '{}' \;
	# remove x bit from jar files
	find debian/texlive-*/usr/share/texlive -name \*.jar -exec chmod a-x '{}' \;
	# we also have to fix the permissions of the link targets from /usr/bin
	bash debian/fix-bin-symlink-targets-permissions.sh
	dh_installdeb
	dh_gencontrol
	dh_md5sums

	dh_builddeb -- -Zxz


binary: binary-indep binary-arch

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

# eof