#!/bin/sh

# Slackware build script for noteye

# Written by B. Watson (yalhcru@gmail.com)

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

PRGNAM=noteye
VERSION=${VERSION:-7.6}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -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 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -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

# apparently Ubuntu has liblua-5.1.so, we just have liblua.so (same version).
sed -i 's,-llua5.1,-llua,g' src/Makefile

# store the writable high score file in /var, not /usr/share.
sed -i 's,"hydrascores.sav","/var/games/hydrascores.sav",' hydra/hydra.cpp

make -C src CFLAGS="$SLKCFLAGS"

# 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,
# and make it setgid games so the high score file can be updated.
install -s -m2755 -oroot -ggames $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/*

# don't install a highscore file here, see doinst.sh.

# icons extracted from the .ico files in the source, with icotool.
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/$PRGNAM.png > $PKG/usr/share/pixmaps/$PRGNAM.png
cat $CWD/hydraslayer.png > $PKG/usr/share/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 COPYING README.txt 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:-tgz}