#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://timidity.sourceforge.net
# packager: mauro.sacchetto@alice.it
# Requirements: celt,gus-patches,libsamplerate
# 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
ARCH=${ARCH:-i486}
BUILD=3ms

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"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
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 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 {} \;

chown -R root.root .


LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure 	--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--disable-static \
		--mandir=/usr/man \
		--enable-audio=oss,alsa,esd,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=$ARCH-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


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/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/timidity++.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/timidity++.SlackBuild

cd $PKG
ADD="gus-patches >= 20090910-noarch-1as" \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/timidity++-$VERSION-$ARCH-$BUILD.txz

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

