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

export SHELL=/bin/bash
export CONFIG_SHELL=/bin/sh

#
# the configure code already checks a lot, but it does not catch
# all cases. We have now two ways to test for where to build.
# One by disabling on the other platforms, one by whitelisting
# and building only on some platforms.
LUAJIT_FAIL_ARCHS := s390x hppa arm64 ppc64 ppc64el x32 mips64 mips64el m68k
LUAJIT_GOOD_ARCHS := amd64 armel armhf hurd-i386 i386 kfreebsd-amd64 kfreebsd-i386 mips mipsel powerpc 

# In case one wants to build with old automake (<< 1.13.1), the following
# variable has to be set. By default the debian/control requires high
# enough versions of automake and friends
#export AM_V_P=false

DEB_HOST_ARCH     ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH), alpha)
  export LDFLAGS = -Wl,--no-relax
endif

#
# gcc-4.9 creates broken binaries on s390x, see #753575,
# fix is to use -O1
# see below for a tentative fix that only compiles
# pdftex0.c, but somehow the make check still fails on
# the same tests, while doing the check manually works, strange?!
ifeq ($(DEB_HOST_ARCH), s390x)
  export CFLAGS = -O1
  export CXXFLAGS = -O1
endif

# for blacklisting bad archs
#ifneq (,$(filter $(DEB_HOST_ARCH), $(LUAJIT_FAIL_ARCHS)))
#  DISABLE_JIT=--disable-luajittex
#endif

# for whitelisting good archs
ifeq (,$(filter $(DEB_HOST_ARCH), $(LUAJIT_GOOD_ARCHS)))
  # it is not in the list of good archs -> disable luajittex
  BUILDLUAJITTEX=--disable-luajittex
else
  # not necessary, but for completeness
  BUILDLUAJITTEX=--enable-luajittex
endif

#
# it seems that ARM metafont segfaults due to a problem with armhf's malloc
# see Debian bug #678604
# Building in arm mode helps
#
ifeq ($(DEB_HOST_ARCH), armhf)
  export CFLAGS = -marm
endif

# warning: if the --with autoreconf is removed then
# the patch debian/patches/debian-no-linked-scripts
# must be adapted to also patch the Makefile.in!

%:
	dh $@ --with autoreconf --builddirectory Work

override_dh_autoreconf:
	dh_autoreconf --as-needed

#
# we need libpng >= 1.4 which is not available in unstable and
# seems not to arrive anytime soon. We have to rebuild also
# the following depending libs with new libpng: gd freetype2 cairo
#	--with-system-freetype2			\
	--with-freetype2-include=/usr/include/freetype2 \
#	--with-system-libpng			\
#	--with-system-gd			\
#	--with-system-cairo 			\
#

# disable-largefile seems to break omegafonts, as they core dump
# leave it as is for now and see what else pops up
#	--disable-largefile			\

override_dh_auto_configure:
	# we have to make sure that the mendex test case that is added
	# by a patch is executable
	chmod 0755 texk/mendexk/tests/mendex.test
	# run out configure script
	dh_auto_configure -- -C --prefix=/usr 	\
	--datarootdir=/usr/share/texlive	\
	--disable-native-texlive-build		\
	--disable-missing			\
	--disable-linked-scripts		\
	--with-banner-add=/Debian		\
	--enable-shared				\
	--with-system-zlib			\
	--with-system-zzlib			\
	--with-system-xpdf			\
	--with-system-poppler			\
	--with-system-potrace			\
	--with-system-graphite2			\
	--with-system-harfbuzz 			\
	--with-system-libgs			\
	--with-system-pixman			\
	--with-system-libpaper			\
	--with-system-zziplib			\
	--with-system-icu			\
	--with-system-gmp			\
	--with-system-mpfr			\
	--with-x				\
	--with-mf-x-toolkit			\
	--with-xdvi-x-toolkit=xaw		\
	$(BUILDLUAJITTEX)			\
	--disable-lcdf-typetools		\
	--disable-biber				\
	--disable-dvipng			\
	--disable-musixflx			\
	--disable-ps2eps			\
	--disable-psutils			\
	--disable-t1utils			\
	--disable-cjkutils			\
	--disable-chktex			\
	--disable-dvidvi			\
	--disable-lacheck			\
	--disable-tex4htk			\
	--disable-pmx				\
	--disable-m-tx				\
	--disable-texdoctk			\
	--disable-ttf2pk			\
	--enable-ttf2pk2			\
	--enable-ipc				

#
# the below should actually solve the problem for s390x
# by only recompiling pdftex0.c with -O1, but somehow
# the test suite still does not succeed, while running
# the same test manually succeeds. I have no idea. 
# Giving up here.
# Sorry s390x, hope for a fix for the gcc bug ...
#override_dh_auto_build:
#	dh_auto_build
#ifeq ($(DEB_HOST_ARCH), s390x)
#	# gcc-4.9 creates broken binaries on s390x, see #753575,
#	# we have to recompile pdftex0.c with -O1 otherwise tests fail
#	cp Work/texk/web2c/Makefile Work/texk/web2c/Makefile.backup
#	cat Work/texk/web2c/Makefile.backup | sed -e 's/\$$(CFLAGS) -c -o pdftex-pdftex0.o/-g -O1 -c -o pdftex-pdftex0.c/' > Work/texk/web2c/Makefile
#	touch Work/texk/web2c/pdftex0.c
#	rm Work/texk/web2c/pdftex
#	rm -f Work/texk/web2c/.libs/pdftex
#	make -C Work/texk/web2c pdftex
#endif

override_dh_auto_install:
	dh_auto_install -- DESTDIR=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install --sourcedir=$(CURDIR)/debian/tmp
	# replace xdvi wrapper shell script with perl variant to
	# allow for opening of gz etc files
	rm debian/texlive-binaries/usr/bin/xdvi
	install -m 0755 debian/xdvi-pl debian/texlive-binaries/usr/bin/xdvi
	# remove texlive directories, they should not be shipped here
	rm -rf debian/texlive-binaries/usr/share/texlive
	# remove wrongly added info/dir.gz
	rm -f debian/texlive-binaries/usr/share/info/dir*
	# remove tlbuild info document, not needed
	rm debian/texlive-binaries/usr/share/info/tlbuild.info*
	rm debian/texlive-binaries/usr/share/man/man1/latex.1*
	rm debian/texlive-binaries/usr/share/man/man1/pdflatex.1*
	rm debian/texlive-binaries/usr/share/man/man1/lamed.1*
	rm debian/texlive-binaries/usr/share/man/man1/amstex.1*
	# fix EUCJ encoding of mendex.1
	recode EUC-JP..UTF8 debian/texlive-binaries/usr/share/man/man1/mendex.1
	# for alternatives treatment we rename usr/bin/bibtex to
	# usr/bin/bibtex.original
	mv debian/texlive-binaries/usr/bin/bibtex \
	   debian/texlive-binaries/usr/bin/bibtex.original
	mv debian/texlive-binaries/usr/share/man/man1/bibtex.1 \
	   debian/texlive-binaries/usr/share/man/man1/bibtex.original.1

override_dh_installdocs:
	dh_installdocs
	dh_installdocs -plibptexenc1 -plibptexenc-dev \
		texk/ptexenc/COPYRIGHT texk/ptexenc/README
	dh_installdocs -plibkpathsea6 -plibkpathsea-dev \
		texk/kpathsea/AUTHORS \
		texk/kpathsea/NEWS texk/kpathsea/PROJECTS \
		texk/kpathsea/README
	dh_installdocs -plibsynctex1 -plibsynctex-dev \
		texk/web2c/synctexdir/README.txt	\
		texk/web2c/synctexdir/synctex_parser_readme.txt

override_dh_installchangelogs:
	dh_installchangelogs
	dh_installchangelogs -plibkpathsea6 -plibkpathsea-dev \
		texk/kpathsea/ChangeLog
	dh_installchangelogs -plibptexenc1 -plibptexenc-dev \
		texk/ptexenc/ChangeLog
	dh_installchangelogs -plibsynctex1 -plibsynctex-dev \
		texk/web2c/synctexdir/ChangeLog

override_dh_compress:
	dh_compress -X.pdf

override_dh_makeshlibs:
	dh_makeshlibs -plibkpathsea6
	dh_makeshlibs -plibptexenc1
	dh_makeshlibs -plibsynctex1

override_dh_shlibdeps:
	dh_shlibdeps \
		-l debian/libkpathsea6/usr/lib \
		-l debian/libptexenc1/usr/lib \
		-l debian/libsynctex1/usr/lib

override_dh_clean:
	dh_clean -X.orig
