#!/bin/bash

# Slackware build script for domino-chain

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

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

# This replaces the old pushover build. Upstream changed the name, possibly
# to avoid a lawsuit.

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

PRGNAM=domino-chain
TARNAM=$PRGNAM.gitlab.io
VERSION=${VERSION:-1.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" ]; 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 $TARNAM-$VERSION
tar xvf $CWD/$TARNAM-$VERSION.tar.gz
cd $TARNAM-$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 {} + \)

# 20251215 bkw: this should help on -current.
patch -p1 < $CWD/gcc14.diff

# 20251215 bkw: needed on the author's system, or the UI flashes like mad
# and is basically unusable.
[ "${OPENGL:-yes}" = "no" ] && patch -p1 < $CWD/software_render.diff

# 20251215 bkw: this is *completely stupid*. The old pushover 0.0.3
# source included the font it uses. The 1.1 source doesn't, and its
# Makefile expects to copy it from /usr/share/fonts... I had to
# host the font myself. Even more stupid: the Makefile doesn't
# *install* the font. It just has to be present or the build fails.
mkdir -p data/fonts $PKG/usr/share/$PRGNAM/fonts
touch data/fonts/FreeSans.ttf
cat $CWD/FreeSans.ttf > $PKG/usr/share/$PRGNAM/fonts/FreeSans.ttf

# 20251215 bkw: add the game data dir to the hardcoded font dir list.
sed -i 's,"data/fonts/","/usr/share/domino-chain/fonts/",' \
  src/domino-chain/screen.cpp

sed -i "s,-O2,$SLKCFLAGS," Makefile
make PKG_LUA=lua
make install BINDIR=/usr/games DESTDIR=$PKG
strip $PKG/usr/games/$PRGNAM

# 20251215 bkw: no good way to override these paths in the Makefile.
mv $PKG/usr/share/man $PKG/usr/man
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKG/usr/doc
mv $PKG/usr/share/doc/$PRGNAM $PKGDOC
rm -rf $PKG/usr/share/doc

# 20251215 bkw: abs path in .desktop
sed -i '/^Exec/s,=,=/usr/games/,' $PKG/usr/share/applications/*.desktop

mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKGDOC
cp -a AUTHORS LICENSE README* $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
cat $CWD/doinst.sh > $PKG/install/douninst.sh

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