#!/bin/busybox 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 those options:
#
# ADPLUG
# CHROMAPRINT
# GME
# MODPLUG
# MPCDEC
# NFS
# SAMBA
# SHINE
# SIDPLAY
# TREMOR
# UDISKS
# UPNP
# WILDMIDI
# YAML (yajl)
# ZZIP
#

PKGNAME=mpd
PKGVERSION=0.21.14
PKGREVISION=1
PKGLICENSE="GPLv20"
PKGSUMMARY="daemon for playing music of various formats"
PKGDOWNLOAD="https://www.musicpd.org/download/mpd/${PKGVERSION%.*}/$PKGNAME-$PKGVERSION.tar.xz"
PKGDEPENDS="libmpdclient meson:build"
PKGOPTIONS="AAC
            ALSA
            AO
            AUDIOFILE
            AVAHI
            BZIP2
            CD
            CURL
            DBUS
            FFMPEG
            FLAC
            FLUIDSYNTH
            ICU
            ID3
            IPV6
            JACK
            MIKMOD
            MMS
            MP2
            MP3
            OPENAL
            OPUS
            OSS
            PULSEAUDIO
            REGEX
            SAMPLERATE
            SHOUT
            SNDFILE
            SOXR
            SQLITE
            SYSLOG
            VORBIS
            WAVPACK
            WEBDAV
            XML
            YAML
            ZLIB"
PKGPROTECT="etc/mpd.conf etc/rc.d/mpd"
PKGUIDS="mpd:106"
PKGGIDS="mpd:106"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${AAC:=yes}
: ${ALSA:=yes}
: ${AO:=yes}
: ${AUDIOFILE:=yes}
: ${AVAHI:=yes}
: ${BZIP2:=yes}
: ${CD:=cdio cdio-paranoia}
: ${CURL:=yes}
: ${DBUS:=yes}
: ${FFMPEG:=yes}
: ${FLAC:=yes}
: ${FLUIDSYNTH:=yes}
: ${ICU:=yes}
: ${ID3:=yes}
: ${IPV6:=yes}
: ${JACK:=yes}
: ${MIKMOD:=yes}
: ${MMS:=yes}
: ${MP2:=yes}
: ${MP3:=lame mad mpg123}
: ${OPENAL:=yes}
: ${OPUS:=yes}
: ${OSS:=yes}
: ${PULSEAUDIO:=yes}
: ${REGEX:=yes}
: ${SAMPLERATE:=yes}
: ${SHOUT:=yes}
: ${SNDFILE:=yes}
: ${SOXR:=yes}
: ${SQLITE:=yes}
: ${SYSLOG:=yes}
: ${VORBIS:=yes}
: ${WEBDAV:=yes}        # Note: requires XML and CURL.
: ${XML:=yes}           # Note: XML support through expat dependency.
: ${YAML:=yes}          # Note: YAML support through yajl dependency.
: ${ZLIB:=yes}

if [ "$AAC" = "yes" ]; then
	PKGDEPENDS="faad2 $PKGDEPENDS"
	with_aac="-D faad=enabled"
else
	with_aac="-D faad=disabled"
fi

if [ "$ALSA" = "yes" ]; then
	PKGDEPENDS="alsa-lib $PKGDEPENDS"
	with_alsa="-D alsa=enabled"
else
	with_alsa="-D alsa=disabled"
fi

if [ "$AO" = "yes" ]; then
	PKGDEPENDS="libao $PKGDEPENDS"
	with_ao="-D ao=enabled"
else
	with_ao="-D ao=disabled"
fi

if [ "$AUDIOFILE" = "yes" ]; then
	PKGDEPENDS="audiofile $PKGDEPENDS"
	with_audiofile="-D audiofile=enabled"
else
	with_audiofile="-D audiofile=disabled"
fi

if [ "$AVAHI" = "yes" ]; then
	PKGDEPENDS="avahi $PKGDEPENDS"
	with_avahi="-D zeroconf=avahi"
else
	with_avahi="-D zeroconf=disabled"
fi

if [ "$BZIP2" = "yes" ]; then
	PKGDEPENDS="bzip2 $PKGDEPENDS"
	with_bzip2="-D bzip2=enabled"
else
	with_bzip2="-D bzip2=disabled"
fi

if [ -z "${CD##*cdio*}" ]; then
	PKGDEPENDS="libcdio $PKGDEPENDS"
	with_cdio="-D iso9660=enabled"
else
	with_cdio="-D iso9660=disabled"
fi

if [ -z "${CD##*cdio-paranoia*}" ]; then
	PKGDEPENDS="libcdio-paranoia $PKGDEPENDS"
	with_cdio_paranoia="-D cdio_paranoia=enabled"
else
	with_cdio_paranoia="-D cdio_paranoia=disabled"
fi

if [ "$CURL" = "yes" ]; then
	PKGDEPENDS="curl $PKGDEPENDS"
	with_curl="-D curl=enabled"
else
	with_curl="-D curl=disabled"
fi

if [ "$DBUS" = "yes" ]; then
	PKGDEPENDS="dbus $PKGDEPENDS"
	with_dbus="-D dbus=enabled"
else
	with_dbus="-D dbus=disabled"
fi

if [ "$FFMPEG" = "yes" ]; then
	PKGDEPENDS="ffmpeg $PKGDEPENDS"
	with_ffmpeg="-D ffmpeg=enabled"
else
	with_ffmpeg="-D ffmpeg=disabled"
fi

if [ "$FLAC" = "yes" ]; then
	PKGDEPENDS="flac $PKGDEPENDS"
	with_flac="-D flac=enabled"
else
	with_flac="-D flac=disabled"
fi

if [ "$FLUIDSYNTH" = "yes" ]; then
	PKGDEPENDS="fluidsynth $PKGDEPENDS"
	with_fluidsynth="-D fluidsynth=enabled"
else
	with_fluidsynth="-D fluidsynth=disabled"
fi

if [ "$ICU" = "yes" ]; then
	PKGDEPENDS="icu $PKGDEPENDS"
	with_icu="-D icu=enabled"
else
	with_icu="-D icu=disabled"
fi

if [ "$ID3" = "yes" ]; then
	PKGDEPENDS="libid3tag $PKGDEPENDS"
	with_id3="-D id3=enabled"
else
	with_id3="-D id3=disabled"
fi

if [ "$IPV6" = "yes" ]; then
	with_ipv6="-D ipv6=enabled"
else
	with_ipv6="-D ipv6=disabled"
fi

if [ "$JACK" = "yes" ]; then
	PKGDEPENDS="jack2 $PKGDEPENDS"
	with_jack="-D jack=enabled"
else
	with_jack="-D jack=disabled"
fi

if [ "$MIKMOD" = "yes" ]; then
	PKGDEPENDS="libmikmod $PKGDEPENDS"
	with_mikmod="-D mikmod=enabled"
else
	with_mikmod="-D mikmod=disabled"
fi

if [ "$MMS" = "yes" ]; then
	PKGDEPENDS="libmms $PKGDEPENDS"
	with_mms="-D mms=enabled"
else
	with_mms="-D mms=disabled"
fi

if [ "$MP2" = "yes" ]; then
	PKGDEPENDS="twolame $PKGDEPENDS"
	with_mp2="-D twolame=enabled"
else
	with_mp2="-D twolame=disabled"
fi

if [ -z "${MP3##*mad*}" ]; then
	PKGDEPENDS="libmad $PKGDEPENDS"
	with_mad="-D mad=enabled"
else
	with_mad="-D mad=disabled"
fi

if [ -z "${MP3##*lame*}" ]; then
	PKGDEPENDS="lame $PKGDEPENDS"
	with_lame="-D lame=enabled"
else
	with_lame="-D lame=disabled"
fi

if [ -z "${MP3##*mpg123*}" ]; then
	PKGDEPENDS="mpg123 $PKGDEPENDS"
	with_mpg123="-D mpg123=enabled"
else
	with_mpg123="-D mpg123=disabled"
fi

if [ "$OPENAL" = "yes" ]; then
	PKGDEPENDS="openal-soft $PKGDEPENDS"
	with_openal="-D openal=enabled"
else
	with_openal="-D openal=disabled"
fi

if [ "$OPUS" = "yes" ]; then
	PKGDEPENDS="opus $PKGDEPENDS"
	with_opus="-D opus=enabled"
else
	with_opus="-D opus=disabled"
fi

if [ "$OSS" = "yes" ]; then
	with_oss="-D oss=enabled"
else
	with_oss="-D oss=disabled"
fi

if [ "$PULSEAUDIO" = "yes" ]; then
	PKGDEPENDS="pulseaudio $PKGDEPENDS"
	with_pulseaudio="-D pulse=enabled"
else
	with_pulseaudio="-D pulse=disabled"
fi

if [ "$REGEX" = "yes" ]; then
	PKGDEPENDS="pcre $PKGDEPENDS"
	with_regex="-D pcre=enabled"
else
	with_regex="-D pcre=disabled"
fi

if [ "$SAMPLERATE" = "yes" ]; then
	PKGDEPENDS="libsamplerate $PKGDEPENDS"
	with_samplerate="-D libsamplerate=enabled"
else
	with_samplerate="-D libsamplerate=disabled"
fi

if [ "$SHOUT" = "yes" ]; then
	PKGDEPENDS="libshout $PKGDEPENDS"
	with_shout="-D shout=enabled"
else
	with_shout="-D shout=disabled"
fi

if [ "$SNDFILE" = "yes" ]; then
	PKGDEPENDS="libsndfile $PKGDEPENDS"
	with_sndfile="-D sndfile=enabled"
else
	with_sndfile="-D snfile=disabled"
fi

if [ "$SOXR" = "yes" ]; then
	PKGDEPENDS="soxr $PKGDEPENDS"
	with_soxr="-D soxr=enabled"
else
	with_soxr="-D soxr=disabled"
fi

if [ "$SQLITE" = "yes" ]; then
	PKGDEPENDS="sqlite $PKGDEPENDS"
	with_sqlite="-D sqlite=enabled"
else
	with_sqlite="-D sqlite=disabled"
fi

if [ "$SYSLOG" = "yes" ]; then
	with_syslog="-D syslog=enabled"
else
	with_syslog="-D syslog=disabled"
fi

if [ "$VORBIS" = "yes" ]; then
	PKGDEPENDS="libvorbis $PKGDEPENDS"
	with_vorbis="-D vorbis=enabled"
else
	with_vorbis="-D vorbis=disabled"
fi

if [ "$WAVPACK" = "yes" ]; then
	PKGDEPENDS="wavpack $PKGDEPENDS"
	with_wavpack="-D wavpack=enabled"
else
	with_wavpack="-D wavpack=disabled"
fi

if [ "$WEBDAV" = "yes" ]; then
	with_webdav="-D webdav=enabled"
else
	with_webdav="-D webdav=disabled"
fi

if [ "$XML" = "yes" ]; then
	PKGDEPENDS="expat $PKGDEPENDS"
	with_xml="-D expat=enabled"
else
	with_xml="-D expat=disabled"
fi

if [ "$YAML" = "yes" ]; then
	PKGDEPENDS="yaml $PKGDEPENDS"
	with_yaml="-D yajl=enabled"
else
	with_yaml="-D yajl=disabled"
fi

if [ "$ZLIB" = "yes" ]; then
	PKGDEPENDS="zlib $PKGDEPENDS"
	with_zlib="-D zlib=enabled"
else
	with_zlib="-D zlib=disabled"
fi

build()
{
	rm -rf $PKGNAME-$PKGVERSION
	tar xvf $PKGNAME-$PKGVERSION.tar.xz
	cd $PKGNAME-$PKGVERSION

	CC="$CC" \
	CFLAGS="$CFLAGS" \
	CXX="$CXX" \
	CXXFLAGS="$CXXFLAGS" \
	LDFLAGS="$LDFLAGS" \
	meson . build \
		--prefix / \
		--buildtype release \
		--default-library shared \
		-D systemd=disabled \
		$with_aac \
		$with_alsa \
		$with_ao \
		$with_audiofile \
		$with_avahi \
		$with_bzip2 \
		$with_cdio \
		$wiht_cdio_paranoia \
		$with_curl \
		$with_dbus \
		$with_ffmpeg \
		$with_flac \
		$with_fluidsynth \
		$with_icu \
		$with_id3 \
		$with_ipv6 \
		$with_jack \
		$with_lame \
		$with_mad \
		$with_mikmod \
		$with_mms \
		$with_mp2 \
		$with_mpg123 \
		$with_openal \
		$with_opus \
		$with_oss \
		$with_pulseaudio \
		$with_regex \
		$with_samplerate \
		$with_shout \
		$with_sndfile \
		$with_soxr \
		$with_sqlite \
		$with_syslog \
		$with_vorbis \
		$with_wavpack \
		$with_webdav \
		$with_xml \
		$with_yaml \
		$with_zlib
	ninja -C build
	DESTDIR=$DESTDIR ninja -C build install

	# Not installed by default.
	install -Dm644 doc/mpdconf.example $DESTDIR/etc/mpd.conf
	install -Dm644 doc/mpd.1 $DESTDIR/share/man/man1/mpd.1
	install -Dm644 doc/mpd.conf.5 $DESTDIR/share/man/man5/mpd.conf.5
	install -Dm644 ../mpd $DESTDIR/etc/rc.d/mpd

	# Customize the default config with our pid, uid and gid.
	sed -i -e 's|^#pid_file.*|pid_file "/var/run/mpd/mpd.pid"|' $DESTDIR/etc/mpd.conf
	sed -i -e 's|^#user.*|user "mpd"|' $DESTDIR/etc/mpd.conf
	sed -i -e 's|^#group.*|group "mpd"|' $DESTDIR/etc/mpd.conf

	cd ..
	rm -rf $PKGNAME-$PKGVERSION
}
