#!/bin/bash

# Slackware build script for mupdf

# Originally written by Hubert Hesse (email removed).
# Heavily modified by B. Watson (urchlay@slackware.uk).

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20251228 bkw: update for 1.27.0.
# 20251207 bkw: update for 1.26.12.
# 20251109 bkw: update for 1.26.11.
# 20250918 bkw:
# - update for 1.26.8. can't build older versions, sorry.
# - move old change-comments to ChangeLog.old.
# - fix .pc file generation.
# 20241010 bkw: Modified by SlackBuilds.org, BUILD=2:
# - Bump BUILD for gumbo-parser-0.12.1
# 20240925 bkw: update for 1.24.9.
# 20240813 bkw: update for 1.24.8.
# 20240314 bkw: update for 1.23.11.
# 20230531 bkw: BUILD=2, actually use SLKCFLAGS.
# 20230117 bkw: update for 1.21.1.
# - Again, this script can't build older versions.
# - There's a gumbo-parser SlackBuild now, so build with it instead of
#   upstream's bundled version.
# - tesseract builds work, so make that an option.
# - Include pre-scaled PNG icons.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=mupdf
VERSION=${VERSION:-1.27.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
set -e

# Building mupdf seems to work OK if an older version is installed,
# but I only tested 1.21.1 when 1.18.0 was installed (doesn't mean
# it works for all versions).
print_failed_message() {
  if pkg-config --exists mupdf; then
  cat 1>&2 <<EOF

**************************************************************************
* The build failed. This might have happened because you had an existing *
* (older) mupdf package installed. Run "removepkg mupdf" and try again.  *
**************************************************************************
EOF
  fi
  exit 1
}

# 20230117 bkw: tesseract builds work fine in 1.21.1, but tesseract
# doesn't get autodetected. Help it out a little.
TESS=no
[ "${TESSERACT:-yes}" = "yes" ] && pkg-config --exists tesseract && TESS=yes

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION-source
tar xvf $CWD/$PRGNAM-$VERSION-source.tar.lz
cd $PRGNAM-$VERSION-source

chown -R root:root .
find . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# 20230531 bkw: derp. Never did apply the flags...
sed -i "s,-O2,$SLKCFLAGS," Makerules

# Build against system libs instead of bundled ones, where possible.
# Upstream recommends using their bundled lcms2 and mujs, I'll go with that.
# Also, we *still* (in Slack 15.0) have to use the bundled freeglut
# to get copy/paste working in mupdf-gl.
# C++ (XCXXFLAGS) is only actually used if building with tesseract.
# 20250918 bkw: v1.26.8 adds barcode support via zxing. I used the
# bundled zxing source because the one on SBo is too old, and to
# avoid yet another dependency. I could have just said 'barcode=no'
# but it seems like a useful feature.

make verbose=yes \
  shared=yes \
  barcode=yes \
  USE_SYSTEM_LIBS=yes \
  USE_SYSTEM_GLUT=no \
  USE_SYSTEM_ZXINGCPP=no \
  tesseract=$TESS \
  build=release \
  prefix=/usr \
  libdir=/usr/lib$LIBDIRSUFFIX \
  mandir=/usr/man \
  docdir=/usr/doc/$PRGNAM-$VERSION \
  DESTDIR=$PKG \
  all extra-apps install install-extra-apps || print_failed_message


# bins and libs are already stripped.
gzip -9 $PKG/usr/man/man1/*.1

# 20230117 bkw: fix up the lib dir.
cd $PKG/usr/lib$LIBDIRSUFFIX
  # 20230117 bkw: AFAIK, we have to list every shared lib this one
  # depends on in the .pc file. This rather odd-looking bit of code
  # creates the list.
  # 20250918 bkw: skip ld-linux-* if it shows up. this avoids the
  # .pc file including "NEEDED     ld-linux-x86-64.so.2".
  PCLIBS="-lmupdf $(
    objdump -p libmupdf.so.*.* | \
       perl -ne 'next unless /NEEDED\s+lib/;
                 chomp;
                 s,.* lib([^.]*)\.so.*,\1,;
                 print "-l$_ " unless /^(c|gcc_s)$/;'
  )"
  WITHTESS=WITHOUT
  echo "$PCLIBS" | grep -q ltesseract && WITHTESS=WITH
  echo "==> PCLIBS='$PCLIBS'"

  # 20240813 bkw: shared library gets installed without symlinks...
  # 20250918 bkw: not in 1.26.8, but keep this here just in case.
  SO="$( echo libmupdf.so.*.* )"
  SOVER="$( echo $SO | cut -d. -f3,4 )"
  SOMAJOR="$( echo $SO | cut -d. -f3 )"
  chmod 755 $SO
  ln -s libmupdf.so.$SOVER libmupdf.so || true
  ln -s libmupdf.so.$SOVER libmupdf.so.$SOMAJOR || true

  # .pc file taken from debian and parameterized.
  mkdir -p pkgconfig/
  sed -e "s,@LIB@,lib$LIBDIRSUFFIX,g" \
      -e "s,@VERSION@,$VERSION,g" \
      -e "s,@PCLIBS@,$PCLIBS,g" \
      $CWD/$PRGNAM.pc > pkgconfig/$PRGNAM.pc

  # Historically, mupdf has included two libraries: libmupdf itself,
  # and libmupdf-third, containing all the bundled third party code
  # included with the source. In version >=1.18.0, when building a
  # shared lib, no libmupdf-third gets created (all the code ends up in
  # libmupdf.so). However, most projects that link with libmupdf expect
  # this library to exist. This will probably change in the future as
  # those projects get updated for the changes in mupdf, but for now,
  # to be compatible with older mupdf versions, we include an empty
  # libmupdf-third for stuff to link with.
  ar crs libmupdf-third.a
cd -

# Compatibility symlinks. Older versions, the binary is just "mupdf".
ln -s $PRGNAM-x11 $PKG/usr/bin/$PRGNAM
for i in x11 x11-curl gl; do
  [ -e $PKG/usr/bin/$PRGNAM-$i ] && \
    ln -s $PRGNAM.1.gz $PKG/usr/man/man1/$PRGNAM-$i.1.gz
done

# .desktop taken from debian and modified:
# - make it validate.
# - add mime types for cbz and xps.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Use shipped SVG icon, make PNGs at various sizes.
mkdir -p $PKG/usr/share/pixmaps $PKG/usr/share/icons/hicolor/scalable/apps
svg=docs/logo/mupdf-logo.svg
for i in 16 22 32 48 64 128; do
  px=${i}x${i}
  dir=$PKG/usr/share/icons/hicolor/$px/apps
  mkdir -p $dir
  rsvg-convert --width=$i --height=$i -o $dir/$PRGNAM.png $svg
done
cat $svg > $PKG/usr/share/icons/hicolor/scalable/apps/$PRGNAM.svg
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# 'make install' already installed most of the docs.
# 20240314 bkw: docs/api removed from upstream source.
cp -a CONTRIBUTORS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@WITHTESS@,$WITHTESS," < $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
