#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://sourceforge.net/projects/toohardforyou
# source           : http://downloads.sourceforge.net/toohardforyou/2H4U-1.3_SOURCES.tar.gz

# Package name
NAME="2H4U"
# Package version
VERSION="1.3"
# Files that should be placed in /usr/doc/package-version/
DOCS="COPYING.txt LISEZ-MOI.txt README.txt help aide"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="x86_64"
BUILD="1gv"

rm -rf $PKG
mkdir -p $PKG

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

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: 2H4U - a mix between a Tetris-like game and a wall breaker
$NAME:
$NAME: 2H4U, which stands for Too Hard For You, is an open source game,
$NAME: and a mix between a Tetris-like game and a wall breaker. It
$NAME: requires good reflexes, coordination, and ambidexters should have
$NAME: some advantages. Will 2H4U be too hard for you?
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xvzf 2H4U-1.3_SOURCES.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=2H4U
Exec=2H4U
Terminal=false
Type=Application
Categories=Game;LogicGame;
Icon=2H4U
EOF

# Patch the source for using a vorbis music file (sdl_mixer errors out with mp3)
cd $NAME/sources
sed -i -e 's,Musique.mp3,Musique.ogg,' son.cpp

# make
cd $CWD/$NAME/scripts
# Force the proper flags
sed -i -e "s/g++/g++ $CXXFLAGS/" Makefile
make

# Install on temporary directory
mkdir -p $PKG/usr/share/$NAME
cd $CWD/$NAME
cp -a 2H4U data $PKG/usr/share/$NAME

# Create launcher
mkdir -p $PKG/usr/bin
cat > $PKG/usr/bin/2H4U << EOF
#!/bin/sh
cd /usr/share/2H4U
./2H4U
EOF
chmod a+x $PKG/usr/bin/2H4U

# Replace mp3 music file with a vorbis one
rm $PKG/usr/share/$NAME/data/sons/Musique.mp3
cp -a $CWD/Musique.ogg $PKG/usr/share/$NAME/data/sons

# Copy icons 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
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps
cp -a $CWD/$NAME.svg $PKG/usr/share/icons/hicolor/scalable/apps
mkdir -p $PKG/usr/share/icons/hicolor/32x32/apps
cd $PKG/usr/share/icons/hicolor/32x32/apps
ln -sf ../../../../$NAME/data/images/icone.png 2H4U.png

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

# Create a hiscores file
cat > $PKG/usr/share/$NAME/data/HighScores.dat <<EOF
-968 -763 610 847 172
EOF

# Create a config file
cat > $PKG/usr/share/$NAME/data/config.dat <<EOF
1 1 0 1 1
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 {} \;

# But we need the hiscores and the config file writable by everyone
chmod a+w $PKG/usr/share/$NAME/data/HighScores.dat
chmod a+w $PKG/usr/share/$NAME/data/config.dat

# 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
rm -rf $PKG