#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://pmplib.sourceforge.net/
# source           : http://kent.dl.sourceforge.net/sourceforge/pmplib/pmplib-0.14.tar.gz

NAME="pmplib"
SOFTNAME="easypmp"
CWD=`pwd`
PKG="$CWD/$NAME-install"
VERSION="0.14"
ARCH="x86_64"
#CPU="i686"
BUILD="1gv"

mkdir -p $PKG

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

# Put Docs in /usr/doc/packagename-version
mkdir -p $PKG/usr/doc/$SOFTNAME-$VERSION
mv $PKG/usr/share/doc/pmplib/* $PKG/usr/doc/$SOFTNAME-$VERSION
rm -rf $PKG/usr/share

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

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$SOFTNAME: easypmp (pmplib) is a portable music player database manager
$SOFTNAME:
$SOFTNAME: easypmp is a command line utility used to create and maintain the
$SOFTNAME: music database and playlists on a variety of portable media players
$SOFTNAME: including the iRiver H100 and H300 series, the iRiver H10, U10 and
$SOFTNAME: E10, the Samsung YH-820, YH-920 and YH-925, the MSI MEGA PLAYER 540
$SOFTNAME: and the Philips HDD6320.
$SOFTNAME:
$SOFTNAME:
$SOFTNAME:
END

# 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/$SOFTNAME-$VERSION-$ARCH-$BUILD.txz
cd $CWD/
md5sum $SOFTNAME-$VERSION-$ARCH-$BUILD.txz > $SOFTNAME-$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