#!/bin/bash
# Slackware build script for rottexpr
# Based on games/rott/rott.SlackBuild by B. Watson (urchlay@slackware.uk)
# Modified for rottexpr by Brent Spillner (s p i l l n e r @ a c m . o r g)
# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

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

PRGNAM=rottexpr
MAKEFILE_TARGET=rott
VERSION=${VERSION:-0.01}
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}

# LIBDIRSUFFIX not needed
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.gz
cd $PRGNAM-$VERSION
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Adjust user configuration dir to avoid conflicts with the other rott engine
sed -i "s/\.rott\//\.rottexpr\//g" src/dosutil.c

# The shareware/demo binary
make -j1 -C src clean all \
  SHAREWARE=1 SUPERROTT=0 \
  EXTRACFLAGS="$SLKCFLAGS -DDATADIR=\\\"/usr/share/games/$PRGNAM/\\\""
mkdir -p $PKG/usr/games
install -s -m0755 src/$MAKEFILE_TARGET $PKG/usr/games/$PRGNAM-demo

# The registered binary
make -j1 -C src clean all \
  SHAREWARE=0 SUPERROTT=0 \
  EXTRACFLAGS="$SLKCFLAGS -DDATADIR=\\\"/usr/share/games/$PRGNAM/\\\""
install -s -m0755 src/$MAKEFILE_TARGET $PKG/usr/games/$PRGNAM-reg

# The retail/cdrom binary
make -j1 -C src clean all \
  SHAREWARE=0 SUPERROTT=1 \
  EXTRACFLAGS="$SLKCFLAGS -DDATADIR=\\\"/usr/share/games/$PRGNAM/\\\""
install -s -m0755 src/$MAKEFILE_TARGET $PKG/usr/games/$PRGNAM-cdrom

# Not going to bother with the "Site License" binary: doubt anyone
# will have any use for it, since multiplayer isn't supported. If you need
# it, build with SHAREWARE=0 SUPERROTT=0 SITELICENSE=1

# Man page is modified from the original version.
MANDIR=$PKG/usr/man/man6
mkdir -p $MANDIR
gzip -9c $CWD/$PRGNAM.6 > $MANDIR/$PRGNAM.6.gz
ln -s $PRGNAM.6.gz $MANDIR/$PRGNAM-demo.6.gz
ln -s $PRGNAM.6.gz $MANDIR/$PRGNAM-reg.6.gz
ln -s $PRGNAM.6.gz $MANDIR/$PRGNAM-cdrom.6.gz

# Shareware game data. Used to be optional, always included now.
mkdir -p $PKG/usr/share/games/$PRGNAM
if [ -e $CWD/1rott13.zip ]; then
  unzip $CWD/1rott13.zip ROTTSW13.SHR
  unzip ROTTSW13.SHR '*.WAD' '*.RTS' '*.RTL' '*.RTC'
  cp *.WAD *.RTS *.RTL *.RTC $PKG/usr/share/games/$PRGNAM
  chmod 644 $PKG/usr/share/games/$PRGNAM/*
fi

# Wrapper script
install -m0755 -oroot -groot $CWD/$PRGNAM.sh $PKG/usr/games/$PRGNAM

mkdir -p $PKG/usr/share/applications $PKG/usr/share/pixmaps
cp $CWD/$PRGNAM.desktop $PKG/usr/share/applications/$PRGNAM.desktop

# Icon made for the original games/rott SlackBuild.
for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -resize $size! $CWD/$PRGNAM.png $dir/$PRGNAM.png
done

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp COPYING LICENSE.DOC *.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README_gamedata.txt > $PKG/usr/doc/$PRGNAM-$VERSION/README_gamedata.txt

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
