#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://sdlpango.sourceforge.net/
# source           : http://dfn.dl.sourceforge.net/sourceforge/sdlpango/SDL_Pango-0.1.2.tar.gz
# patch            : http://zarb.org/~gc/t/SDL_Pango-0.1.2-API-adds.patch

# Package name
NAME="SDL_Pango"
# Package version
VERSION="0.1.2"
# Files that should be placed in /usr/doc/package-version/
DOCS="AUTHORS COPYING ChangeLog INSTALL NEWS README docs/html"

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: SDL_Pango (connects the pango rendering engine to SDL)
$NAME:
$NAME: SDL_Pango is a wrapper around the Pango library and allows you to
$NAME: use TrueType fonts to render internationalized and tagged text in
$NAME: SDL applications.
$NAME: 
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

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

# Configure and make
cd $NAME-$VERSION
patch -p0 src/SDL_Pango.c ../SDL_Pango-0.1.2-API-adds.patch

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

sed -i "s/-O2/$CFLAGS/" configure
./configure --prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX} --disable-static
make

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