#!/bin/bash

# Slackware build script for tint

# Original author: Alexander Verbovetsky, Moscow, Russia.
# Modified and now maintained by B. Watson <urchlay@slackware.uk>.
# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20241008 bkw: v0.07, BUILD=2:
# - take over maintenance.
# - relicense as WTFPL.
# - fix .desktop file.
# - binary in /usr/games.
# - use a modified man page (include keystrokes, for one thing).
# - new-style icons.
# - fix score file handling in doinst.sh (relative path, not abs).
# - add CREDITS and copyright to doc dir.
# - patch the game to make the controls not suck. yes, this breaks
#   the Slackware philosophy; no, you're not going to change my mind.
#   build with PRISTINE=yes if it bothers you.

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

PRGNAM=tint
VERSION=${VERSION:-0.07}
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 -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 {} +

# 20241008 bkw: I find this game utterly unplayable without this patch.
# I keep hitting the up-arrow, expecting it to rotate the piece like
# in all the other tetris clones I've played. Instead, it speeds up
# the game... and there's no "slow down" option, in case I hit it by
# accident.
# With the patch, up is rotate, and speed-up is still available
# with the A key. The patch also allows uppercase keystrokes, in case
# the user's dumb enough to have caps lock enabled.
[ "${PRISTINE:-no}" = "yes" ] || patch -p1 < $CWD/key_up_rotate.diff

CFLAGS="$SLKCFLAGS" \
make

mkdir -p $PKG/usr/games $PKG/usr/man/man6 $PKG/usr/share/applications

install -o root -g games -m 2755 -s tint $PKG/usr/games/tint

# 20241008 bkw: man page based on upstream's, but expanded.
sed "s,@VERSION@,$VERSION," < $CWD/tint.6 | gzip -9c > $PKG/usr/man/man6/tint.6.gz

sed '/^Exec=/s,=\(.*\),="\1",' \
    < debian/tint.desktop \
    > $PKG/usr/share/applications/tint.desktop

for px in 22 32 48 64 128; do
  sz=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$sz/apps
  mkdir -p $dir
  convert -resize $sz debian/tint.png $dir/tint.png
done
mkdir -p $PKG/usr/share/pixmaps/
ln -s ../icons/hicolor/48x48/apps/tint.png $PKG/usr/share/pixmaps/tint.png

mkdir -p $PKG/var/games

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CREDITS NOTES debian/{changelog,copyright} $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/doinst.sh > $PKG/install/doinst.sh

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