# Forked from Alpine for temporary downstream patches

pkgname=libcamera
pkgver=9999
_pkgver=0.3.1
pkgrel=2
pkgdesc="Linux camera framework"
url="https://libcamera.org/"
arch="all"
license="LGPL-2.1-or-later AND GPL-2.0-or-later"
depends_dev="
	eudev-dev
	glib-dev
	gnutls-dev
	gst-plugins-bad-dev
	qt6-qtbase-dev
	"
# upstream calls 'date' with a non-POSIX option so we pull in coreutils
makedepends="$depends_dev
	coreutils
	doxygen
	graphviz
	gtest-dev
	libevent-dev
	libunwind-dev
	linux-headers
	meson
	py3-jinja2
	py3-ply
	py3-sphinx
	py3-yaml
	qt6-qttools-dev
	yaml-dev
	"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-doc
	qcam
	$pkgname-gstreamer
	$pkgname-v4l2
	$pkgname-tools
	"
source="https://gitlab.freedesktop.org/camera/libcamera/-/archive/v$_pkgver/libcamera-v$_pkgver.tar.gz
	0001-qcam-Decrease-minimum-width-of-selector-dialog.patch
	0001-libcamera-simple-Enable-softwareISP-for-the-librem5.patch
	0002-libcamera-simple-Force-disable-softwareISP-for-milli.patch
	0003-libcamera-simple-Enable-softISP-for-the-Pinephone.patch
	0004-libcamera-simple-Skip-hwISP-formats-if-swISP-is-acti.patch
	qcam.desktop
	90-libcamera.rules
	"
builddir="$srcdir/$pkgname-v$_pkgver"
# gstreamer tests fail
# manual strip because ipa .sign files depend on the file contents- have to re-sign after strip
options="!strip !check"

case "$CARCH" in
arm*|aarch64)
	subpackages="$subpackages $pkgname-raspberrypi"
	;;
esac

case "$CARCH" in
ppc64le|s390x|riscv64|loongarch64)
	# doesn't install any ipa
	;;
*)
	depends="$pkgname-ipa=$pkgver-r$pkgrel"
	subpackages="$subpackages $pkgname-ipa"
	;;
esac

build() {
	abuild-meson \
		-Dv4l2=true \
		-Dwerror=false \
		. output
	meson compile -C output
}

check() {
	meson test --no-rebuild -C output --print-errorlogs
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
	install -Dm644 -t "$pkgdir"/usr/share/applications "$srcdir"/qcam.desktop

	# manual strip first..
	scanelf --recursive \
		--nobanner \
		--etype "ET_DYN,ET_EXEC" \
		--format "%F" \
		"$pkgdir" \
		| while read -r file; do
			strip "$file"
	done

	install -Dm644 "$srcdir"/90-libcamera.rules \
		-t "$pkgdir"/lib/udev/rules.d/
}

ipa() {
	depends=""
	amove usr/lib/libcamera
	# then sign ipa's
	local ipa
	for ipa in "$subpkgdir"/usr/lib/libcamera/ipa*.so; do
		msg "signing $ipa"
		"$builddir"/src/ipa/ipa-sign.sh \
			"$(find "$builddir"/output -type f -iname "*ipa-priv-key.pem")" \
			"$ipa" \
			"$ipa".sign
	done
}

qcam() {
	depends=""
	amove usr/bin/qcam

	amove usr/share/applications/qcam.desktop
}

gstreamer() {
	depends=""
	amove usr/lib/gstreamer-1.0
}

v4l2() {
	depends=""
	amove usr/libexec/libcamera/v4l2-compat.so
}

raspberrypi() {
	depends=""
	amove usr/share/libcamera/ipa/rpi
	amove usr/libexec/libcamera/raspberrypi_ipa_proxy
	amove usr/share/libcamera/pipeline/rpi/vc4
}

tools() {
	depends=""
	amove usr/bin/cam
	amove usr/bin/lc-compliance
}

sha512sums="
85f62383194ca597baab36375e7b5ee99d6a8580fd54baf2615e5854bef790ef5ae57a92347c7e8408e2b9c04330d467dc56fab1af2f1c478c679a5a05e4f562  libcamera-v0.3.1.tar.gz
69d6e29d764f752feed453b9c0f61c81cdb506eb14893d0d99d0d6834f369f1e816c2381dfab5ad1a744a3d11211af02d75dc041ecc87c7e4f73e2bd34c666c2  0001-qcam-Decrease-minimum-width-of-selector-dialog.patch
a5809b7664685bb44ca05a722655d8b94991c90306b69be1e7b135a1ad72699ab69b0e9bf7edbee041700087b7d7bc7ae1ef704334e7672aa81e9e7c96ccf7f9  0001-libcamera-simple-Enable-softwareISP-for-the-librem5.patch
979259bb16112b1fbbec0543a1d642a65e62f24ba10b229caffe68398da343160d98c2811f923877ce5d2c9a3c8a53205feb2eb4fbaf921612bbc5419d6be1df  0002-libcamera-simple-Force-disable-softwareISP-for-milli.patch
47616a06fad66df31e29ebd9c024c95bdea542dd1f5c0ed448a242e35a6d9909f973a74332bfe084478c91ac7a2d778d7b10270cd493dba07bb8d5ad34e9544c  0003-libcamera-simple-Enable-softISP-for-the-Pinephone.patch
685b8cfae3bb9a000d95a36d9daf9d4ff8770fd877e43c69f0fc0768ebd457282f03ec6ad9bf3a33e72765665bbd642af5db66c1a82aac249ce8c2522291dee6  0004-libcamera-simple-Skip-hwISP-formats-if-swISP-is-acti.patch
22167a4eceb6d1b40b0b7c45fdf116c71684f5340de7f767535cb8e160ad9d2ae0f00cb3d461f73a344520a48a4641cf46226841d78bee06bfbfd2a91337f754  qcam.desktop
444a0a52b374751c388f807b6adcc63b36488547adaa5fb70b606dc5646132b1a893a76254236b5c80f3a6d041a4597d413e050af0323cd05626d758190838e2  90-libcamera.rules
"