#!/bin/bash

# Slackware build script for zork

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

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

# VERSION is the last modification date of this script. The various zcode
# files of course have their own version information, but at this late date
# they aren't going to ever change.

# 20211011 bkw:
# - move executables to /usr/games.
# - new-style icons.
# - man page.
# - reflow readme-*.txt (with fmt -s).

# 20140219 bkw: fix .desktop files so they validate.

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

PRGNAM=zork
VERSION=${VERSION:-20211011}
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
mkdir $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION

# zork 1/2/3 are z-code version 3 (z3), uu is z5.
mkdir -p $PKG/usr/share/zcode $PKGDOC/invisiclues
for game in 1 2 3; do
  rm -rf *
  unzip -a -LL $CWD/zork$game.zip
  cat data/zork$game.dat > $PKG/usr/share/zcode/zork$game.z3
  fmt -s readme.txt > $PKGDOC/readme-zork$game.txt
done

rm -rf *
unzip -a -LL $CWD/ztuu.zip
cat ztuu.z5 > $PKG/usr/share/zcode/ztuu.z5
fmt -s readme.txt > $PKGDOC/readme-ztuu.txt
cat invisiclues.txt > $PKGDOC/invisiclues/ztuu.txt
cat invisiclues.html > $PKGDOC/invisiclues/ztuu.html

# ztuu has invisiclues in the zip file, add the clues for 1/2/3 also
cat $CWD/ZorkI.inv > $PKGDOC/invisiclues/zork1.txt
cat $CWD/ZorkII.inv > $PKGDOC/invisiclues/zork2.txt
cat $CWD/ZorkIII.inv > $PKGDOC/invisiclues/zork3.txt

cat $CWD/README.interpreters > $PKGDOC/README.interpreters
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Script wrapper and .desktop files written for this SlackBuild
mkdir -p \
  $PKG/usr/share/games/zork \
  $PKG/usr/games \
  $PKG/usr/man/man6 \
  $PKG/usr/share/applications \
  $PKG/usr/share/pixmaps

install -m0755 $CWD/zorkwrapper.sh $PKG/usr/share/games/zork/zorkwrapper.sh
gzip -9c < $CWD/zorkwrapper.sh.6 > $PKG/usr/man/man6/zorkwrapper.sh.6.gz

for game in zork1 zork2 zork3 ztuu; do
  ln -s ../share/games/zork/zorkwrapper.sh $PKG/usr/games/$game
  ln -s zorkwrapper.sh.6.gz $PKG/usr/man/man6/$game.6.gz
  cat $CWD/desktop/$game.desktop > $PKG/usr/share/applications/$game.desktop
done

# zork1 => zork, so we have an exe and a man page matching the package name.
ln -s zorkwrapper.sh.6.gz $PKG/usr/man/man6/$PRGNAM.6.gz
ln -s ../share/games/zork/zorkwrapper.sh $PKG/usr/games/$PRGNAM

# Icons created with mkicons.sh, q.v.
for i in $CWD/icons/*.png; do
  px=$( basename $i | cut -d. -f1 )
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cat $i > $dir/$PRGNAM.png
done

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

mkdir -p $PKG/install
sed "s,@VERSION@,$VERSION," $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
