#!/bin/bash

# Slackware build script for pysolfc

# Originally written by Matthew "mfillpot" Fillpot.

# Now maintained by B. Watson <urchlay@slackware.uk>

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

# 20211228 bkw: update for v2.14.1.
# - relicensed as WTFPL with permission from Matthew.
# - we now use python3 and -current's python-pillow, no need for
#   pillow6 patch, six, etc.
# - document optional dep pygame in README.

# 20181206 bkw:
# - Add missing 'six' dependency. Thanks to Carsten Boysen Jensen for
#   the bug report. BUILD=2

# 20181013 bkw:
# - Take over maintenance.
# - Update for v2.4.0. Incompatible changes, can't build the old
#   version with VERSION=2.0, sorry.
# - Upstream removed all the cardsets from the source tarball, moved
#   them to a separate "minimal" cardset tarball, so add it to DOWNLOAD
#   and script stuff to handle it.
# - Get rid of the option to include the complete cardsets tarball. Moved
#   to a separate pysylfc-extra-cardsets build.
# - Install the correct man page for the FC edition, not the old pre-fork
#   pysol one.
# - Include all_games.html in the docdir (with fixed paths for the links).
# - Update README and slack-desc.

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

PRGNAM=pysolfc
VERSION=${VERSION:-2.14.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

CARDSETVER=${CARDSETVER:-2.0}
CARDSETS=$SRCNAM-Cardsets--Minimal-$CARDSETVER

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

fixperms() {
  chown -R root:root $1
  find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
          \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+
}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.?z
cd $SRCNAM-$VERSION
fixperms .

# do not use 'make install', it has no way to pass --root to setup.py.
# "mo" creates the UI translations. "rules" and "all_games_html" make
# the docs. setup.py will install the docs & translations if they
# exist.
make mo
make rules
make all_games_html
python3 setup.py install --root $PKG

mkdir -p $PKG/usr/man/man6
gzip -9c < docs/$PRGNAM.6 > $PKG/usr/man/man6/$PRGNAM.6.gz

# Executable is called pysol.py, there damn well better be a man
# page with the same name.
ln -s $PRGNAM.6.gz $PKG/usr/man/man6/pysol.py.6

# The minimal cardsets are now required, since the source tarball
# has none. Extract directly to $PKG instead of extracting and copying.
tar xvf $CWD/$CARDSETS.tar.xz -C $PKG/usr/share/$SRCNAM/ --strip-components=1
fixperms $PKG/usr/share/$SRCNAM

# old-style icon, in case something needs it.
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/pysol.png $PKG/usr/share/pixmaps/pysol.png

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a contrib/*.asciidoc *.md NEWS.* COPYING docs/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

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