# $Id: PKGBUILD 163857 2012-07-21 11:53:06Z remy $
# Maintainer: Rémy Oudompheng <remy@archlinux.org>
# Contributor: francois <francois.archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - Removed clisp makedepend, removed xindy from build and packaging (only thing that used clisp)
#  - Use -fPIC with armv7
#  - --disable-cxx-runtime-hack in configure

pkgname=texlive-bin
pkgver=2012.0
pkgrel=11
pkgdesc="TeX Live binaries"
license=('GPL')
arch=('i686' 'x86_64')
depends=('t1lib' 'gd' 'poppler>=0.22.0' 'libsigsegv' 'zziplib' 'libpng' 'libjpeg' 'freetype2')
makedepends=('perl' 'ffcall')
optdepends=('ed: for texconfig')
provides=('lcdf-typetools' 'kpathsea' 'pdfjam')
conflicts=('pdfjam')
options=('!libtool' '!strip')
url='http://tug.org/texlive/'
install="texlive.install"
source=('texmf.cnf'
        'fix-fontforge-encoding.patch'
        '09-texlive-fonts.conf'
        'poppler-0.20.patch'
        'http://mirrors.kernel.org/archlinux/other/texlive/texlive-bin-source-20120623.tar.xz'
        'http://mirrors.kernel.org/archlinux/other/texlive/texlive-bin-texmf-20120623.tar.xz'
        'luatex-r4449-radical-rule-thickness.patch'
)
backup=(etc/texmf/web2c/texmf.cnf \
    etc/texmf/chktex/chktexrc \
	etc/texmf/dvipdfm/config/config \
	etc/texmf/dvipdfmx/dvipdfmx.cfg \
	etc/texmf/dvips/config/config.ps \
	etc/texmf/tex/generic/config/language.dat \
	etc/texmf/tex/generic/config/language.def \
	etc/texmf/tex/generic/config/pdftexconfig.tex \
	etc/texmf/ttf2pk/ttf2pk.cfg \
	etc/texmf/web2c/fmtutil.cnf \
	etc/texmf/web2c/mktex.cnf \
	etc/texmf/xdvi/XDvi)

md5sums=('220a4f4cc0d915bf8fcbcb553dcee1ae'
         'bfb9716aa00c86c08cd31e5b32edeb98'
         '393a4bf67adc7ca5df2b386759d1a637'
         'e3a664d5a7f0923f35060fe9d25d2573'
         'ac2c3c3aa765581f8504e21a5edbfb9f'
         'f315a860158cfc952c82a6d759961024'
         '5190f3295eb9a604c5f17f5bd607299b')

build() {
   cd "$srcdir"

   if [ "${CARCH}" = "armv6h" -o "${CARCH}" = "armv7h" ]; then
     export CFLAGS="${CFLAGS} -fPIC"
     export CXXFLAGS="${CXXFLAGS} -fPIC"
   fi

   # this patch removes spurious error message with locale "xx_YY.utf8"
   patch -Np0 -i fix-fontforge-encoding.patch
   # fix build with poppler >= 0.20
   patch -Np0 -i poppler-0.20.patch
   # fix radical rule thickness regression in Luatex
   # http://tex.stackexchange.com/q/61952/729
   patch -Np0 -i luatex-r4449-radical-rule-thickness.patch
   # t4ht expects to be un /usr/share/texmf/bin/t4ht (FS#27251)
   sed -i s/SELFAUTOPARENT/TEXMFROOT/ source/texk/tex4htk/t4ht.c
   #############################################################
   ### configure
   cd source
   ## prevent compiling Xdvi with libXp
   sed -i~ 's|-lXp ||' texk/xdvik/configure
   test ! -d Work && mkdir Work
   cd Work
   echo "--> Initial configuration..."
   # we use temporary prefix to avoid messing the existing
   # $pkgdir/usr/share/texmf tree
   # system zlib is disabled due to issues with zlib 1.2.6 (FS#28221)
   ../configure --prefix=/usr -C \
     --sysconfdir=/etc \
     --datarootdir=/usr/share \
     --datadir=/usr/share \
     --mandir=/usr/share/man \
     --disable-native-texlive-build \
     --with-banner-add="/Arch Linux" \
     --disable-cxx-runtime-hack \
     --disable-multiplatform \
     --disable-dialog \
     --disable-psutils \
     --disable-t1utils \
     --disable-bibtexu \
     --disable-xz \
     --with-system-zlib \
     --with-system-zziplib \
     --with-system-pnglib \
     --with-system-ncurses \
     --with-system-t1lib \
     --with-system-gd \
     --with-system-poppler \
     --with-system-xpdf \
     --with-system-freetype2 \
     --without-system-graphite \
     --with-freetype2-libdir=/usr/lib \
     --with-freetype2-include=/usr/include/freetype2 \
     --with-xdvi-x-toolkit=xaw \
     --disable-dump-share \
     --disable-aleph \
     --enable-luatex
   #############################################################
   ### make
   echo "-------------------------------------------------------"
   echo "--> Building the whole beast ..."
   echo "-------------------------------------------------------"
   make
}

package() {
   cd "$srcdir"
   #############################################################
   ### install texmf tree
   echo "-------------------------------------------------------"
   echo "--> installing the texmf tree"
   echo "-------------------------------------------------------"
   install -m755 -d "$pkgdir"/usr/share
   cp -r texmf "$pkgdir"/usr/share/
   install -d -m755 "$pkgdir"/etc/texmf/web2c
   install -d -m755 "$pkgdir"/etc/texmf/chktex
   install -d -m755 "$pkgdir"/etc/texmf/dvips/config
   install -d -m755 "$pkgdir"/etc/texmf/dvipdfm/config
   install -d -m755 "$pkgdir"/etc/texmf/dvipdfmx
   install -d -m755 "$pkgdir"/etc/texmf/tex/generic/config
   install -d -m755 "$pkgdir"/etc/texmf/ttf2pk
   install -d -m755 "$pkgdir"/etc/texmf/xdvi
   install -d -m755 "$pkgdir"/etc/fonts/conf.avail
   install -m644 "$srcdir"/09-texlive-fonts.conf "$pkgdir"/etc/fonts/conf.avail/
   # move man files to /usr/share/man
   for i in 1 5; do
     install -d -m755 "$pkgdir"/usr/share/man/man$i
     mv "$pkgdir"/usr/share/texmf/doc/man/man$i/*.$i "$pkgdir"/usr/share/man/man$i/
   done
   # move info files to /usr/share/info
   install -d -m755 "$pkgdir"/usr/share/info
   mv "$pkgdir"/usr/share/texmf/doc/info/*.info "$pkgdir"/usr/share/info/
   rm -rf "$pkgdir"/usr/share/texmf/doc/{man,info}
   # copy config files to $TEXMFCONFIG tree
   cp -a "$pkgdir"/usr/share/texmf/chktex/chktexrc \
     	"$pkgdir"/etc/texmf/chktex/
   cp -a "$pkgdir"/usr/share/texmf/web2c/mktex.cnf \
   	  "$pkgdir"/etc/texmf/web2c/
   cp -a "$pkgdir"/usr/share/texmf/web2c/updmap.cfg \
   	  "$pkgdir"/etc/texmf/web2c/
   cp -a "$pkgdir"/usr/share/texmf/web2c/fmtutil.cnf \
   	  "$pkgdir"/etc/texmf/web2c/
   cp -a "$pkgdir"/usr/share/texmf/dvips/config/config.ps \
   	  "$pkgdir"/etc/texmf/dvips/config/
   cp -a "$pkgdir"/usr/share/texmf/dvipdfm/config/config \
   	  "$pkgdir"/etc/texmf/dvipdfm/config/
   cp -a "$pkgdir"/usr/share/texmf/dvipdfmx/dvipdfmx.cfg \
   	  "$pkgdir"/etc/texmf/dvipdfmx/
   cp -a "$pkgdir"/usr/share/texmf/tex/generic/config/pdftexconfig.tex \
   	  "$pkgdir"/etc/texmf/tex/generic/config/
   cp -a "$pkgdir"/usr/share/texmf/tex/generic/config/language.dat \
   	  "$pkgdir"/etc/texmf/tex/generic/config/
   cp -a "$pkgdir"/usr/share/texmf/tex/generic/config/language.def \
   	  "$pkgdir"/etc/texmf/tex/generic/config/
   cp -a "$pkgdir"/usr/share/texmf/ttf2pk/ttf2pk.cfg \
   	  "$pkgdir"/etc/texmf/ttf2pk/
   cp -a "$pkgdir"/usr/share/texmf/xdvi/XDvi \
   	  "$pkgdir"/etc/texmf/xdvi/
   # remove TL specific warnings in the language.{dat,def} files:
   sed -i -e '/DO NOT EDIT/,+3 d' "$pkgdir"/etc/texmf/tex/generic/config/language.*
   # clean updmap.cfg
   sed -i '/^\(Map\|MixedMap\)/d' "$pkgdir"/etc/texmf/web2c/updmap.cfg
   sed -i '/^#! \(Map\|MixedMap\)/d' "$pkgdir"/etc/texmf/web2c/updmap.cfg

   #############################################################
   ### install
   cd source

   echo "-------------------------------------------------------"
   echo "--> Proceeding with make install ..."
   echo "-------------------------------------------------------"
   cd Work
   make DESTDIR="${pkgdir}" texmf="$pkgdir"/usr/share/texmf install
   rm -rf "${pkgdir}"/usr/{texmf,share/texmf-dist}

   # replace upstream texmf.cnf with ours
   rm -f "$pkgdir"/usr/share/texmf/web2c/texmf.cnf
   install -m644 "$srcdir"/texmf.cnf "$pkgdir"/etc/texmf/web2c/texmf.cnf
   # since the location of texmf.cnf is hard-wired to be under /usr/share/texmf/web2c
   # we make a symlink from /etc/texmf/web2c/texmf.cnf to the latter
   ln -sf /etc/texmf/web2c/texmf.cnf "$pkgdir"/usr/share/texmf/web2c/texmf.cnf
   # fix location of TEXMFCACHE for luatools
   sed -i 's#texlive2010#texlive#' "$pkgdir"/usr/share/texmf/web2c/texmfcnf.lua
   ## remove aleph from fmtutil.cnf
   sed -i -e '/aleph/d' "$pkgdir"/usr/share/texmf/web2c/fmtutil.cnf

   ## install Perl libraries
   mkdir -p "$pkgdir"/usr/share/tlpkg/TeXLive
   install -m644 "${srcdir}"/source/utils/biber/TeXLive/*.pm "$pkgdir"/usr/share/tlpkg/TeXLive

   # create symlinks for formats 
   echo "--> Create symlinks for TeX formats ..."
   PATH="$PATH:${pkgdir}/usr/bin" texlinks -f "$pkgdir"/usr/share/texmf/web2c/fmtutil.cnf "$pkgdir"/usr/bin/
   #############################################################
   # remove dangling symlinks
   _bibtexextra_scripts="
bibexport
listbib
urlbst
"
   _core_scripts="
afm2afm
arlatex
autoinst
bundledoc
checkcites
cmap2enc
ctanify
ctanupload
de-macro
dosepsbin
dviasm
epstopdf
findhyph
font2afm
fragmaster
installfont-tl
latex2man
latexdiff
latexdiff-vc
latexfileversion
latexmk
latexrevise
listings-ext.sh
match_parens
mf2pt1
mkjobtexmf
mkluatexfontdb
mkt1font
mptopdf
ot2kpx
pdf180
pdf270
pdf90
pdfatfi
pdfbook
pdfcrop
pdfflip
pdfjam
pdfjam-pocketmod
pdfjam-slides3up
pdfjam-slides6up
pdfjoin
pdfnup
pdfpun
pfm2kpx
pkfix
pkfix-helper
purifyeps
repstopdf
rpdfcrop
showglyphs
sty2dtx
texcount
texdef
texdiff
texdirflatten
texliveonfly
texloganalyser
typeoutfileinfo
thumbpdf
vpl2ovp
vpl2vpl
"
   _htmlxml_scripts="ht htcontext htlatex htmex httex httexi htxelatex htxetex mk4ht"
   _langcjk_scripts="convbkmk updmap-setup-kanji"
   _langextra_scripts="ebong"
   _langgreek_scripts="mkgrkindex"
   _latexextra_scripts="
authorindex
exceltex
makeglossaries
pdfannotextractor
perltex
ppower4
ps4pdf
splitindex
svn-multi
vpe"
   _music_scripts="m-tx musixtex musixflx pmx2pdf"
   _pictures_scripts="cachepic epspdf epspdftk fig4latex mathspic"
   _pstricks_scripts="pedigree pst2pdf"
   _science_scripts="ulqda"
   for s in \
        ${_bibtexextra_scripts} \
        ${_core_scripts}       \
        ${_htmlxml_scripts}    \
        ${_langcjk_scripts}    \
        ${_langextra_scripts}  \
        ${_langgreek_scripts}  \
        ${_latexextra_scripts} \
        ${_music_scripts}      \
        ${_pictures_scripts}   \
        ${_pstricks_scripts}   \
        ${_science_scripts}    \
        tlmgr; do
     rm -f "$pkgdir"/usr/bin/$s
   done
   ###################################################################
}