#!/bin/bash
shopt -s extglob
ulimit -s unlimited

# texlive.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2009-2026  Patrick J. Volkerding, Sebeka, MN, USA
# Copyright 2009-2014  Robby Workman, Northport, AL, USA
# Copyright 2016-2017  Johannes Schoepfer, Germany
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

case $ARCH in
   arm|aarch64) SLKLDFLAGS="-Wl,-z,relro" ;;
esac

# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull the version numbers from the x86 trunk:
slack_setvarfromupstream TEXMFVERSION

# Extract source:
tar xf $CWD/$PKGNAM-*source.tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || failextract
slackhousekeeping

# Apply patches:
# Make aftergroup work again with the output routine
cat $CWD/patches/aftergroup.patch | patch -p1 --verbose || exit 1

# Don't break texdoc et al every time zlib is upgraded:
cat $CWD/patches/quit-breaking-on-minor-zlib-bump.patch | patch -p1 --verbose || exit 1

cat $CWD/patches/woff.gcc15.patch | patch -p1 --verbose || exit 1
cd texk/dvisvgm/dvisvgm-src
cat $CWD/patches/dvisvgm-3.4-gcc15-cstdint.patch | patch -p1 --verbose || exit 1
cd -

# GCC15 workaround:
SLKCFLAGS+=" -std=gnu17 -Wno-template-body"

# Configure:
mkdir build
pushd build
CFLAGS="$SLKCFLAGS -Wno-error=incompatible-pointer-types" \
CXXFLAGS="$SLKCFLAGS" \
LDFLAGS="$SLKLDFLAGS" \
../configure -C \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --infodir=/usr/info \
   --with-banner-add=' on Slackware' \
   --disable-native-texlive-build \
   --enable-largefile \
   --enable-shared \
   --disable-static \
   --disable-missing \
   --disable-multiplatform \
   --disable-debug \
   --disable-dialog \
   --disable-xz \
   --with-x \
   --enable-xindy \
   --disable-linked-scripts \
   --disable-xindy-docs \
   --disable-texdoctk \
   --disable-xindy-rules \
   --with-clisp-runtime=system \
   --enable-gc=system \
   --with-system-libpng \
   --with-system-t1lib \
   --with-system-gd \
   --with-system-freetype2 \
   --with-system-libgs \
   --with-system-icu \
   --with-system-pixman \
   --with-system-cairo \
   --with-system-gmp \
   --with-system-mpfr \
   --with-system-fontconfig \
   --with-system-ncurses \
   --with-system-harfbuzz \
   --with-system-graphite2 \
   --with-system-libpaper \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake
# This failes on the 'mendex' test - it includes some of the Slackware ARM build
# system scripts into the logs.. no idea.
#make check || failmake

# Install into package framework:
make install-strip DESTDIR=$PKG || failinstall
popd

TEXMFROOT=/usr/share
# Remove all files which are covered by tlnet
rm -rf $PKG$TEXMFROOT $PKG/usr/man $PKG/usr/info

# install the tlnet stuff
mkdir -p $PKG$TEXMFROOT
tar xvf $CWD/texlive-base-$TEXMFVERSION.tar.xz -C $PKG$TEXMFROOT || exit 1
chown -R root:root $PKG
chmod -R u+w,go-w,a+rX-st $PKG

# use symlinks/scripts from tlnet
mv $PKG$TEXMFROOT/texmf-dist/linked_scripts/* $PKG/usr/bin
rmdir $PKG$TEXMFROOT/texmf-dist/linked_scripts

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

# provide texlive fonts optionally for other system apps
mkdir -p $PKG/usr/share/fontconfig/conf.avail
cat > $PKG/usr/share/fontconfig/conf.avail/09-texlive.conf << EOF
<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
   <dir>$TEXMFROOT/texmf-dist/fonts/opentype</dir>
   <dir>$TEXMFROOT/texmf-dist/fonts/truetype</dir>
</fontconfig>
EOF

# Install the docs while we're still here
mkdir -p $PKG/usr/doc/texlive-$VERSION
cp -a ChangeLog README* $PKG/usr/doc/texlive-$VERSION
# Install index of provided tex packages to the docs
mv -fv $PKG$TEXMFROOT/texmf-dist/packages.base.gz $PKG/usr/doc/texlive-$VERSION

# Put a symlink to $TEXMFROOT/texmf-dist/doc in our Slackware docdir
ln -srf $PKG$TEXMFROOT/texmf-dist/doc $PKG/usr/doc/texlive-$VERSION/doc

# We won't make tlmgr easily available; it's still there, but
# we'll make it harder to void the warranty :-)
rm -f $PKG/usr/bin/tlmgr
install -vpm644 $CWD/README.tlpkg $PKG/usr/doc/texlive-$VERSION/

# Create some directories to make it clear that they're owned by this package
mkdir -vpm755 $PKG$TEXMFROOT/{texmf-config,texmf-var,texmf-local}

# Add texlive perl modules
mkdir -p $PKG/usr/share/perl5
mv -fv $PKG$TEXMFROOT/texmf-dist/scripts/texlive/TeXLive $PKG/usr/share/perl5/

# Move manual pages to the correct place
mkdir $PKG/usr/man
mv -fv $PKG$TEXMFROOT/texmf-dist/doc/man/man1 $PKG/usr/man
mv -fv $PKG$TEXMFROOT/texmf-dist/doc/man/man5 $PKG/usr/man
rmdir $PKG$TEXMFROOT/texmf-dist/doc/man

# Handle the GNU info pages
mv -fv $PKG$TEXMFROOT/texmf-dist/doc/info $PKG/usr

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set standard Slackware file/dir permissions and ownerships
slackdesc       # install slack-desc and doinst.sh

# ARM note: Note that the doinst.sh will have been placed by "slackdesc" above, but we'll overwrite
#           it below.
#
# Don't demand a global lock when running the install script:
echo "# This install script should not collide with any other script's activities," > $PKG/install/doinst.sh
echo "# so don't demand a lock by installpkg: NOLOCK" >> $PKG/install/doinst.sh
zcat $CWD/doinst.sh.gz >> $PKG/install/doinst.sh
install -vpm644 $CWD/douninst.sh $PKG/install/

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
