SUMMARY="The Music Player Daemon"
DESCRIPTION="Music Player Daemon (MPD) is a flexible, powerful, server-side \
application for playing music. Through plugins and libraries it can play a \
variety of sound files while being controlled by its network protocol."
HOMEPAGE="https://www.musicpd.org/"
COPYRIGHT="2003-2020 The Music Player Daemon Project"
LICENSE="GNU GPL v2"
REVISION="6"
SOURCE_URI="http://www.musicpd.org/download/mpd/${portVersion%.*}/mpd-$portVersion.tar.xz"
CHECKSUM_SHA256="422ef0a996d961f3ebc6856395f3a855b45fa0059910e878fb98281007e510e1"
PATCHES="mpd-$portVersion.patchset"
ADDITIONAL_FILES="userlaunch.in"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

PROVIDES="
	mpd$secondaryArchSuffix = $portVersion
	cmd:mpd$commandSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
#	lib:libadplug$secondaryArchSuffix
#	lib:libao$secondaryArchSuffix >= 4
	lib:libaudiofile$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libbz2$secondaryArchSuffix
#	lib:libcdio$secondaryArchSuffix
#	lib:libcdio_paranoia$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
#	lib:ldns_sd$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix
	lib:libfaad$secondaryArchSuffix
	lib:libFLAC$secondaryArchSuffix
	lib:libfluidsynth$secondaryArchSuffix
	lib:libgcrypt$secondaryArchSuffix
	lib:libgme$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix >= 66
	lib:libid3tag$secondaryArchSuffix >= 0.3.0
	lib:libintl$secondaryArchSuffix
	lib:libiso9660$secondaryArchSuffix
	lib:libixml$secondaryArchSuffix #libupnp
	lib:libmad$secondaryArchSuffix
#	lib:libmms$secondaryArchSuffix
	lib:libmodplug$secondaryArchSuffix
	lib:libmp3lame$secondaryArchSuffix
	lib:libmpcdec$secondaryArchSuffix
	lib:libmpdclient$secondaryArchSuffix
	lib:libmpg123$secondaryArchSuffix
#	lib:libnfs$secondaryArchSuffix
	lib:libogg$secondaryArchSuffix
	lib:libopenal$secondaryArchSuffix
	lib:libopus$secondaryArchSuffix
	lib:libpcre$secondaryArchSuffix
	lib:libsamplerate$secondaryArchSuffix
#	lib:libshine$secondaryArchSuffix
#	lib:libshout$secondaryArchSuffix
#	lib:libsidplay2$secondaryArchSuffix
#	lib:libsmbclient$secondaryArchSuffix
	lib:libsndfile$secondaryArchSuffix
	lib:libsoxr$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libtwolame$secondaryArchSuffix
	lib:libupnp$secondaryArchSuffix
	lib:libvorbis$secondaryArchSuffix
	lib:libvorbisenc$secondaryArchSuffix
	lib:libwavpack$secondaryArchSuffix
#	lib:libwildmidi$secondaryArchSuffix
#	lib:libwrap$secondaryArchSuffix
	lib:libyajl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
#	devel:libadplug$secondaryArchSuffix
#	devel:libao$secondaryArchSuffix >= 4
	devel:libaudiofile$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libboost_system$secondaryArchSuffix >= 1.69.0
	devel:libbz2$secondaryArchSuffix
#	devel:libcdio$secondaryArchSuffix >= 19
#	devel:libcdio_paranoia$secondaryArchSuffix
	devel:libchromaprint$secondaryArchSuffix
	devel:libcppunit$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
#	devel:ldns_sd$secondaryArchSuffix
	devel:libexpat$secondaryArchSuffix
	devel:libfaad$secondaryArchSuffix
	devel:libFLAC$secondaryArchSuffix
	devel:libfluidsynth$secondaryArchSuffix
	devel:libgcrypt$secondaryArchSuffix
#	devel:libglib_2.0$secondaryArchSuffix >= 0.38
	devel:libgme$secondaryArchSuffix
	devel:libgtest$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libicui18n$secondaryArchSuffix >= 66
	devel:libid3tag$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libiso9660$secondaryArchSuffix >= 11
	devel:libmad$secondaryArchSuffix
#	devel:libmms$secondaryArchSuffix
	devel:libmodplug$secondaryArchSuffix
	devel:libmp3lame$secondaryArchSuffix
	devel:libmpcdec$secondaryArchSuffix
	devel:libmpdclient$secondaryArchSuffix
	devel:libmpg123$secondaryArchSuffix
#	devel:libnfs$secondaryArchSuffix
	devel:libogg$secondaryArchSuffix
	devel:libopenal$secondaryArchSuffix
	devel:libopus$secondaryArchSuffix
	devel:libpcre$secondaryArchSuffix
	devel:libsamplerate$secondaryArchSuffix
#	devel:libshine$secondaryArchSuffix
#	devel:libshout$secondaryArchSuffix
#	devel:libsidplay2$secondaryArchSuffix
#	devel:libsmbclient$secondaryArchSuffix
	devel:libsndfile$secondaryArchSuffix
	devel:libsoxr$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libtag$secondaryArchSuffix
	devel:libtwolame$secondaryArchSuffix
	devel:libupnp$secondaryArchSuffix
	devel:libvorbis$secondaryArchSuffix
	devel:libvorbisenc$secondaryArchSuffix
	devel:libwavpack$secondaryArchSuffix
#	devel:libwildmidi$secondaryArchSuffix
#	devel:libwrap$secondaryArchSuffix
	devel:libyajl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	setuptools_python3
#	cmd:avahi_client$secondaryArchSuffix
	cmd:diff
	cmd:gcc$secondaryArchSuffix
	cmd:meson >= 0.55.0
	cmd:ninja
	cmd:pkg_config$secondaryArchSuffix
	cmd:xres
	"

defineDebugInfoPackage mpd$secondaryArchSuffix \
	"$commandBinDir"/mpd

BUILD()
{
	mkdir -p haiku_build && cd haiku_build

	export LDFLAGS="-lnetwork"
	meson .. \
		--buildtype=release \
		--prefix=$prefix \
		--datadir=$docDir \
		-Dtest=true

	ninja $jobArgs
}

INSTALL()
{
	cd haiku_build
	ninja install

	# Should be fixed in the build-system
	mkdir -p $commandBinDir
	cp mpd $commandBinDir
	rc -o mpd.rsrc ../src/haiku/mpd.rdef
	xres -o $commandBinDir/mpd -- mpd.rsrc

	# Cleanup
	mv $docDir/doc/mpd/* $docDir
	cp ../doc/mpdconf.example $docDir
	rm -rf $docDir/{icons,doc}

	mkdir -p $dataDir/user_launch
	sed \
		-e "s|@APP@|mpd|" \
		-e "s|@BIN@|"$commandBinDir"/mpd$secondaryArchSuffix|" \
		$portDir/additional-files/userlaunch.in > $dataDir/user_launch/mpd
}

TEST()
{
	cd haiku_build
	ninja test
}
