#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com, gapan@zenwalk forums)
# project web page : http://openmortal.sourceforge.net/
# source           : http://prdownloads.sourceforge.net/openmortal/openmortal-0.7.tar.bz2

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

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
BUILD="48.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: openmortal - a parody of Mortal Kombat
$NAME:
$NAME: openmortal is a parody of the popular coin-up game, Mortal Kombat.
$NAME: It is currently playable (maybe even enjoyable), although it is
$NAME: still under development. There are currently 24 playable
$NAME: characters, and several more in the making!
$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

# Check to see if an icon is present. If it's not, download it
if [ -f $NAME.png ]; then
	echo "Icon file already here, no need to download";
else
 	echo "Downloading icon file"
	wget -c http://users.zenwalk.org/user-accounts/gapan/$NAME/$NAME.png
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=OpenMortal
Exec=openmortal
Terminal=false
Type=Application
Categories=Game;ActionGame;
Icon=openmortal
EOF

#
# building-computer dependent: the number of parallel 'jobs' of 'make'
#
# 'heavy' computer: 6 build-jobs at once
#
#numjobs='-j6'
#
# 'light' computer: 2 build-jobs at once
#
numjobs='-j2'

# Configure and make
cd $NAME-$VERSION
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="-O2 -march=$ARCH -mtune=$CPU"
./configure $CONFIGOPTIONS
# Patch so it can actually compile
sed -i -e 's/-Wdeclaration-after-statement//' data/fonts/Makefile
sed -i -e 's/-Wdeclaration-after-statement//' data/gfx/Makefile
sed -i -e 's/-Wdeclaration-after-statement//' data/script/Makefile
sed -i -e 's/-Wdeclaration-after-statement//' data/Makefile
sed -i -e 's/-Wdeclaration-after-statement//' src/Makefile
sed -i -e 's/CChallengeMenu::CChallengeMenu/CChallengeMenu/' src/OnlineChat.cpp
# Now do make
make $numjobs

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

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

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