#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com, gapan@zenwalk forums)
# project web page : http://ri-li.sourceforge.net

# Package name
NAME="Ri-li"
# Package version
VERSION="2.0.1"
# Source code URL
SOURCE="http://prdownloads.sourceforge.net/ri-li/Ri-li-$VERSION.tar.bz2"
# ./configure options
CONFIGOPTIONS="--prefix=/usr"
# Files that should be placed in /usr/doc/package-version/
DOCS="AUTHORS COPYING COPYING.Music ChangeLog INSTALL NEWS README"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="60.1"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: Ri-li is an arcade game with toy wood train engine
$NAME:
$NAME: Ri-li is an arcade game licensed under the GPL. You drive a toy
$NAME: wood engine in many levels and you must collect all the coaches to
$NAME: win. You can dowload it and play indefinitely
$NAME: 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Check to see if source is present. If it's not, download it
rm -rf $NAME-$VERSION
if [ -f $NAME-$VERSION.tar.bz2 ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c $SOURCE
fi

# Unpack source
tar xvjf $NAME-$VERSION.tar.bz2

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

# Configure and make
cd $NAME-$VERSION
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
./configure $CONFIGOPTIONS
make

# Install on temporary directory
make install DESTDIR=$PKG

# 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 $DOCS $PKG/usr/doc/$NAME-$VERSION

# Compress the man page(s)
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Symlink icon
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
cd $PKG/usr/share/icons/hicolor/48x48/apps
ln -sf ../../../../Ri-li/Ri-li-icon-48x48.png Ri-li.png
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps
cd $PKG/usr/share/icons/hicolor/32x32/apps
ln -sf ../../../../Ri-li/Ri-li-icon-32x32.png Ri-li.png
mkdir -p $PKG/usr/share/icons/hicolor/16x16/apps
cd $PKG/usr/share/icons/hicolor/16x16/apps
ln -sf ../../../../Ri-li/Ri-li-icon-16x16.png Ri-li.png

# Add the build-file in the package
mkdir -p $PKG/usr/src/$NAME-$VERSION
cp "$CWD/build-$NAME.sh" "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"
chmod a+x "$PKG/usr/src/$NAME-$VERSION/build-$NAME.sh"

# 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.tgz

# Calculate md5sum
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $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
