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

CWD=`pwd`
PKGNAME=TiMidity++
TMP=${TMP:-/tmp/txz/$PKGNAME}
PKG=$TMP/package
VERSION=2.13.2
ARCH=${ARCH:-i486}
BUILD=${BUILD:-5}
TAG=${TAG:-sl}

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

if [ ! -e $PKGNAME-$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="i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"; CHOST="x86_64"
fi

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

cd $TMP
tar xjvf $CWD/$PKGNAME-$VERSION.tar.bz2 || exit 1

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

echo -e "\E[0;32m+-----------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild TiMidity++ |\E[0;0m"
echo -e "\E[0;32m+-----------------------------+\E[0;0m"

chmod -R u+w,go+r-w,a-s .
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,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 || exit 1
make install DESTDIR=$PKG || exit 1



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


install -m 644 -p -D $CWD/rc.timidity $PKG/etc/rc.d/rc.timidity


mkdir -p  $PKG/usr/share/applications
cat << EOF > $PKG/usr/share/applications/$PKGNAME.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


install -m 644 -p -D $TMP/$PKGNAME-$VERSION/interface/pixmaps/timidity.xpm $PKG/usr/share/pixmaps/timidity.xpm


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


find $PKG/usr/man -type f -exec gzip -9 {} \;


cd $PKG
chown -R root:root $PKG
if [ -x "$(which depfinder 2>/dev/null)" ];then
  depfinder -f -s -a -p $PKG
  mv slack-required $CWD/
elif [ -x "$(which requiredbuilder 2>/dev/null)" ];then
  ADD=$(ls /var/log/packages/ | sed -ne 's/\(gus-patches\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2/p') \
  requiredbuilder -y -v -s $CWD $PKG
fi

/sbin/makepkg -l y -c n $CWD/timidity++-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

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



