#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://mscore.sourceforge.net/en/idx.php
# Packager Michelino Chionchio - Thanks to www.slacky.eu
# Depends on qt4; jack-audio-connection-kit

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-mscore
NAME=mscore
VERSION=0.9.3
ARCH=${ARCH:-i686}
BUILD=1mch

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

cd $TMP
tar jxvf $CWD/$NAME-$VERSION.tar.bz2
cd $NAME-$VERSION
chown -R root:root .
export PATH=/opt/qt4/bin:$PATH

make release || exit 1
make install DESTDIR=$PKG || exit 1

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a mscore/{COPYING,ChangeLog,README,NEWS} $PKG/usr/doc/$NAME-$VERSION
cp -a $PKG/usr/local/bin/* $PKG/usr/bin/
cp -r $PKG/usr/local/share $PKG/usr/
rm -r $PKG/usr/local/

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mkdir -p $PKG/install
mkdir -p $PKG//usr/share/$NAME/icons/
cat $CWD/slack-desc > $PKG/install/slack-desc
cp -a $CWD/doinst.sh $PKG/install/
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cp -a $CWD/mscore_icon.png $PKG//usr/share/$NAME/icons/
mkdir -p $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Name=MuseScore
GenericName=Graphical music typesetter
Exec=mscore
Icon=/usr/share/mscore/icons/mscore_icon.png
Type=Application
Categories=Application;Utility;
EOF

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
fi
