# Maintainer: @naveen521kk on Github, Naveen M K <naveen521kk@gmail.com>

pkgbase=mingw-w64-texlive-bin
pkgname=("${MINGW_PACKAGE_PREFIX}-texlive-bin" "${MINGW_PACKAGE_PREFIX}-libsynctex")
pkgver=2021.20210424
pkgdesc="TeX Live binaries (mingw-w64)"
pkgrel=5
license=('GPL')
arch=('any')
url='https://tug.org/texlive/'
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-perl"
    "${MINGW_PACKAGE_PREFIX}-cairo"
    "${MINGW_PACKAGE_PREFIX}-pixman"
    "${MINGW_PACKAGE_PREFIX}-graphite2"
    "${MINGW_PACKAGE_PREFIX}-libsigsegv"
    "${MINGW_PACKAGE_PREFIX}-zziplib"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
    "${MINGW_PACKAGE_PREFIX}-freetype"
    "${MINGW_PACKAGE_PREFIX}-icu"
    "${MINGW_PACKAGE_PREFIX}-harfbuzz"
    "${MINGW_PACKAGE_PREFIX}-gmp"
    "${MINGW_PACKAGE_PREFIX}-mpfr"
    "${MINGW_PACKAGE_PREFIX}-potrace"
    "${MINGW_PACKAGE_PREFIX}-libpaper"
    "${MINGW_PACKAGE_PREFIX}-ffcall"
    "${MINGW_PACKAGE_PREFIX}-libgd"
    "${MINGW_PACKAGE_PREFIX}-lua"
    "${MINGW_PACKAGE_PREFIX}-luajit"
    "${MINGW_PACKAGE_PREFIX}-ghostscript"
    # perl dependencies
    "${MINGW_PACKAGE_PREFIX}-perl-win32-api"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-console"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-shortcut"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-tieregistry"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-winerror"
    "${MINGW_PACKAGE_PREFIX}-perl-win32api-registry"
    "${MINGW_PACKAGE_PREFIX}-perl-file-which"
    "${MINGW_PACKAGE_PREFIX}-perl-io-string"
)
_commit=ccb6338507149edf2772bfccb9933b72761f4cc4 # svn://tug.org/texlive/trunk/Build/source@58686
source=(
    "texlive-source-${_commit}.tar.gz::https://github.com/Tex-Live/texlive-source/archive/${_commit}.tar.gz"
    "0001-Remove-DLUASOCKET_INET_PTON-from-Makefile.patch"
    "0002-fix-lauchers-mingw32.patch"
    "0003-runscript-always-quote-args.patch")
sha256sums=('f17bdf0fba64790151b39bcb1d6d4f0ee97da6bcf15b4c9282ff9056bb8da5ee'
            '86ef2a99b2f0caed42777419837d9b539af1d1c8a101701e539e5467bb88662e'
            'decff8db61302c2a1760cfd4b42aaf0e25fcb7d2b9621cea3ec3f4c2557976a1'
            '0339fe12b2ec7537ce62d064fffd43e8ee02280ffac3dd012f07dc17a713acdc')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
    mv -f "$srcdir/texlive-source-${_commit}" "$srcdir/texlive-source"
    cd "$srcdir/texlive-source"
    apply_patch_with_msg 0001-Remove-DLUASOCKET_INET_PTON-from-Makefile.patch
    apply_patch_with_msg 0003-runscript-always-quote-args.patch
    # t4ht expects to be un /usr/share/texmf/bin/t4ht (FS#27251)
    sed -i s/SELFAUTOPARENT/TEXMFROOT/ texk/tex4htk/t4ht.c

}

build() {
   cd "$srcdir/texlive-source"

   #############################################################
   ### configure
   mkdir -p Work
   cd Work
   echo "--> Initial configuration..."
   # we use temporary prefix to avoid messing the existing
   # $pkgdir/usr/share/texmf tree
   ../configure --prefix="${MINGW_PREFIX}" -C \
     --build=${MINGW_CHOST} \
     --host=${MINGW_CHOST} \
     --target=${MINGW_CHOST} \
     --disable-static \
     --enable-shared \
     --disable-missing \
     --disable-native-texlive-build \
     --with-banner-add="/Built by MSYS2 project" \
     --disable-multiplatform \
     --disable-t1utils \
     --disable-bibtexu \
     --with-system-zlib \
     --with-system-zziplib \
     --with-system-libpng \
     --with-system-ncurses \
     --with-system-gd \
     --with-system-freetype2 \
     --with-system-pixman \
     --with-system-cairo \
     --with-system-harfbuzz \
     --with-system-graphite2 \
     --with-system-icu \
     --with-system-gmp \
     --with-system-mpfr \
     --with-system-potrace \
     --with-system-libpaper \
     --with-xdvi-x-toolkit=xaw \
     --disable-dump-share \
     --disable-aleph \
     --disable-xpdfopen
   #############################################################
   ### make
   echo "-------------------------------------------------------"
   echo "--> Building the whole beast ..."
   echo "-------------------------------------------------------"
   make

   msg "Building Lauchers"
   # let's build lauchers first
   # it is present in texk/texlive/w32_wrapper/ (for mingw32) 
   # and ../texk/texlive/w64_mingw_wrapper (for mingw64) wrapper

   # first delete *.exe and *.dll from the locations
   rm ../texk/texlive/w64_mingw_wrapper/*.exe ../texk/texlive/w64_mingw_wrapper/*.dll \
      ../texk/texlive/w64_mingw_wrapper/context/*.exe ../texk/texlive/w64_mingw_wrapper/context/*.dll
   case ${MINGW_CHOST} in
      i686-w64-mingw32)
         rm -rf ../texk/texlive/w32_wrapper
         cp -r ../texk/texlive/w64_mingw_wrapper ../texk/texlive/w32_wrapper
         cp "libs/lua53/.libs/texlua.dll" ../texk/texlive/w32_wrapper
         cd ../texk/texlive/w32_wrapper
         patch -i "${srcdir}/0002-fix-lauchers-mingw32.patch"
         ;;
      *)
         cp "libs/lua53/.libs/texlua.dll" ../texk/texlive/w64_mingw_wrapper
         cd ../texk/texlive/w64_mingw_wrapper
         ;;
   esac
   echo '1 ICON "tlmgr.ico"'>texlive.rc
   windres texlive.rc texlive.o

   gcc -Os -s -shared -o runscript.dll runscript_dll.c -L./ -ltexlua
   gcc -Os -s -o runscript.exe runscript_exe.c texlive.o -L./ -lrunscript
   gcc -mwindows -Os -s -o wrunscript.exe wrunscript_exe.c texlive.o -L./ -lrunscript

   cd context
   gcc -Os -s -shared -o mtxrun.dll mtxrun_dll.c
   gcc -Os -s -o mtxrun.exe mtxrun_exe.c -L./ -lmtxrun
   cd ..
   #cleanup
   rm texlive.rc texlive.o texlua.dll
}

package_libsynctex() {
    pkgdesc='Library for synchronization between TeX files and resulting file (mingw-w64)'
    depends=("${MINGW_PACKAGE_PREFIX}-zlib")

    cd "${srcdir}/texlive-source/Work"
    make -C texk/web2c DESTDIR="${pkgdir}" \
        install-data-am install-libLTLIBRARIES
}

package_texlive-bin() {
  pkgdesc="TeX Live binaries"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-cairo"
    "${MINGW_PACKAGE_PREFIX}-pixman"
    "${MINGW_PACKAGE_PREFIX}-graphite2"
    "${MINGW_PACKAGE_PREFIX}-libsigsegv"
    "${MINGW_PACKAGE_PREFIX}-zziplib"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
    "${MINGW_PACKAGE_PREFIX}-freetype"
    "${MINGW_PACKAGE_PREFIX}-icu"
    "${MINGW_PACKAGE_PREFIX}-harfbuzz"
    "${MINGW_PACKAGE_PREFIX}-gmp"
    "${MINGW_PACKAGE_PREFIX}-mpfr"
    "${MINGW_PACKAGE_PREFIX}-potrace"
    "${MINGW_PACKAGE_PREFIX}-libpaper"
    "${MINGW_PACKAGE_PREFIX}-libgd"
    "${MINGW_PACKAGE_PREFIX}-libsynctex"
    "${MINGW_PACKAGE_PREFIX}-lua"
    "${MINGW_PACKAGE_PREFIX}-luajit"
    "${MINGW_PACKAGE_PREFIX}-ghostscript"
    # perl dependencies
    "${MINGW_PACKAGE_PREFIX}-perl-win32-api"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-console"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-shortcut"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-tieregistry"
    "${MINGW_PACKAGE_PREFIX}-perl-win32-winerror"
    "${MINGW_PACKAGE_PREFIX}-perl-win32api-registry"
    "${MINGW_PACKAGE_PREFIX}-perl-file-which"
    "${MINGW_PACKAGE_PREFIX}-perl-io-string")
  provides=(
    "${MINGW_PACKAGE_PREFIX}-lcdf-typetools"
    "${MINGW_PACKAGE_PREFIX}-kpathsea"
    #"${MINGW_PACKAGE_PREFIX}-xindy" #once clisp is build this needs to be enabled
  )
  #optdepends=('ed: for texconfig'
  #            'biber: for bibliography processing')
  options=('!strip')

  cd "$srcdir/texlive-source"

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

  # should be updated with what arch does.
  ## symlink engines by hand. texlinks has moved to texlive-core...
  cd "${pkgdir}${MINGW_PREFIX}"/bin
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/bin
  install -D -m755 eptex.exe ${pkgdir}${MINGW_PREFIX}/bin/platex.exe
  install -D -m755 euptex.exe ${pkgdir}${MINGW_PREFIX}/bin/uplatex.exe
  install -D -m755 luatex.exe ${pkgdir}${MINGW_PREFIX}/bin/dvilualatex.exe
  install -D -m755 luatex.exe ${pkgdir}${MINGW_PREFIX}/bin/dviluatex.exe
  install -D -m755 luahbtex.exe ${pkgdir}${MINGW_PREFIX}/bin/lualatex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/amstex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/cslatex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/csplain.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/eplain.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/etex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/jadetex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/latex.exe
  install -D -m755 tex.exe ${pkgdir}${MINGW_PREFIX}/bin/lollipop.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/mex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/mllatex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/mltex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfetex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfcslatex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfcsplain.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfjadetex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdflatex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfmex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/pdfxmltex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/texsis.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/utf8mex.exe
  install -D -m755 pdftex.exe ${pkgdir}${MINGW_PREFIX}/bin/xmltex.exe
  install -D -m755 xetex.exe ${pkgdir}${MINGW_PREFIX}/bin/xelatex.exe
   
   # remove dangling executables, they are available in seperate pacakges
   _bibtexextra_scripts="
bbl2bib
bib2gls
bibdoiadd
bibexport
bibmradd
biburl2doi
bibzbladd
convertgls2bib
listbib
ltx2crossrefxml
multibibliography
urlbst
"
   _core_scripts="
a2ping
a5toa4
adhocfilelist
afm2afm
albatross
allcm
allec
allneeded
arara
arlatex
autoinst
bundledoc
checkcites
checklistings
chklref
chkweb
cjk-gs-integrate
cluttex
cllualatex
clxelatex
context
contextjit
ctanbib
ctanify
ctanupload
ctan-o-mat
de-macro
depythontex
deweb
dosepsbin
dtxgen
dvi2fax
dviasm
dviinfox
dvired
e2pall
epstopdf
findhyph
fmtutil
fmtutil-sys
fmtutil-user
fontinst
fragmaster
git-latexdiff
ht
htcontext
htlatex
htmex
httex
httexi
htxelatex
htxetex
installfont-tl
jfmutil
ketcindy
kpsepath
kpsetool
kpsewhere
kpsexpand
latex-git-log
latex-papersize
latex2man
latex2nemeth
latexdef
latexdiff
latexdiff-vc
latexfileversion
latexindent
latexmk
latexpand
latexrevise
listings-ext.sh
llmk
ltxfileinfo
ltximg
luaotfload-tool
luatools
lwarpmk
make4ht
match_parens
mf2pt1
mk4ht
mkjobtexmf
mkt1font
mktexfmt
mktexlsr
mktexmf
mktexpk
mktextfm
mptopdf
mtxrun
mtxrunjit
ot2kpx
pamphletangler
pdfatfi
pdfbook2
pdfcrop
pdfjam
pdflatexpicscale
pdftex-quiet
pdfxup
pfarrei
pkfix
pkfix-helper
ps2eps
ps2frag
pslatex
purifyeps
pythontex
repstopdf
rpdfcrop
rungs
simpdftex
spix
srcredact
sty2dtx
tex4ebook
texconfig
texconfig-dialog
texconfig-sys
texcount
texdef
texdiff
texdirflatten
texdoc
texdoctk
texexec
texfot
texhash
texindy
texlinks
texliveonfly
texloganalyser
texmfstart
texosquery
texosquery-jre5
texosquery-jre8
texplate
thumbpdf
tlcockpit
tlshell
typeoutfileinfo
updmap
updmap-sys
updmap-user
vpl2ovp
vpl2vpl
xhlatex
xindex
xindy
"
   _games_scripts="rubikrotation"
   _humanities_scripts="diadia"
   _langcyrillic_scripts="rubibtex rumakeindex"
   _langextra_scripts="ebong"
   _langgreek_scripts="mkgrkindex"
   _langjapanese_scripts="convbkmk ptex2pdf
kanji-fontmap-creator
kanji-config-updmap
kanji-config-updmap-sys
kanji-config-updmap-user
"
   _langkorean_scripts="jamo-normalize komkindex ttf2kotexfont"
   _latexextra_scripts="
authorindex
exceltex
hyperxmp-add-bytecount
l3build
makedtx
makeglossaries
makeglossaries-lite
pdfannotextractor
perltex
ps4pdf
splitindex
svn-multi
vpe
webquiz
wordcount
yplan"
   _music_scripts="lily-glyph-commands lily-image-commands lily-rebuild-pdfs
m-tx
musixtex
musixflx
pmxchords"
   _pictures_scripts="
cachepic
epspdf
epspdftk
fig4latex
getmapdl
mathspic
mkpic
pn2pdf
tikztosvg"
   _pstricks_scripts="pedigree pst2pdf"
   _science_scripts="pygmentex ulqda"
   for s in \
        ${_bibtexextra_scripts} \
        ${_core_scripts}        \
        ${_games_scripts}       \
        ${_htmlxml_scripts}     \
        ${_humanities_scripts}   \
        ${_langcyrillic_scripts} \
        ${_langextra_scripts}    \
        ${_langgreek_scripts}    \
        ${_langjapanese_scripts} \
        ${_langkorean_scripts} \
        ${_latexextra_scripts} \
        ${_music_scripts}      \
        ${_pictures_scripts}   \
        ${_pstricks_scripts}   \
        ${_science_scripts}    \
        tlmgr; do
        rm -f "${pkgdir}${MINGW_PREFIX}"/bin/$s.exe
   done
   ###################################################################

   # remove libsynctex
   rm -f "${pkgdir}${MINGW_PREFIX}"/include/synctex/*
   rm -f "${pkgdir}${MINGW_PREFIX}"/lib/libsynctex.*
   rm -f "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/synctex.pc
   rm -f "${pkgdir}${MINGW_PREFIX}"/share/man/man*/synctex.*
   rm -f "${pkgdir}${MINGW_PREFIX}"/bin/libsynctex-2.dll
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
