#!/bin/bash

# Slackware build script for meh

# Original author: Leonard Schmidt <email removed>.
# Now maintained by B. Watson <urchlay@slackware.uk>.

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

# 20260114 bkw: update for v0.3+20181021_69f653a.
# - relicense as WTFPL, with permission from Leonard.
# - this is the last git commit from upstream.
# - this is the same version FreeBSD ports has as "0.3.27".
# - redo man page as rst, add new key bindings.
# - rework giflib patch for this version.
# - add key bindings space (next) and backspace (previous).

# 20250117 bkw: BUILD=2
# - stop spewing spurious 'GIFLIB: (null)' to stderr when viewing gifs.
# - fix imagemagick support so it actually *works*.
# - add GIF to the list of built-in formats in README, man page.
# - mention that animations aren't supported in README, man.

# 20180628 bkw:
# - Take over maintenance.
# - Add man page, BUILD=2
# - Script simplifications.

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

PRGNAM=meh
VERSION=${VERSION:-0.3+20181021_69f653a}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
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 . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# 20260114 bkw: supposedly upstream fixed the 'GIFLIB: (null)' spam
# on stderr, but... not quite. this is now a one-line patch.
patch -p1 < $CWD/meh-giflib.patch

# 20260114 bkw: every image viewer I use (qiv, pqiv, etc) supports
# Space => next image. Since the spacebar doesn't do anything else
# in meh, make it do what I want.
patch -p1 < $CWD/space-backspace.diff

# 20250117 bkw: this fixes imagemagick support, so we can view e.g.
# webp, jpeg2000, etc.
sed -i '/argv/s,"255","16",' src/imagemagick.c

# 20260114 bkw: have the -v (version) option report the full version.
sed -i "/meh version 0/s,0\\.3,$VERSION," src/main.c

CFLAGS="$SLKCFLAGS" make
install -D -s -o root -g root -m 755 $PRGNAM $PKG/usr/bin/$PRGNAM

# 20180628 bkw: Man page originally came from upstream's git, and I
# added the key bindings section (and sent a patch upstream, hopefully
# he likes it).
# 20250119 bkw: add GIF to the list of builtin types, mention that
# animation's not supported.
# 20260114 bkw: add new keystrokes to man.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a AUTHORS BUGS COPYING NEWS README THANKS $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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