#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://www.nongnu.org/enigma/
# source           : http://download.berlios.de/enigma-game/enigma-1.01.tar.gz

# Package name
NAME="enigma"
# Package version
VERSION="r1830"

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: Enigma (a puzzle game inspired by Oxyd on the Atari ST)
$NAME:
$NAME: Enigma is a puzzle game inspired by Oxyd on the Atari ST and 
$NAME: Rock'n'Roll on the Amiga. The object of the game is to find
$NAME: uncover pairs of identically colored Oxyd stones. Simple? Yes.
$NAME: Easy? Certainly not! Hidden traps, vast mazes, laser beams, and,
$NAME: most of all, countless hairy puzzles usually block your direct
$NAME: way to the Oxyd stones...
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xvzf $NAME-$VERSION.tar.gz

# Configure and make
cd $NAME-$VERSION

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

./configure \
--prefix=/usr \
--libdir=/usr/lib${LIBDIRSUFFIX} \
--mandir=/usr/man || exit 1
make || exit 1

# Install on temporary directory
make install DESTDIR=$PKG || exit 1

# enet is a separate package, why put it in here too? /usr/lib and
# /usr/include directories only have enet files
rm -rf $PKG/usr/include
rm -rf $PKG/usr/lib

# 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
mv $PKG/usr/share/doc/enigma/* $PKG/usr/doc/$NAME-$VERSION
rm -rf $PKG/usr/share/doc

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

# Remove the pixmaps dir, the icon is already in the right place anyway
rm -rf $PKG/usr/share/pixmaps

# 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