#!/bin/bash

# Slackware build script for fbida

# (C) 2007 Michael Wagner <email removed>
# 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.

# Modified by Luis Henrique <email removed>
# Modified and currently maintained by B. Watson <urchlay@slackware.uk>

# 20261019 bkw:
# - update for v2.15_1
# - new homepage, since the old one doesn't mention the new version.
# - lots of script changes (upstream now uses meson), can't build
#   old versions.

# 20170921 bkw:
# - update for v2.14
# - remove old maintainers' email addresses
# - fix French man page stuff for new version, but allow old version
#   to still build via VERSION=2.13 in env.
# - add man page for fbipdf.

# 20170703 bkw:
# - update for v2.13
# - use new homepage
# - verbose make

# 20161015 bkw:
# - update for v2.12
# - add optional motif support, since Slack 14.2 has real Motif. Also
#   add .desktop and icon, if motif was included.
# - actually apply SLKCFLAGS
# - 2.12 has a new fbpdf command, which I've renamed to
#   fbipdf to avoid conflicting with the fbpdf package
# - include French man pages, which aren't installed by default

# 20150508 bkw: update for v2.10

# 20140915 bkw: take over maintenance, minor script cleanup

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

PRGNAM=fbida
VERSION=${VERSION:-2.15_1}
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" -o "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

TARNAM=$PRGNAM-$PRGNAM-${VERSION/_/-}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $TARNAM
tar xvf $CWD/$TARNAM.tar.gz --no-same-owner
cd $TARNAM
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 {} + \) \)

# 20261019 bkw: app-defaults belongs in /etc/X11/app-defaults on
# Slackware, not /usr/lib(64)/X11/app-defaults, even though the latter
# is a symlink to the former.
sed -i "/^x11resdir/s,=.*,= '/etc/X11/app-defaults/'," meson.build

# 20261020 bkw: fix typo and help message.
sed -i -e 's,\<pfd\>,pdf,' -e 's,"fbpdf ,"fbipdf ,' fbpdf.c

MOTIF_OPT="enabled"
[ "${MOTIF:-yes}" != "yes" ] && MOTIF_OPT="disabled"

# 20261020 bkw: gif and webp weren't working in ida, which is why
# all the stuff is force-enabled below.
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 \
  -Dpng=enabled \
  -Dtiff=enabled \
  -Dgif=enabled \
  -Dwebp=enabled \
  -Dmotif=$MOTIF_OPT \
  build/
"${NINJA:=ninja}" -vC build/
DESTDIR=$PKG $NINJA -vC build/ install

PKGBIN=$PKG/usr/bin
PKGMAN1=$PKG/usr/man/man1
PKGMANFR1=$PKG/usr/man/fr/man1
mkdir -p $PKGMAN1 $PKGMANFR1

# 20261019 bkw: these man pages don't get installed.
cp -a man/fbgs.1 $PKGMAN1
[ -e $PKGBIN/ida ] || rm -f man/fr/ida.1
cp -a man/fr/*.1 $PKGMANFR1

# 20261019 bkw: and this man page is local
cat $CWD/fbipdf.1 > $PKGMAN1/fbipdf.1

gzip $PKGMAN1/*.1 $PKGMANFR1/*.1

# 20261019 bkw: this gets installed with wrong permissions:
chmod 755 $PKGBIN/fbgs

# 20261019 bkw: avoid conflict with fbpdf package.
mv $PKGBIN/fbpdf $PKGBIN/fbipdf

# upstream's .desktop file doesn't validate, we ship a corrected one,
# but only include it if motif support was built in. PNG icon was
# converted from mallard_48.xpm in the src.
if [ -e $PKGBIN/ida ]; then
  ICONDIR=$PKG/usr/share/icons/hicolor/48x48/apps
  APPDIR=$PKG/usr/share/applications
  PIXDIR=$PKG/usr/share/pixmaps
  mkdir -p $APPDIR $ICONDIR $PIXDIR
  cat $CWD/ida.desktop > $APPDIR/ida.desktop
  cat $CWD/ida.png > $ICONDIR/ida.png
  ln -s ../icons/hicolor/48x48/apps/ida.png $PIXDIR/ida.png
fi

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a README* Changes COPYING TODO $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild
chmod 644 $PKGDOC/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
[ -e $PKGBIN/ida ] && cat $CWD/doinst.sh > $PKG/install/doinst.sh

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