#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://www.arnoldarts.de/drupal/?q=JackMix%3Aintro
# Packagers zapotech (zapotech2000 at yahoo.it)
# http://www.slacky.eu
# Depends From:
# Jack Audio (http://jackit.sourceforge.net/) and QT>=4.2

CWD=`pwd`
if ["$TMP" = ""]; then
 TMP=/tmp/tgz
fi
PKG=$TMP/pkg-jackmix0.3.1
NAME=jackmix
VERSION=0.3.1
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=2fv

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then 
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar zxvf $CWD/$NAME-$VERSION.tar.gz
mv jackmix-0.3 $NAME-$VERSION
cd $NAME-$VERSION
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
# To compile we need to set the QTDIR to the QT4 directory tree (regarding the "qt4-4.3.1-i686-3as.tgz" package)
# The jackmix-0.3.1 directory tree is: /opt/jackmix-0.3.1. This allow the coexistence with the old jackmix version QT3 based version, too.
# Per la compilazione settiamo la variabile QTDIR su /opt/qt4 (in relazione al pacchetto "qt4-4.3.1-i686-3as.tgz")
# jackmix verrą installato in /opt/jackmix-0.3.1. Questo consente, inoltre, la coesistenza con la vecchia versione basata sulle QT3
QTDIR=/opt/qt4 scons configure prefix=/opt/$NAME-$VERSION
scons
scons install DESTDIR=$PKG

mkdir $PKG/opt/$NAME-$VERSION/doc
cp -a AUTHORS COPYING INSTALL NEWS README VERSION TODO ChangeLog \
$PKG/opt/$NAME-$VERSION/doc
cp $CWD/Usage.txt $PKG/opt/$NAME-$VERSION/doc

mkdir $PKG/opt/$NAME-$VERSION/share
cp $CWD/jackmix.png $PKG/opt/$NAME-$VERSION/share

# Put the desktop link in /usr/share/applications
# Posizioniamo il collegamento desktop in /usr/share/applications
mkdir -p $PKG/usr/share/applications/
cp $CWD/jackmix-0.3.1.desktop $PKG/usr/share/applications

# Make a symbolic link in /usr/bin named jackmix-0.3.1 to the /opt/jackmix-0.3.1/bin/jackmix executable.
# Creiamo un link simobolico, in /usr/bin, all'eseguibile /opt/jackmix-0.3.1/bin/jackmix
mkdir -p $PKG/usr/bin
ln -s /opt/$NAME-$VERSION/bin/jackmix $PKG/usr/bin/jackmix-0.3.1

cd $PKG
mv *_test opt/$NAME-$VERSION/share

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 -f1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/opt/$NAME-$VERSION/doc/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/opt/$NAME-$VERSION/doc/$NAME.SlackBuild
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
