#!/bin/bash
# http://yoshimi.sourceforge.net/
#
# script: til <rob DOT til DOT freedman AT googlemail DOT com>
#
set -e -u -x

#yoshimi
version=0.056
#
build=64.1
arch=i686
tune=pentium-m
#
PKG_DEP="alsa-lib,cxxlibs|gcc-g++,expat,fftw,flac,fltk,fontconfig,freetype,gcc,jack,libICE,libSM,libX11,libXau,libXdmcp,libXext,libXft,libXinerama,libXrender,libjpeg,libogg,libpng,libsndfile,libvorbis,libxcb,util-linux-ng,zlib"
PKG_SRC="http://sourceforge.net/projects/yoshimi/files/yoshimi-$version.tar.bz2/download
http://zenwalk.pinguix.com/user-accounts/tilderb/extra/xap/yoshimi/build-yoshimi.sh
http://zenwalk.pinguix.com/user-accounts/tilderb/extra/xap/yoshimi/yoshimi.png"

# check source exists
#for url in $PKG_SRC;do [ ! -f ${url##*/} ] && wget -N -c $url; done

pkgname=yoshimi-$version
package=$pkgname-$arch-$build

# build directory
CWD=`pwd`
TMP=$CWD/tmp
PKG=$TMP/$package
SRC=$TMP/$pkgname

[ -d $PKG ] && rm -fr $PKG
[ -d $SRC ] && rm -fr $SRC

mkdir -p $PKG/{install,usr/{src,doc}/$pkgname}
mkdir -p $PKG/usr/share/{applications,icons/hicolor/48x48/apps}
#
tar xjf $CWD/$pkgname.tar.bz2 -C $TMP
cd $SRC/src
# 
sed -i "s/O3 -march=native -msse -msse2/O2 -march=$arch -mtune=$tune -msse/" CMakeLists.txt
sed -i "s/fftw3_threads;pthread/fftw3/g" CMakeLists.txt

# Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders
# not needed anyway
sed -i "s:#include <sys/sysinfo.h>::" DSP/FFTwrapper.cpp

cmake . -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_VERBOSE_MAKEFILE=TRUE
  
make || exit 1
make install DESTDIR=$PKG

# docs
cp -a ../{COPYING,INSTALL,README.txt} $PKG/usr/doc/$pkgname
cd $PKG

# strip symbols
strip --strip-unneeded usr/bin/yoshimi

# copy slack description
#      |-----handy-ruler------------------------------------------------------|
cat <<EOF > install/slack-desc
yoshimi: yoshimi (is a software synthesizer)
yoshimi:
yoshimi: Yoshimi is a software synthesizer for Linux based on the 2.4.0 
yoshimi: release of ZynAddSubFX, written by Nasca Octavian Paul. 
yoshimi: Yoshimi delivers the same synth capabilities, along with very good 
yoshimi: Jack and Alsa midi/audio functionality. 
EOF

# desktop & (unofficial) icon 
cp $CWD/yoshimi.png usr/share/icons/hicolor/48x48/apps
cat <<EOF >usr/share/applications/yoshimi.desktop
[Desktop Entry]
Type=Application
Name=Yoshimi
Icon=yoshimi
Exec=yoshimi
GenericName=Virtual Software Synthesizer
Comment=A software synthesizer based on ZynAddSubFX  
Terminal=false
Categories=Audio;AudioVideo;AudioVideoEditing;Multimedia
EOF

# build script
cp $CWD/build-yoshimi.sh usr/src/$pkgname

rootworker=$(which fakeroot 2>/dev/null) || { [ "$UID" != "0" ] && { echo "need fakeroot"; exit 1; } }
$rootworker /sbin/makepkg -l y -c n $CWD/$package.txz

cd $CWD
echo "$PKG_SRC" >$package.src
echo -n $PKG_DEP >$package.dep
md5sum $package.txz >$package.md5
