#!/bin/bash

# Slackware build script for evince

# Copyright 2009  Andrew Brouwers <email removed>
# Copyright 2009-2014, Michiel van Wessem, Manchester, United Kingdom.
# Copyright 2019, B. Watson
# 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.

# Now maintained by B. Watson <urchlay@slackware.uk>

# 20230104 bkw: update for v41.5 (see README for why it's not 42.x or 43.x)

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

PRGNAM=evince
VERSION=${VERSION:-48.4}
BUILD=${BUILD:-1}
TAG=${TAG:-_lngn}
PKGTYPE=${PKGTYPE:-txz}

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/lngn}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686 -DHAVE_STRING_H -DHAVE_STDLIB_H"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2 -DHAVE_STRING_H -DHAVE_STDLIB_H"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86-64 -mtune=generic -fPIC -DHAVE_STRING_H -DHAVE_STDLIB_H"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -march=armv8-a -mtune=generic -fPIC -DHAVE_STRING_H -DHAVE_STDLIB_H"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# Detect nautilus is installed and enable building the plugin:
if ( pkg-config --exists libnautilus-extension-4 ); then
  NAUTILUS_PLUGIN=true
  echo "nautilus found, enabling plugin."
else
  NAUTILUS_PLUGIN=false
fi

#set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+
# Moving set -e here since the build pukes over the above find
set -e

mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson setup .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dstrip=true \
    -Ddbus=${DBUS:-true} \
    -Dcomics=enabled \
    -Ddjvu=enabled \
    -Ddvi=enabled \
    -Dpdf=enabled \
    -Dps=enabled \
    -Dtiff=enabled \
    -Dnautilus=false \
    -Dgtk_doc=false \
    -Dsystemduserunitdir=no
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

gzip -9 $PKG/usr/man/man1/*.1

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS *.md COPYING MAINTAINERS NEWS* NOTES README* TODO \
      $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-required > $PKG/install/slack-required
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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