#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://xmoto.tuxfamily.org
#
# if configure complains about not being able to link to sqlite, use
# sqlite 3.4.2 to build.

# Package name
NAME="xmoto"
# Package version
VERSION="0.5.2"
# ./configure options
CONFIGOPTIONS="--prefix=/usr --mandir=/usr/man"
# Files that should be placed in /usr/doc/package-version/
DOCS="ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README TODO"

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

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: xmoto - a 2D motocross game
$NAME:
$NAME: X-Moto is a challenging 2D motocross platform game, where physics
$NAME: plays an all important role in the gameplay. You need to control
$NAME: your bike to its limits, if you want to have a chance to finish the
$NAME: most difficult challenges.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xf $NAME-$VERSION-src.tar.gz

# Create the desktop entry
mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/$NAME.desktop <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=X-Moto
Comment=A challenging 2D motocross platform game
Exec=xmoto
Terminal=false
Type=Application
Categories=Game;SportsGame;
Icon=xmoto
EOF

if [ $ARCH = "x86_64" ]; then
	export CFLAGS="-O2 -fPIC"
	export CXXFLAGS=$CFLAGS
	export LIBDIRSUFFIX="64"
else
	export CFLAGS="-O2 -march=$ARCH -mtune=i686"
	export CXXFLAGS=$CFLAGS
	export LIBDIRSUFFIX=""
fi

# Configure and make
cd $NAME-$VERSION
./configure --prefix=/usr \
--libdir=/usr/lib${LIBDIRSUFFIX} \
--mandir=/usr/man || exit 1
make || exit 1

# Install on temporary directory
make install DESTDIR=$PKG

# Copy icon to the right place
mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps
cp -a $CWD/$NAME.png $PKG/usr/share/icons/hicolor/48x48/apps

# 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 {} \;

# 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

# Calculate md5sum
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
