# Maintainer (arch): Antonio Rojas <arojas@archlinux.org>
# Contributor: Rémy Oudompheng <remy@archlinux.org>
# Contributor: francois <francois.archlinux.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Michał Masłowski <mtjm@mtjm.eu>
# Contributor: Isaac David <isacdaavid(at)isacdaavid!info>
# Contributor: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
# Contrbutor: bill-auger <bill-auger@programmer.net>
# Maintainer (archarm): Kevin Mihelich <kevin@archlinuxarm.org>


# parabola changes and rationale:
# technical:
#  - re-brand for parabola (due to changes in texlive-{core,fontsextra,latexextra})
#  - pin to strongly-coupled dependency versions
#  - disable texindy/xindy on armv7h
#  - build from upstream sourve-ball, rather than VCS
# archarm changes:
#  -fPIC for v6 and v7
#  --disable-xindy, remove xindy stuff, segfaults with clisp
#  --disable-cxx-runtime-hack
#  drop clisp and ffcall makedepends


pkgname=(texlive-bin libsynctex)
_rev=66984
pkgver=2023.$_rev
pkgrel=17
pkgrel+=.parabola1
license=(GPL)
arch=(x86_64)
arch+=(armv7h i686)
makedepends=(git cairo pixman graphite t1lib gd libsigsegv zziplib libpng libjpeg freetype2
             icu harfbuzz-icu mpfr potrace libpaper perl clisp ffcall libxcrypt subversion
             glibc zlib bash libxaw fontconfig icu readline libxmu libxpm libunistring libxt
             libx11 ncurses gcc-libs harfbuzz)
makedepends=( ${makedepends[*]/git/} )
[[ "${CARCH}" == 'armv7h' ]] && makedepends=( $(sed -E 's/clisp|ffcall//g' <<<${makedepends[@]}) )
url='https://tug.org/texlive/'
# we use source-ball
source=(texlive-bin-${_rev}.tar.gz::https://github.com/TeX-Live/texlive-source/archive/refs/tags/build-svn${_rev}.tar.gz
        ptex-debug-print.patch
        context-luatex-1.17.patch)
sha256sums=('SKIP'
            'aa838f09003c62c2efb5770a8de66f99b409df049fbd65098d80fd1957d06c50'
            'a56838d19c3bd820781693b5a2e058e1a22378b37ea199bac426d97fcc420920')

prepare() {
  ln -s texlive-source-build-svn${_rev} texlive-source # minimize diff
  cd texlive-source

  # bibtex-x needs kpathsea flags
  sed -i '/AC_SEARCH_LIBS/a KPSE_KPATHSEA_FLAGS' texk/bibtex-x/configure.ac
  (cd texk/bibtex-x && autoreconf)
  # t4ht expects to be un /usr/share/texmf/bin/t4ht (FS#27251)
  sed -i s/SELFAUTOPARENT/TEXMFROOT/ texk/tex4htk/t4ht.c
  # remove spurious ptex "guessed encoding" print
  patch -p1 -i ../ptex-debug-print.patch
  # update context to work with luatex 1.17
  patch -p1 -i ../context-luatex-1.17.patch
}

build() {
  cd texlive-source

  mkdir -p build
  cd build
  ax_cv_c_float_words_bigendian=no \
  ../configure --prefix=/usr -C \
    --sysconfdir=/etc \
    --datarootdir=/usr/share \
    --datadir=/usr/share \
    --mandir=/usr/share/man \
    $([[ "${CARCH}" != armv7h ]] || printf -- '--disable-cxx-runtime-hack') \
    --disable-native-texlive-build \
    --with-banner-add="/Parabola GNU/Linux-libre" \
    --disable-multiplatform \
    --disable-dialog \
    --disable-psutils \
    --disable-t1utils \
    --disable-bibtexu \
    --disable-dvisvgm \
    --disable-xz \
    --enable-shared \
    --disable-static \
    --with-system-zlib \
    --with-system-zziplib \
    --with-system-pnglib \
    --with-system-ncurses \
    --with-system-t1lib \
    --with-system-gd \
    --with-system-freetype2 \
    --with-system-pixman \
    --with-system-cairo \
    --with-system-harfbuzz \
    --with-system-graphite \
    --with-system-icu \
    --with-system-gmp \
    --with-system-mpfr \
    --with-system-potrace \
    --with-system-libpaper \
    --with-freetype2-libdir=/usr/lib \
    --with-freetype2-include=/usr/include/freetype2 \
    --with-xdvi-x-toolkit=xaw \
    --disable-dump-share \
    --with-clisp-runtime=default \
    $([[ "${CARCH}" == armv7h ]] && echo -n --disable-xindy || echo -n --enable-xindy) \
    --disable-xindy-rules \
    --disable-xindy-docs
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package_libsynctex() {
  pkgdesc='Library for synchronization between TeX files and resulting file'
  depends=(glibc zlib)

  cd texlive-source/build
  make -C texk/web2c DESTDIR="$pkgdir" \
        install-data-am install-libLTLIBRARIES

  # remove docs included in texlive-doc
  rm -r "$pkgdir"/usr/share/man
}

package_texlive-bin() {
  pkgdesc='TeX Live binaries'
  depends=(cairo pixman graphite t1lib gd poppler ffcall libsigsegv zziplib libpng libjpeg freetype2 libxcrypt
           harfbuzz-icu mpfr potrace libpaper libsynctex glibc zlib bash libxaw fontconfig icu readline libxmu
           libxpm libunistring libxt libx11 ncurses gcc-libs harfbuzz)
  provides=(lcdf-typetools kpathsea xindy)
  optdepends=('psutils: to manipulate the output of dvips')

  case "$CARCH" in
  armv7h)
    depends=(  ${depends[*]/ffcall/} )
    provides=( ${provides[*]/xindy/} )
  esac

  # pin to strongly-coupled dependency versions
  depends+=('libicui18n.so' 'libicuuc.so')

  cd texlive-source
  # fixes for xindy
  find utils/xindy -name Makefile -exec sed -i -e "s|^prefix =.\+$|prefix = $pkgdir/usr|" -e "s|^mandir =.\+$|mandir = \${prefix}/share/man|" -e "s|^datadir =.\+$|datadir = \${datarootdir}/texmf|" -e "s|^docdir =.\+$|docdir = \${datadir}/doc/xindy|" '{}' \;

  cd build
  make DESTDIR="$pkgdir" texmf="$pkgdir"/usr/share/texmf install
  LD_PRELOAD="$pkgdir"/usr/lib/libkpathsea.so.6 \
  make DESTDIR="$pkgdir" texlinks

  # install old ConTeXt stubs
  cd "$srcdir"/texlive-source
  for _stub in `ls texk/texlive/linked_scripts/context/stubs/unix`; do
    install -m755 texk/texlive/linked_scripts/context/stubs/unix/$_stub "$pkgdir"/usr/bin/$_stub
  done

  # remove stuff included in texlive-texmf
  rm -r "$pkgdir"/usr/share/texmf-dist

  # remove docs included in texlive-doc
  rm -r "$pkgdir"/usr/share/{info,man}

  # remove links to scripts
  for _link in $(ls "$pkgdir"/usr/bin); do
    [[ $(readlink -m "$pkgdir"/usr/bin/$_link) == */scripts/* ]] && _rmlinks+="$pkgdir/usr/bin/$_link "
  done
  rm $_rmlinks

  # remove libsynctex
  rm "$pkgdir"/usr/include/synctex/*
  rm "$pkgdir"/usr/lib/libsynctex.*
  rm "$pkgdir"/usr/lib/pkgconfig/synctex.pc
}
