#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
#
# http://www.codelathe.com/mmex/
# Packager neongen ( neongen at libero.it)
# http://www.slacky.it

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

NAME=moneymanagerex
REVISION=349
VERSION=svn_$REVISION
ARCH=${ARCH:-i486}
BUILD=1ng
PKG=$TMP/package-$NAME

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 --march=i686 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

echo "+=============================================================================="
echo "| $NAME-$VERSION"
echo "+=============================================================================="


# if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
# echo "Downloading source"
# wget -c $SOURCE
# fi

cd $TMP

# echo "Unpacking $NAME..."
# tar -xjf $CWD/$NAME-$VERSION.tar.bz2

svn co --revision $REVISION https://moneymanagerex.svn.sourceforge.net/svnroot/moneymanagerex moneymanagerex
mkdir -p $NAME/trunk/mmex/buildgtk
cd $NAME/trunk/mmex/buildgtk

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;

chown -R root:root .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
../configure \
	--prefix=/usr \
	--mandir=/usr/man \
	--build=$ARCH-slackware-linux
make

mkdir -p $PKG/usr/bin
strip --strip-unneeded mmex
cp mmex $PKG/usr/bin/
cp $CWD/mmex.sh $PKG/usr/bin/

mkdir -p $PKG/usr/share
cp -ar ../runtime $PKG/usr/share/$NAME
rm $PKG/usr/share/$NAME/*dll $PKG/usr/share/$NAME/*manifest

mkdir -p $PKG/usr/share/applications
cp $CWD/mmex.desktop $PKG/usr/share/applications
 mkdir -p $PKG/usr/share/pixmaps
cp $CWD/mmex.png $PKG/usr/share/pixmaps

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a ../docs/* ../README.TXT ../INSTALL $PKG/usr/doc/$NAME-$VERSION
mv $PKG/usr/share/$NAME/*txt $PKG/usr/doc/$NAME-$VERSION

find $PKG -name .svn -print0 | xargs -0 /bin/rm -rf

chown root:root -R $PKG
# gzip -9 $PKG/usr/man/*/*
# chmod 644 $PKG/usr/doc/$NAME-$VERSION/*
# chmod 644 $PKG/usr/man/*/*

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/../$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME
  rm -rf $PKG
fi
