#!/bin/bash

# Slackware build script for maelstrom

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

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

# 20260728 bkw: upstream's alive again, ported to SDL3.
# - updated for v4.0.2.
# - upstream installs everything in the same dir, so use /opt/$PRGNAM
#   because I don't feel like spending hours fighting with it...
# - patch it to start windowed by default. SDL3 windows are arbitrarily
#   resizable so you can just maximize the game. Also fullscreen doesn't
#   play well with multi-head. The --fullscreen option still works, it
#   just isn't the default now.
# - get rid of maelstrom_addon_package.pl, since mods are no longer done
#   the same way. will add a maelstrom-mods build to replace it.
# - update man page.

# 20211020 bkw: upstream's first update since 2002! ported to SDL2!
# - update for v3.0.7.
# - new-style icons.
# - update URLs in perl script and man page.

# 20180627 bkw: fix compile with gcc 8 (for -current).

# 20160730 bkw:
# - Fix compile with gcc 5 (for Slack 14.2). Also fix some
#   compiler warnings while I'm at it.
# - Remove warnings about multiplayer not working on 64-bit, it
#   works now.

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

PRGNAM=maelstrom
VERSION=${VERSION:-4.0.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=Maelstrom

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"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
SLKCFLAGS+=" -DNDEBUG"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$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 {} + \)

# 20260730 bkw: starting in windowed mode by default makes more sense on
# modern desktop platforms.
patch -p1 < $CWD/no_fullscreen_default.diff

mkdir -p build
cd build
  cmake \
    -DUSE_VENDORED_SDL=OFF \
    -DCMAKE_C_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/opt/$PRGNAM \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DMAN_INSTALL_DIR=/usr/man \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
cd ..

mkdir -p $PKG/usr/games
ln -s ../../opt/$PRGNAM/Maelstrom $PKG/usr/games/Maelstrom
ln -s ../../opt/$PRGNAM/Maelstrom $PKG/usr/games/maelstrom

DOC=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG$DOC
mkdir -p $PKG/usr/doc
mv $PKG/opt/$PRGNAM/Docs $PKGDOC
ln -s ../../$DOC $PKG/opt/$PRGNAM/Docs

mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# upstream's icon is 48x48, resize
for i in 16 32 48 64; do
  px=$( basename $i | cut -d. -f1 )
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  ln -s ../../../../../../opt/maelstrom/Data/Icons/icon-$i.png $dir/$PRGNAM.png
done

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

mkdir -p $PKG/usr/man/man6
gzip -9c $CWD/$SRCNAM.6 > $PKG/usr/man/man6/$SRCNAM.6.gz
( cd $PKG/usr/man/man6
  ln -s $SRCNAM.6.gz $PRGNAM.6.gz
  ln -s $SRCNAM.6.gz $SRCNAM-netd.6.gz )

cp -a CREDITS $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
