#!/bin/sh

# Slackware build script for opendune

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

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

PRGNAM=opendune
VERSION=${VERSION:-0.8}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

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

CWD=$(pwd)
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

SRCNAM=OpenDUNE

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

# Fix compile issue with /usr/include/alsa/pcm.h (uses inline)
sed -i 's,-ansi,,' config.lib

# Install docs to Slack standard location
sed -i "s,share/doc/$PRGNAM,doc/$PRGNAM-$VERSION," config.lib

# Look in game dir for .ini file.
sed -i "/\"data\//s,data,/usr/share/games/$PRGNAM," src/inifile.c

# Allow building with ALSA instead of pulseaudio. There's no way to switch
# between ALSA and Pulse at runtime, and many users prefer to avoid Pulse.
if [ "${PULSE:-yes}" = "no" -o ! -x /usr/bin/pulseaudio ]; then
  PULSEOPT="--without-pulse"
fi

# The configure script autodetects SDL 2, but it doesn't check for
# SDL2_image (assumes it's there if SDL2 is). So if someone's installed
# SDL2 but not SDL2_image, the build will fail. Fix:
if [ "${SDL2:-yes}" = "no" ]; then
  SDLOPT="--without-sdl2"
  SDLVER=1
elif pkg-config --exists sdl2 && pkg-config --exists SDL2_image; then
  SDLOPT="--with-sdl2"
  SDLVER=2
else
  SDLOPT="--without-sdl2"
  SDLVER=1
fi

# N.B. non-standard (non-autoconf) configure options here!
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  $PULSEOPT \
  $SDLOPT \
  --prefix-dir=/usr \
  --install-dir=$PKG \

make
strip bin/$PRGNAM
make install

# Include shell script wrapper to start timidity if installed.
mkdir -p $PKG/usr/libexec/$PRGNAM
mv $PKG/usr/games/$PRGNAM $PKG/usr/libexec/$PRGNAM
install -m0755 -oroot -groot $CWD/$PRGNAM.sh $PKG/usr/games/$PRGNAM

# Include default .ini file with location of data files. data/
# is always appended to whatever datadir is set to.
INI=$PKG/usr/share/games/$PRGNAM/$PRGNAM.ini
cat bin/$PRGNAM.ini.sample > $INI
echo "datadir=/usr/share/games/$PRGNAM" >> $INI

# This dir is where the .PAK files should go.
mkdir -p $PKG/usr/share/games/$PRGNAM/data

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

# docs already installed, but include this:
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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