#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# Requirements: freealut, openal, jam, physfs, smpeg,
# http://www.positro.net/trigger/
# http://www.slacky.it

NAME=trigger
VERSION=0.5.2.1
DATA_VER=0.5.2
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=2bj

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

URL="http://puzzle.dl.sourceforge.net/sourceforge/trigger-rally/$NAME-$VERSION-src.tar.bz2 \
      http://puzzle.dl.sourceforge.net/sourceforge/trigger-rally/$NAME-$DATA_VER-data.tar.bz2"

for i in $URL
do
  package=$(basename $i)
  if [ ! -e "$CWD/$package" ]; then
    wget -c $i
  fi
done

cd $TMP
rm -rf $NAME-$VERSION-src
tar -xjvf $CWD/$NAME-$VERSION-src.tar.bz2 || exit 1
cd $NAME-$VERSION-src
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --datadir=/usr/share/$NAME \
  --enable-optimize \
  --program-prefix= \
  --program-suffix= \
  --disable-debug \
  --build=$CHOST-slackware-linux
jam || exit 1

mkdir -p $PKG/usr/{bin,doc/$NAME-$VERSION,share/{applications,pixmaps}}
cp -a trigger $PKG/usr/bin
cp -a doc/*.txt $PKG/usr/doc/$NAME-$VERSION
cp -a $CWD/trigger.png $PKG/usr/share/pixmaps
strip $PKG/usr/bin/*

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Trigger
GenericName=Rally Race
Comment=3D rally racing car game
Exec=trigger
Icon=/usr/share/pixmaps/trigger.png
Terminal=0
Type=Application
Categories=Application;Game;
EOF

( cd $PKG/usr/share
tar -xjvf $CWD/$NAME-$DATA_VER-data.tar.bz2 || exit 1
mv -f $NAME-$DATA_VER-data $NAME
cd $NAME
find . -type d | xargs -r chmod 755 
find . -type f | xargs -r chmod 644
mv -f events/triggercup/{bigjump,monkeybusiness,oldmacdonald,warmup} maps/
)

cd $PKG
mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
