#!/bin/bash

# Slackware build script for magnus

# Written by B. Watson (urchlay@slackware.uk)

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

# 20230117 bkw: BUILD=2, go ahead and include an icon instead of a symlink.

# This thing is more flexible and featureful than mag, but slower and
# more resource-intensive (due to being written in python, I guess).

# Notes:

# The default refresh interval is 250ms, or 4 frames/sec. This is
# painful to use. On my not-very-powerful machine, 15ms (67fps) is
# fine. I'm patching the code to set the default to 30ms. Also, the
# man page and --help output claim the default is 120ms...

# This thing draws its own window titlebar and decorations, which look
# and behave different from *everything else* I use. Very annoying,
# and I'm told that's the direction the GTK/Gnome crowd is moving
# towards, especially in GTK+4.

# The default behaviour is to only update the magnified view when the
# mouse moves, which makes this pretty useless IMO. Upstream added
# a patch in git that adds a --force-refresh option, which makes it
# always update. What I'm doing here: patch it so it always refreshes,
# without having to pass an argument.

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

PRGNAM=magnus
VERSION=${VERSION:-1.0.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}

set -e

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

# always refresh even if the mouse hasn't moved.
sed -i '/def *poll/s,=False,=True,' $PRGNAM

# set the default refresh rate to something usable.
REFRESH=${REFRESH:-30}
sed -i -e '/refresh_interval *=/s,250,'$REFRESH',' \
       -e '/--refresh-interval=/s,120,'$REFRESH',' \
       $PRGNAM
sed -i 's,120ms,'$REFRESH',' data/$PRGNAM.1

python3 setup.py install --root=$PKG

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README.md LICENSE $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mv $PKG/usr/share/man $PKG/usr/man
gzip -9 $PKG/usr/man/man?/*

sed -i '/^Icon/s,=.*,=magnus,' $PKG/usr/share/applications/$PRGNAM.desktop
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
cat $CWD/$PRGNAM.png > $PKG/usr/share/icons/hicolor/48x48/apps/$PRGNAM.png

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
