#!/bin/bash

# Slackware build script for solarwolf

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

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

# 20260806 bkw:
# - update for v1.6.0.
# - new homepage/download (pygame.org is dead).
# - use python3 and python3-pygame.

# 20230107 bkw: BUILD=3, ARCH=noarch.

# 20211228 bkw: BUILD=2.
# SDL2_mixer on -current utterly lacks support for tracker music
# (.mod, .it, .xm, etc). This game's music is in .xm format... so
# unless/until Pat adds support in SDL2_mixer, this script works
# around it by using timidity to convert the .xm music to .ogg, which
# SDL2_mixer can handle.
# Add TiMidity++ as a dep, to make this work. It's really only
# build-time, not runtime.
# Also, new-style icon.

# TODO: scaling in fullscreen mode. our pygame is built with SDL2,
# this should be possible.

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

PRGNAM=solarwolf
VERSION=${VERSION:-1.6.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}
ARCH=noarch

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 . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)

# 20260806 bkw: Our SDL2 can't play .xm files, so convert them to
# something we *can* play. First, patch the code before installing.
# I was hoping that, since we're using a newer pygame (python3-pygame
# 2.5.2), it had some other way to play .xm files... but no, this is
# still necessary.
# 20211228 bkw: make the game play the converted .ogg music.
sed -i 's,\.xm,.ogg,g' solarwolf/game*.py

# 20260806 bkw: we could actually use python2 for this, but let's not.
python3 setup.py install --root=$PKG

# 20260806 bkw: it's a game, so:
mv $PKG/usr/bin $PKG/usr/games

mkdir -p \
  $PKG/usr/share/icons/hicolor/64x64/apps \
  $PKG/usr/share/pixmaps \
  $PKG/usr/share/applications \
  $PKG/usr/man/man6

# 20211228 bkw: convert the music to .ogg. Use separate oggenc instead
# of timidity's -Ov, so we can set the bitrate to 64k (to match the
# one .ogg file shipped with the source). This bloats the package by
# 30%, but it still comes out to less than 4MB total, so no problem.
cd $PKG/usr/lib*/python3*/site-packages/solarwolf/data/music
for i in *.xm; do
  out="$( basename $i .xm ).ogg"
  timidity -Ow1sl -id -o- $i | oggenc -Q -b 64 -o $out -
  rm -f $i
done
cd -

# 20260806 bkw: man page from previous version. command line options
# haven't changed so it's fine to use.
gzip -9c < $CWD/$PRGNAM.6 > $PKG/usr/man/man6/$PRGNAM.6.gz

# 20260806 bkw: upstream dropped the .desktop file...
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# 20260806 bkw: ...and the icon.
cat $CWD/$PRGNAM.png > $PKG/usr/share/icons/hicolor/64x64/apps/$PRGNAM.png
ln -s ../icons/hicolor/64x64/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a *.txt *.rst $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
