# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DUNE_PKG_NAME="liquidsoap liquidsoap-lang liquidsoap-js liquidsoap-mode
	prometheus-liquidsoap sdl-liquidsoap tls-liquidsoap"

inherit dune edo systemd shell-completion tmpfiles

JSON_COMMIT="1ef36fa01286573e846ac449e8683f8833c5b26a"
JSON5_COMMIT="ceb24d4080137d70833f86c25659c1331b80a387"
BLNS_COMMIT="db33ec7b1d5d9616a88c76394b7d0897bd0b97eb"

DESCRIPTION="A swiss-army knife for multimedia streaming, used for netradios"
HOMEPAGE="https://github.com/savonet/liquidsoap"
SRC_URI="https://github.com/savonet/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
	test? ( https://download.thimeo.com/Stereo_Tool_Generic_plugin.zip -> ${P}-stereo-tool-plugin.zip
		https://github.com/nst/JSONTestSuite/archive/${JSON_COMMIT}.tar.gz -> JSONTestSuite-${JSON_COMMIT}.tar.gz
		https://github.com/json5/json5-tests/archive/${JSON5_COMMIT}.tar.gz -> json5-tests-${JSON5_COMMIT}.tar.gz
		https://github.com/minimaxir/big-list-of-naughty-strings/archive/${BLNS_COMMIT}.tar.gz
			-> big-list-of-naughty-strings-${BLNS_COMMIT}.tar.gz )"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64"
IUSE="alsa ao camlimages ctypes dssi faad fdk ffmpeg frei0r gd graphics inotify
	irc jack jemalloc ladspa lame libsamplerate lilv lo mad memtrace +ocamlopt
	osc portaudio posix pulseaudio shine soundtouch sqlite srt ssl test yaml"
REQUIRED_USE="test? ( fdk lame shine soundtouch srt )"
RESTRICT="mirror test"
PROPERTIES="test_network"

RDEPEND="acct-group/liquidsoap
	acct-user/liquidsoap
	dev-ml/ca-certs:=[ocamlopt?]
	dev-ml/camomile:=[ocamlopt?]
	dev-ml/cohttp:0=[ocamlopt?]
	dev-ml/cry:0=[ocamlopt?]
	dev-ml/cstruct:0=[ocamlopt?]
	dev-ml/js_of_ocaml:0=[ocamlopt?]
	dev-ml/magic-mime:0=[ocamlopt?]
	dev-ml/menhir:0=[ocamlopt?]
	dev-ml/mem_usage:0=[ocamlopt?]
	dev-ml/metadata:0=[ocamlopt?]
	dev-ml/mirage-crypto:0=[ocamlopt?]
	dev-ml/mm:0=[ocamlopt?]
	dev-ml/ocaml-fileutils[ocamlopt?]
	dev-ml/ocurl:0=[ocamlopt?]
	dev-ml/prometheus:0=[ocamlopt?]
	dev-ml/re:0=[ocamlopt?]
	dev-ml/sedlex:0=[ocamlopt?]
	dev-ml/tls:0=[ocamlopt?]
	dev-ml/tsdl:0=[ocamlopt?]
	dev-ml/tsdl-image:0=[ocamlopt?]
	dev-ml/tsdl-ttf:0=[ocamlopt?]
	dev-ml/uri[ocamlopt?]
	dev-ml/xiph:0=[ocamlopt?]
	dev-ml/xml-light:0=[ocamlopt?]
	alsa? ( dev-ml/alsa:0=[ocamlopt?] )
	ao? ( dev-ml/ao:0=[ocamlopt?] )
	camlimages? ( dev-ml/camlimages:0=[ocamlopt?] )
	ctypes? ( dev-ml/ocaml-ctypes:0=[ocamlopt?] )
	dssi? ( dev-ml/dssi:0=[ocamlopt?] )
	faad? ( dev-ml/faad:0=[ocamlopt?] )
	fdk? ( dev-ml/fdkaac:0=[ocamlopt?] )
	ffmpeg? ( dev-ml/ffmpeg:0=[ocamlopt?] )
	frei0r? ( dev-ml/frei0r:0=[ocamlopt?] )
	gd? ( dev-ml/gd:0=[ocamlopt?]
		media-fonts/dejavu )
	graphics? ( dev-ml/graphics[ocamlopt?]
		media-fonts/dejavu )
	inotify? ( dev-ml/inotify:0=[ocamlopt?] )
	irc? ( dev-ml/irc-client:0=[ocamlopt?] )
	jack? ( dev-ml/bjack:0=[ocamlopt?] )
	jemalloc? ( dev-ml/jemalloc:0=[ocamlopt?] )
	ladspa? ( dev-ml/ladspa:0=[ocamlopt?] )
	lame? ( dev-ml/lame:0=[ocamlopt?] )
	libsamplerate? ( dev-ml/samplerate:=[ocamlopt?] )
	lilv? ( dev-ml/lilv:0=[ocamlopt?] )
	lo? ( dev-ml/lo:0=[ocamlopt?] )
	mad? ( dev-ml/mad:0=[ocamlopt?] )
	memtrace? ( dev-ml/memtrace:0=[ocamlopt?] )
	osc? ( dev-ml/osc:=[ocamlopt?] )
	portaudio? ( dev-ml/portaudio:0=[ocamlopt?] )
	posix? ( dev-ml/posix:0=[ocamlopt?] )
	pulseaudio? ( dev-ml/pulseaudio:0=[ocamlopt?] )
	shine? ( dev-ml/shine:0=[ocamlopt?] )
	soundtouch? ( dev-ml/soundtouch:0=[ocamlopt?] )
	sqlite? ( dev-ml/sqlite3:0=[ocamlopt?] )
	srt? ( dev-ml/srt:0=[ocamlopt?] )
	ssl? ( dev-ml/ocaml-ssl:0=[ocamlopt?] )
	yaml? ( dev-ml/yaml:0=[ocamlopt?] )"
BDEPEND="dev-ml/dune-build-info
	dev-ml/dune-site
	dev-ml/ppx_hash
	dev-ml/ppx_string
	test? ( app-arch/unzip
		media-video/ffmpeg[lame,opus,x264,vorbis] )"

PATCHES=( "${FILESDIR}/${PN}"-2.4.6-fix-extinf-parsing.patch
	"${FILESDIR}/${PN}"-2.4.6-tests-file-watch-timing.patch
	"${FILESDIR}/${PN}"-2.4.6-tests-offline-json.patch
	"${FILESDIR}/${PN}"-2.4.6-tests-regression-dune.patch )

src_unpack() {
	unpack "${P}.tar.gz"
	use test && unpack "JSONTestSuite-${JSON_COMMIT}.tar.gz" \
		"json5-tests-${JSON5_COMMIT}.tar.gz" \
		"big-list-of-naughty-strings-${BLNS_COMMIT}.tar.gz"
}

src_prepare() {
	default

	if use test ; then
		edo cp "${DISTDIR}/${P}-stereo-tool-plugin.zip" \
			tests/regression/Stereo_Tool_Generic_plugin.dist
		edo mkdir -p tests/core/{json,json5,big-list-of-naughty-strings}
		edo cp "${WORKDIR}/JSONTestSuite-${JSON_COMMIT}"/test_parsing/*.json \
			tests/core/json/
		edo find "${WORKDIR}/json5-tests-${JSON5_COMMIT}" \
			-type f \( -name '*.json' -o -name '*.js' -o -name '*.txt' \) \
			-exec cp -t tests/core/json5/ {} +
		edo cp -a "${WORKDIR}/big-list-of-naughty-strings-${BLNS_COMMIT}/." \
			tests/core/big-list-of-naughty-strings/
	fi
}

src_compile() {
	dune-compile ${DUNE_PKG_NAME}
}

src_test() {
	# exclude performance tests
	edo dune build @tests/core/citest @tests/language/citest \
		@tests/regression/citest @tests/streams/citest \
		@tests/harbor/citest @tests/media/mediatest \
		--profile release
}

src_install() {
	dune_src_install

	dobashcomp scripts/bash-completion
	keepdir /var/log/liquidsoap

	newtmpfiles "${FILESDIR}"/liquidsoap.tmpfile liquidsoap.conf
	newinitd "${FILESDIR}"/liquidsoap.initd liquidsoap
	systemd_dounit "${FILESDIR}"/liquidsoap.service
}

pkg_postinst() {
	tmpfiles_process liquidsoap.conf

	elog "You have to create an init script for each stream:"
	elog "ln -s liquidsoap /etc/init.d/liquidsoap.<stream>"
	elog "where script refers to /etc/liquidsoap/<stream>.liq"
}