#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://freedroid.sourceforge.net/
# http://www.slacky.eu
# Packager cenni ( nullesimo at libero.it)

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/pkg/freedroidrpg
NAME=freedroidrpg
VERSION=0.11
ARCH=${ARCH:-i486}
BUILD=1cen

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mtune=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "athlon-mp" ]; then
  SLKCFLAGS="-O2 -march=athlon-mp -pipe -mfpmath=sse"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

SOURCE=http://downloads.sourceforge.net/freedroid/$NAME-$VERSION.tar.bz2

if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
wget -c $SOURCE
fi

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

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

cd $TMP

tar xvjf $CWD/$NAME-$VERSION.tar.bz2

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

CFLAGS="$SLKCFLAGS"
./configure --prefix=/usr \
--mandir=/usr/man \
--program-prefix= \
--program-suffix= \
--build=i486-slackware-linux
make
make install DESTDIR=$PKG

find $PKG/usr/man -type f -exec gzip -9 {} \;

chmod 755 $PKG/usr/bin/*

mkdir -p $PKG/install
mkdir -p $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/share/pixmaps
mkdir -p $PKG/usr/share/applications

cp -a AUTHORS ChangeLog COPYING INSTALL README NEWS $PKG/usr/doc/$NAME-$VERSION

cat $TMP/freedroidrpg-$VERSION/graphics/skill_icons/TeleportHomeSpell.png > $PKG/usr/share/pixmaps/freedroidrpg.png

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Type=Application
Categories=Game;StrategyGame;
Comment=isometric 3D RPG inspered from Diablo
Comment[it]=RPG con grafica isometrica ispirato da Diablo
Encoding=UTF-8
Exec=freedroidRPG
GenericName=RPG 3D inspired from Diablo
GenericName[it]=RPG ispirato da Diablo
Icon=/usr/share/pixmaps/freedroidrpg.png
Name=freedroidRPG
Name[it]=freedroidRPG
EOF

cd $PKG
find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

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 -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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