#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project homepage : http://www.bigorno.net/xrick/
# source           : http://www.bigorno.net/xrick/xrick-021212.tgz

NAME="xrick"
CWD=`pwd`
PKG="$CWD/$NAME-install"
VERSION="021212"
ARCH="i486"
CPU="i686"
BUILD="1gv"

rm -rf $PKG
mkdir -p $PKG

tar xvzf $NAME-$VERSION.tgz

# As it is the binary expects the data.zip file to be in the same
# directory, so we're changing the source code to specify where we want
# the data.zip file to be (/usr/share/xrick)
sed -i -e 's,data.zip,/usr/share/xrick/data.zip,' $NAME-$VERSION/src/xrick.c

cd $NAME-$VERSION
sed -i "s/-O2/-O2 -march=i486 -mtune=i686/" Makefile
make 

# Install on temporary directory
mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/man/man6
mkdir -p $PKG/usr/share/xrick
cp xrick $PKG/usr/bin/
cp xrick.6.gz $PKG/usr/man/man6/
cp data.zip $PKG/usr/share/xrick/

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

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a \
	README \
		$PKG/usr/doc/$NAME-$VERSION

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << EOF
$NAME: xrick is a clone of the platform game Rick Dangerous
$NAME:
$NAME: Way before Lara Croft, back in the 1980's and early 1990's,
$NAME: Rick Dangerous was the Indiana Jones of computer games, running
$NAME: away from rolling rocks, avoiding traps, from South America to a
$NAME: futuristic missile base via Egypt and the Schwarzendumpf castle.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
EOF

# Place the icon in the right place
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps/
cp $CWD/xrick.png $PKG/usr/share/icons/hicolor/48x48/apps/

# Create the desktop entry
mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/$NAME.desktop <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=xrick
Exec=xrick
Terminal=false
Type=Application
Categories=Game;ActionGame;
Icon=xrick
EOF

# Make sure ownerships and permissions are sane
cd $PKG
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

# Make the package
/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.txz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG