#!/bin/bash

# Slackware build script for noteye

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

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

# 20211115 bkw:
# - update for v8.4.
# - compile with g++ again (needs -std=gnu++98).
# - new-style icons.
# - s/lua/luajit/ in REQUIRES.
# - get rid of shared high score file in /var. it's now per-user
#   in ~/.config/noteye.
# - don't install binary setgid games.
# - add hydraslayer info to slack-desc.

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

PRGNAM=noteye
VERSION=${VERSION:-8.4}
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}

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

set -e

SRCVER=${VERSION/./}

# don't bother to extract these files from the archive, don't need them.
EXCLUDES="-x '*.dll' '*.exe' '*.bat' '*.mgw'"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
eval unzip $CWD/$PRGNAM-$SRCVER.zip $EXCLUDES
cd $PRGNAM
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# these don't need to be executable.
chmod 0644 common/*.noe games/*.noe

# On SBo we have rogue, which installs to /usr/games/rogue, and we also
# have yamagi-quake2 which installs a /usr/bin/rogue. Patch hardcodes
# /usr/games/rogue so selecting rogue from the menu doesn't start up a
# game of Quake 2, and changes the Ubuntu-specific apt-get instructions
# to Slack-specific sbopkg.
patch -p1 < $CWD/sbo.diff

# fix underlinking.
sed -i '/-lGLU *\$(TGT) *$/s,$, -lSDL2,' src/Makefile

# release tarball for 8.4 is missing this header (so use a copy
# from upstream's github)
xzcat $CWD/mtrand.h.xz > hydra/mtrand.h

make -C src CFLAGS="$SLKCFLAGS -std=gnu++98 -fpermissive"

# The 'make install' target is bad and wrong. Don't use.
PKGLIB=$PKG/usr/lib$LIBDIRSUFFIX
PKGSHARE=$PKG/usr/share/$PRGNAM
SOVER=$( echo $VERSION | cut -d. -f1 )

mkdir -p $PKG/usr/games $PKGLIB/$PRGNAM $PKGSHARE

# shared lib needs to be in /usr/lib(64), *not* /usr/share.
# also put the game binary in a private dir instead of /usr/share.
install -s -m0755 $PRGNAM $PKGLIB/$PRGNAM/
install -s -m0755 lib$PRGNAM.so $PKGLIB/lib$PRGNAM.so.$VERSION
ln -s lib$PRGNAM.so.$VERSION $PKGLIB/lib$PRGNAM.so.$SOVER
ln -s lib$PRGNAM.so.$VERSION $PKGLIB/lib$PRGNAM.so

# scripts in /usr/share are OK
install -m0755 src/calleri.sh $PKGSHARE/caller.sh
install -m0755 src/rogue.sh $PKGSHARE/rogue

# we've already set the permissions to 0644 for the files here, not
# 744 as the install target does.
cp -r gfx sound common games $PKGSHARE

# install the headers, in case someone wants to package up a game that
# uses this.
mkdir -p $PKG/usr/include
install -m0644 src/$PRGNAM.h src/$PRGNAM-curses.h $PKG/usr/include

# wrapper scripts
install -m0755 src/hydraslayer.sh $PKG/usr/games/hydraslayer
install -m0755 src/$PRGNAM.sh $PKG/usr/games/$PRGNAM

# have the wrapper scripts look in the right place for the game binary
sed -i \
  "s,^\$NOTEYEDIR/,/usr/lib$LIBDIRSUFFIX/$PRGNAM/," \
  $PKG/usr/games/*

# this results in better-looking noteye icons than if we extracted
# noteye.ico, which is only 48x36. hydra.ico is 48x48 and there's
# no larger version of the image in the source (it's composed of
# even smaller pieces, from the hydraslayer graphics).
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 -background none \
          -extent 400x400 \
          -gravity center \
          -resize $size \
          gfx/noteye-logo.png \
          $dir/$PRGNAM.png
  [ "$px" -le "48" ] && \
    convert 'hydra/hydra.ico[2]' -resize $px $dir/hydraslayer.png
done

# icons extracted from the .ico files in the source, with icotool.
PIXMAPS=$PKG/usr/share/pixmaps
mkdir -p $PIXMAPS
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PIXMAPS/$PRGNAM.png
ln -s ../icons/hicolor/48x48/apps/hydraslayer.png $PIXMAPS/hydraslayer.png

# .desktop files written for this build.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
cat $CWD/hydraslayer.desktop > $PKG/usr/share/applications/hydraslayer.desktop

# man pages written for this build.
mkdir -p $PKG/usr/man/man6
gzip -9c $CWD/$PRGNAM.6 > $PKG/usr/man/man6/$PRGNAM.6.gz
gzip -9c $CWD/hydraslayer.6 > $PKG/usr/man/man6/hydraslayer.6.gz

# We don't need licenses/* as we're linking with system-wide libs, which
# have already installed their own licenses in /usr/doc.
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE README.md sample $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$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
