#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://www.talula.demon.co.uk/allegro/

# Package name
NAME="allegro"
# Package version
VERSION="4.2.2"
# Files that should be placed in /usr/doc/package-version/
DOCS="AUTHORS CHANGES THANKS readme.txt docs/html"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="x86_64"
#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: Allegro is a game programming library for C/C++
$NAME:
$NAME: Allegro is a game programming library for C/C++ developers
$NAME: distributed freely, supporting the following platforms: DOS, Unix
$NAME: (Linux, FreeBSD, Irix, Solaris, Darwin), Windows, QNX, BeOS and
$NAME: MacOS X. It provides many functions for graphics, sounds, player
$NAME: input (keyboard, mouse and joystick) and timers. It also provides
$NAME: fixed and floating point mathematical functions, 3d functions, file
$NAME: management functions, compressed datafile and a GUI.
$NAME:
$NAME:
END

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

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

patch -p1 < ../allegro-4.2.2-icpus.patch || exit 1

./configure --prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX} \
	--enable-svgalib=yes --enable-jackdigi=no \
	--enable-artsdigi=no --enable-esddigi=no
sed -i -e "s/-O2 -mtune=pentium/$CFLAGS/" makefile
make || exit 1

# Install on temporary directory
make install DESTDIR=$PKG || exit 1
make install-gzipped-man DESTDIR=$PKG
mv $PKG/usr/share/man $PKG/usr/

# 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

# 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