# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=sonarr
pkgver=4.0.9.2244
pkgrel=0
pkgdesc="TV download automation for usenet and torrents."
url="https://github.com/Sonarr/Sonarr"
arch="x86_64 aarch64 armv7"
license="GPL-3.0-only"
options="net" # net for nuget and yarn
depends="aspnetcore6-runtime sqlite-libs"
makedepends="dotnet6-sdk yarn"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/Sonarr/Sonarr/archive/refs/tags/v$pkgver.tar.gz
	0001-disable-restart.patch
	sonarr.initd
	sonarr.confd
	package_info
	"
builddir="$srcdir/Sonarr-$pkgver"
pkgusers="sonarr"
pkggroups="sonarr"

# secfixes:
#   4.0.5.1710-r0:
#     - CVE-2024-32655

# map arch to dotnet
case $CARCH in
	x86_64) _dotnet_arch="x64" ;;
	aarch64) _dotnet_arch="arm64" ;;
	armv7) _dotnet_arch="arm" ;;
	*) _dotnet_arch="$CARCH" ;;
esac

# custom variables
_runtime="linux-musl-$_dotnet_arch"
_framework="net6.0"
_output="_output"
_artifacts="$_output/$_framework/$_runtime/publish"

prepare() {
	default_prepare

	# increase max opened files
	ulimit -n 4096

	# replace version info
	local buildprops=src/Directory.Build.props
	sed -i "/<AssemblyVersion>/s/>.*<\//>$pkgver<\//" "$buildprops"
	sed -i "/<AssemblyConfiguration>/s/>.*<\//>master<\//" "$buildprops"

	export BROWSERSLIST_IGNORE_OLD_DATA=true
	yarn install --frozen-lockfile --network-timeout 120000

	# remove upstream dotnet version
	rm global.json
}

build() {
	dotnet build src \
		-p:RuntimeIdentifiers="$_runtime" \
		-p:Configuration=Release \
		-p:SelfContained=false \
		-t:PublishAllRids
	yarn build --env production --no-stats

	# fix ffprobe permissions
	chmod +x "$_artifacts"/ffprobe

	# cleanup
	find "$_artifacts" \( \
		-name "ServiceUninstall.*" -o \
		-name "ServiceInstall.*" -o \
		-name "Sonarr.Windows.*" \) -delete

	mv "$_output"/UI "$_artifacts"
}

check() {
	local _filters="Category!=ManualTest&Category!=AutomationTest&Category!=WINDOWS"

	# skipping:
	# 	IntegrationTest: broken
	# 	DiskTransferServiceFixture: CI fails due disk permission
	_filters="$_filters&Category!=IntegrationTest&FullyQualifiedName!~DiskTransferServiceFixture"

	# link build to tests
	ln -sf ../../../$_artifacts _tests/$_framework/$_runtime/bin
	mkdir -p ~/.config/Sonarr

	dotnet test src \
		--runtime "$_runtime" \
		--configuration Release \
		--filter "$_filters" \
		--no-build
}

package() {
	local destdir="$pkgdir"/usr/lib/sonarr

	# use package_info to disable update feature
	install -Dm644 "$srcdir"/package_info "$destdir"/package_info
	echo "PackageVersion=$pkgver-r$pkgrel" >>"$destdir"/package_info

	cp -af "$_artifacts" "$destdir"/bin
	chown -R "$pkgusers:$pkggroups" "$destdir"

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
b497620f7a79ccd058e9842cdbe47528ebc84e40846f87769bdb2823e27bac6b37c0f49389e8d705f487a7cbcee74083eaf0a4f00105c23b6c694287745c2bc1  sonarr-4.0.9.2244.tar.gz
753520379a6c5f2899b4ddfd820d5573598a32cb105f74c6fd978a3f2c98895cf709ef2d9afe2bae8c4856da86cb6a1364f5713ea7ef6a36bb21b5a5840c1471  0001-disable-restart.patch
6cda2b7afc5f61dc8460b9e6a1683d8a04b705cf0dfaf8ef79845b797bdf1b4bb582cf217c83f02717d7daaaf337112da8720c400ab1d374f8a6b04a7ee07286  sonarr.initd
d1cf825905ee8847f8094ec27d1b43df51558f0e94b17071cc15d019438c3c7d0c884fcc22876690e8b49d0f9dd3c2893ffcdab0c215282db27e24d0c8c6ec29  sonarr.confd
91274754046fe7b7aade722af2c0a1e00265cb0b43808ad757bb55e780794f2c9466d1036841bb2a6f2e114784585ebef8ad6378cee72197e27a7d2963e022ba  package_info
"
