#!/bin/sh
# Heavily based on the Slackware 12.1 SlackBuild
# http://timidity.sourceforge.net
# packager: mauro.sacchetto@alice.it
# Requirements: Jack,Speex.
# Modify By Andrea Sciucca (gohanz at infinito)
# Thank's to phobos3576 for TiMidity++ Howto.

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package/TiMidity++
NAME=TiMidity++
VERSION=2.13.2
CHOST=i486
ARCH=${ARCH:-i486}
BUILD=1ms

SOURCE=http://mesh.dl.sourceforge.net/sourceforge/timidity/$NAME-$VERSION.tar.bz2

if [ ! -e $NAME-$VERSION.tar.bz2 ]; then
wget -c $SOURCE
fi

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"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
fi

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

cd $TMP
tar xjvf $CWD/$NAME-$VERSION.tar.bz2

echo -e "\E[0;32m+------------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild $NAME-$VERSION |\E[0;0m"
echo -e "\E[0;32m+------------------------------------+\E[0;0m"

cd $NAME-$VERSION
zcat $CWD/timidity_2.13.2-19.diff.gz | patch -p1 -E --backup --verbose

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 \
--enable-audio=oss,alsa,arts,esd,jack,ao,vorbis,flac,speex \
--enable-dynamic \
--enable-interface=ncurses,slang,motif,tcltk,emacs,vt100,xskin,gtk,server,alsaseq \
--enable-spectrogram \
--enable-network \
--program-prefix= \
--program-suffix= \
--build=$CHOST-slackware-linux
make
make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION 
cp -a AUTHORS ChangeLog* COPYING INSTALL NEWS README TODO $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/etc/rc.d
cp -a $CWD/rc.timidity $PKG/etc/rc.d

( cd $PKG/usr/share
  tar xvzf $CWD/timidity.tar.gz
)

mkdir -p  $PKG/usr/share/applications

cat << EOF > $PKG/usr/share/applications/$NAME.desktop
[Desktop Entry]
Name=TiMidity++
GenericName=Software Midi Synthesizer
GenericName[it]=Sintetizzatore Midi Software
Comment=TiMidity++ can play MIDI files by converting them into PCM waveform data
Comment[it]=TiMidity++ può suonare file midi convertendoli in dati audio PCM.
Exec=timidity -ig
Terminal=false
Type=Application
Icon=/usr/share/pixmaps/timidity.xpm
Categories=AudioVideo;
Encoding=UTF-8
EOF

mkdir -p $PKG/usr/share/pixmaps

( cd $TMP/$NAME-$VERSION/interface/pixmaps
  cp -a timidity.xpm $PKG/usr/share/pixmaps
)

gzip -9 $PKG/usr/man/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/timidity++.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/timidity++.SlackBuild

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

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

