default_pre_build      &&
cd "$SOURCE_DIRECTORY" &&

apply_patch_dir patches &&

# Fix linking of dvisvgm; configure is broken
sedit 's/@LIBGS_LIBS@/-lgs/' texk/dvisvgm/dvisvgm-src/src/Makefile.in &&

# removing as much internal things as possible
      #asymptote \
      #devnag \
      #lacheck \
      #m-tx \
      #pmx \
      #texdoctk \
      #tpic2pdftex \
      #vlna \
      #xindy \
for i in \
      ps2eps \
      t1utils \
      xpdfopen
do
  message "${MESSAGE_COLOR}Removing$DEFAULT_COLOR utils/$i" &&
  rm -rf utils/"$i" || exit 1
done &&

# Override bundled libraries' buildsystems to force system versions
# XXX lua52 and luajit can't be so easily removed due to internal API
# dependencies.
# The whole xpdf business is confusing. We need an overhaul of dependencies,
# anyway for the 2022 update. Just getting it to build!
#      xpdf \
for i in \
      cairo \
      freetype2 \
      gd \
      gmp \
      graphite2 \
      harfbuzz \
      icu \
      libpaper \
      libpng \
      mpfr \
      pixman \
      potrace \
      zlib \
      zziplib
do
  message "${MESSAGE_COLOR}Removing$DEFAULT_COLOR libs/$i" &&
  rm -rf libs/"$i" &&
  mkdir  libs/"$i" &&
  cat <<"!" > libs/"$i"/configure &&
#!/bin/sh
echo '%:;echo Warning: $@ target suppressed' > Makefile
!
  chmod 755 libs/"$i"/configure
done &&

# Use /usr/share/texmf and /usr/share/texmf-dist, adapted from lunarlinux
find \( -name Makefile\* -o -name configure \) \
       -exec sed -i -e "s:\(\$.\)prefix\(./tex\):\1datadir\2:" {} + \
  -o \( -name \*.info -o -name \*.texi \) \
       -exec sed -i -e "s:/usr/local:/usr:" {} + &&

# Patch paths in texmf.cnf
sedit '
	/^TEXMFROOT =/   s@=.*@= $SELFAUTODIR/share@
	/^TEXMFLOCAL =/  s@=.*@= $SELFAUTODIR/local/share/texmf@
	/^TEXMFSYSVAR =/ s@=.*@= $SELFAUTOPARENT/var/lib/texmf@
' texk/kpathsea/texmf.cnf &&

# ICU fix
sed '/utypes.h/i\
#include <unicode/utf16.h>
' -i texk/upmendex/mendex.h &&

mkdir "$COMPILE_DIRECTORY"
