# Description: smallish TeX distribution, including luatex support
# URL: https://tug.org/texlive/
# Maintainer: John McQuah, jmcquah at disroot dot org
# Depends on: ghostscript graphite2 icu harfbuzz libgd libmpfr paper potrace xorg-libxi zziplib
# Nice to have: clisp xorg-libxaw

name=texlive-base
version=20230313
release=1
source=(ftp://tug.org/historic/systems/texlive/${version:0:4}/${name:0:7}-$version-source.tar.xz
  https://download.savannah.gnu.org/releases/texi2html/texi2html-5.0.tar.bz2 tabu.sty.diff
  https://sources.archlinux.org/other/texlive/texlive-core-${version:0:4}.66587-src.zip
  https://sources.archlinux.org/other/texlive/texlive-latexextra-${version:0:4}.66551-src.zip
  https://sources.archlinux.org/other/texlive/texlive-pictures-${version:0:4}.66549-src.zip)

build() {
  # unpack the selected collections
  mkdir -p $SRC/ctan-packages
  mv $SRC/*.xz $SRC/ctan-packages

  cd $SRC/ctan-packages
  for xzfile in *.xz; do
	bsdtar -p -o -xf $xzfile
  done
  # install what was archived from a subdirectory
  mkdir -p $PKG/usr/share/texmf-dist
  subdirs=$(for d in *; do [[ -d $d && $d != texmf* ]] && echo $d; done) || true
  for dir in $subdirs; do
	find $dir -type d -exec install -d -m755 $PKG/usr/share/texmf-dist/'{}' \;
	find $dir -type f -exec install -m644 '{}' $PKG/usr/share/texmf-dist/'{}' \;
  done
  # install what was archived relative to TEXMFROOT
  find texmf-dist -type d -exec install -d -m755 $PKG/usr/share/'{}' \;
  find texmf-dist -type f -exec install -m644 '{}' $PKG/usr/share/'{}' \;
  find texmf-dist -type f -executable -exec chmod 755 $PKG/usr/share/'{}' \;
  cd -
  rm -rf $SRC/ctan-packages

  # remove broken symlinks
  find $SRC -type l -xtype l -exec rm '{}' \+
  find $PKG -type l -xtype l -exec rm '{}' \+

  # make ConTeXt work, and remove unused settings
  sed -i \
    -e 's|selfautoparent:|/usr/share/|g' \
    -e 's|\(TEXMFLOCAL[ ]*=[ ]*\)[^,]*|\1"/usr/share/texmf-local"|' \
    -e '/selfautodir/d' \
    -e '/texmflocal/d' \
    $PKG/usr/share/texmf-dist/web2c/texmfcnf.lua

  cd ${name:0:7}-$version-source

  chmod -R a-s $SRC

  find $SRC \
    \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
    -exec chmod 755 {} \; -o \
    \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
    -exec chmod 644 {} \;

  # Fix some paths
  sed -i 's/SELFAUTOPARENT/TEXMFROOT/' texk/tex4htk/t4ht.c
  sed -i 's|TEXMFROOT = .*|TEXMFROOT = $SELFAUTODIR/share|g' texk/kpathsea/texmf.cnf
  sed -i 's|TEXMFLOCAL = .*|TEXMFLOCAL = $TEXMFROOT/texmf-local|g' texk/kpathsea/texmf.cnf

  export TEXMFCNF=""
  export KPATHSEA_WARNING=0

  # prevent compiling Xdvi with libXp
  sed -i 's|-lXp ||' texk/xdvik/configure

  # disable xindy if clisp is not available
  prt-get isinst clisp && PKGMK_XINDY="--enable-xindy --with-clisp-runtime=system" || \
    PKGMK_XINDY="--disable-xindy"


  # disable Xdvi if xorg-libxaw is not available
  prt-get isinst xorg-libxaw && PKGMK_XDVI="--with-x --with-xdvi-x-toolkit=xaw" || \
    PKGMK_XDVI="--without-x"

  mkdir build
  cd build
  ../configure --with-banner-add="$(crux)" --prefix=/usr \
    --enable-{dvipdfm-x,gc=system,libtool-hack,luatex,pdftex,pdftex-synctex,shared,makeindexk,tex,texlive,tex-synctex,web2c} \
    --with-system-{cairo,freetype2,gd,graphite2,gmp,harfbuzz,icu,libpaper,libpng,mpfr,ncurses,pixman,potrace,t1lib,zlib,zziplib} \
    $PKGMK_XINDY $PKGMK_XDVI \
    --disable-{all-pkgs,aleph,native-texlive-build,xetex,xindy-docs,xindy-rules}

  make
  make DESTDIR=$PKG install

  cd ..

  #fmtutil depends on these
  perl_sitelib=$(perl -V:sitelib | sed -e "s|sitelib=[']\(.*\)['].*$|\1|")
  install -d $PKG/$perl_sitelib/TeXLive
  install texk/tests/TeXLive/TL{Config,Utils}.pm $PKG/$perl_sitelib/TeXLive

  # configure fmtutil
  sed \
    -e 's/^aleph/#! aleph/g' \
    -e 's/^lamed/#! lamed/g' \
    -e 's/^amstex/#! amstex/g' \
    -e 's/^cont-en xetex/#! cont-en xetex/g' \
    -e 's/^pdfcs/#! pdfcs/g' \
    -e 's/^luacs/#! luacs/g' \
    -e 's/^pdfcs/#! pdfcs/g' \
    -e 's/^csplain/#! csplain/g' \
    -e 's/^eplain/#! eplain/g' \
    -e 's/^pdfjadetex/#! pdfjadetex/g' \
    -e 's/^jadetex/#! jadetex/g' \
    -e 's/^pdfmex/#! pdfmex/g' \
    -e 's/^utf8mex/#! utf8mex/g' \
    -e 's/^mex/#! mex/g' \
    -e 's/^mllatex/#! mllatex/g' \
    -e 's/^mltex/#! mltex/g' \
    -e 's/^mptopdf/#! mptopdf/g' \
    -e 's/^platex/#! platex/g' \
    -e 's/^eptex/#! eptex/g' \
    -e 's/^ptex/#! ptex/g' \
    -e 's/^texsis/#! texsis/g' \
    -e 's/^euptex/#! euptex/g' \
    -e 's/^uplatex/#! uplatex/g' \
    -e 's/^uptex/#! uptex/g' \
    -e 's/^xelatex/#! xelatex/g' \
    -e 's/^xetex/#! xetex/g' \
    -e 's/^cslatex/#! cslatex/g' \
    -e 's/^lollipop/#! lollipop/g' \
    -i $PKG/usr/share/texmf-dist/web2c/fmtutil.cnf
#    -e 's/^pdftex/#! pdftex/g' \
#    -e 's/^pdflatex/#! pdflatex/g' \
#    -e 's/^luajittex/#! luajittex/g' \
#    -e 's/^mf/#! mf/g' \

  # delete the hard-coded list of maps; the correct list
  # will be generated in post-install
  sed -e '/^Map/d; /^MixedMap/d; /^KanjiMap/d' \
    -i $PKG/usr/share/texmf-dist/web2c/updmap.cfg

  cd $SRC/texi2html-*
  ./configure --prefix=/usr
  make
  make install DESTDIR=$PKG

  # Fix some paths
  sed -e 's|TEXMFROOT = .*|TEXMFROOT = $SELFAUTODIR/share|g' \
    -e 's|TEXMFLOCAL = .*|TEXMFLOCAL = $TEXMFROOT/texmf-local|g' \
    -e 's|^OSFONTDIR.*|OSFONTDIR = ~/.fonts:/usr/share/fonts|' \
    -i $PKG/usr/share/texmf-dist/web2c/texmf.cnf

  # final clean up
  rm -r $PKG/usr/share/{info,locale}
  rm $PKG/usr/bin/tlmgr
  find $PKG/usr/share/texmf-dist \
    -regex '.*/\(COPYING\|README\|AUTHORS\|INSTALL\|ChangeLog\|NEWS\|TODO\|fonts_README.md\|BUGS\).*' \
    -exec rm '{}' \+
  find $PKG/usr/share/texi2html \
    -regex '.*/\(README\|COPYING\).*' \
    -exec rm '{}' \+
  # strip .la files
  rm -f $PKG/usr/lib/*.la
  # patch tabu.sty from https://github.com/tabu-issues-for-future-maintainer/tabu
  # to fix breakage when compiling doxygen
  ( cd $PKG/usr/share/texmf-dist/tex/latex/tabu
  patch -p1 --verbose < $SRC/tabu.sty.diff || exit 1
  )
}