#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# TODO: add PORTAUDIO option

PKGNAME=fluidsynth
PKGVERSION=2.0.6
PKGREVISION=1
PKGLICENSE="CUSTOM"
PKGSUMMARY="SoundFont synthesizer"
PKGDOWNLOAD="https://github.com/FluidSynth/$PKGNAME/archive/v2.0.6.tar.gz"
PKGDEPENDS="cmake:build"
PKGOPTIONS="ALSA
            AUDIO
            DBUS
            IPV6
            JACK
            LADSPA
            OSS
            PULSEAUDIO
            READLINE
            SNDFILE"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${ALSA:=yes}
: ${AUDIO:=yes}
: ${DBUS:=yes}
: ${IPV6:=yes}
: ${JACK:=yes}
: ${LADSPA:=yes}
: ${OSS:=yes}
: ${PULSEAUDIO:=yes}
: ${READLINE:=yes}
: ${SNDFILE:=yes}

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="-Denable-alsa=On"
else
	with_alsa="-Denable-alsa=Off"
fi

if [ "$AUDIO" = "yes" ]; then
	with_audio="-Denable-audio=On"
else
	with_audio="-Denable-audio=Off"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="dbus $PKGDEPENDS"
	with_dbus="-Denable-dbus=On"
else
	with_dbus="-Denable-dbus=Off"
fi

if [ "$IPV6" = "yes" ]; then
	with_ipv6="-Denable-ipv6=On"
else
	with_ipv6="-Denable-ipv6=Off"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="jack2 $PKGDEPENDS"
	with_jack="-Denable-jack=On"
else
	with_jack="-Denable-jack=Off"
fi

if [ "$LADSPA" = "yes" ]; then
	PKGDEPENDS="ladspa glib $PKGDEPENDS"
	with_ladspa="-Denable-ladspa=On"
else
	with_ladspa="-Denable-ladspa=Off"
fi

if [ "$OSS" = "yes" ]; then
	with_oss="-Denable-oss=On"
else
	with_oss="-Denable-oss=Off"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="pulseaudio $PKGDEPENDS"
	with_pulseaudio="-Denable-pulseaudio=On"
else
	with_pulseaudio="-Denable-pulseaudio=Off"
fi

if [ "$READLINE" = "yes" ]; then
	PKGDEPENDS="readline $PKGDEPENDS"
	with_readline="-Denable-readline=On"
else
	with_readline="-Denable-readline=Off"
fi

if [ "$SNDFILE" = "yes" ]; then
	PKGDEPENDS="libsndfile $PKGDEPENDS"
	with_sndfile="-Denable-libsndfile=On"
else
	with_sndfile="-Denable-libsndfile=Off"
fi

rm -rf $PKGNAME-$PKGVERSION
tar xvf v$PKGVERSION.tar.gz
cd $PKGNAME-$PKGVERSION

mkdir build && cd build
cmake .. \
	-DBUILD_SHARED_LIBS=On \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_CXX_COMPILER="$CXX" \
	-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
	-DCMAKE_C_COMPILER="$CC" \
	-DCMAKE_C_FLAGS="$CFLAGS" \
	-DCMAKE_INSTALL_PREFIX= \
	-DLIB_SUFFIX="" \
	-Denable-systemd=Off \
	$with_alsa \
	$with_audio \
	$with_dbus \
	$with_ipv6 \
	$with_ladspa \
	$with_oss \
	$with_pulseaudio \
	$with_readline \
	$with_sndfile
make
make install DESTDIR=$DESTDIR
cd ..

cd ..
rm -rf $PKGNAME-$PKGVERSION
