#!/bin/bash

# Slackware build script for feh

# Copyright 2006-2009 Chess Griffin
# Copyright 2011-2012 Grigorios Bouzakis
# Copyright 2014-2015 Luka Novsak
# Copyright 2017-2021 Hunter Sezen
# Copyright 2024 B. Watson <urchlay@slackware.uk>
# Copyright 2024 Lumin Etherlight <lumin+slackbuilds@etherlight.link>
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright
# notice and this permission notice appear in all copies.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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
# AUTHORS AND COPYRIGHT HOLDERS AND THEIR CONTRIBUTORS 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.

# 20250124 lumin:
# - add feature macro to expose mkstemps(3) support.
# 20240916 lumin:
# - new maintainer
# 20240903 bkw:
# - new maintainer
# - update for v3.10.3
# - build with all useful options enabled, instead of having 4
#   separate environment vars.
# - add rest of docs to doc dir.

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

PRGNAM=feh
VERSION=${VERSION:-3.10.3}
BUILD=${BUILD:-2}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -eu

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
chmod 0755 $PRGNAM-$VERSION
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 {} +

# Add _DEFAULT_SOURCE to allow mkstemps usage.
# Some setups may error out on the implicit
# declaration of mkstemps otherwise.
CFLAGS="$SLKCFLAGS -D_DEFAULT_SOURCE" \
make \
  PREFIX=/usr \
  curl=1 \
  debug=1 \
  exif=1 \
  help=1 \
  inotify=1 \
  stat64=1 \
  magic=1 \
  mkstemps=1 \
  verscmp=0 \
  xinerama=1

strip src/feh

make install \
  DESTDIR=$PKG \
  PREFIX=/usr \
  man_dir=$PKG/usr/man \
  doc_dir=$PKG/usr/doc/$PRGNAM-$VERSION \
  example_dir=$PKG/usr/doc/$PRGNAM-$VERSION/examples

gzip -9 $PKG/usr/man/man*/*

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a AUTHORS COPYING ChangeLog README.md TODO $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $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
