# Forked from Alpine for temporary downstream patches

pkgname=libcamera
_pkgver=0.5.1
pkgver=9999$_pkgver
pkgrel=1
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
	libpisp-dev
	libunwind-dev
	libyuv-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-pipeline-simple-Fix-matching-with-empty-media-graphs.patch
	0002-libcamera-simple-Enable-softwareISP-for-the-librem5.patch
	0003-libcamera-simple-Force-disable-softwareISP-for-milli.patch
	0004-libcamera-simple-Enable-softISP-for-the-Pinephone.patch
	0005-libcamera-simple-Skip-hwISP-formats-if-swISP-is-acti.patch
	0006-pipeline-simple-Consider-output-sizes-when-choosing-.patch
	0007-pipeline-simple-Increase-internal-buffer-count-to-fo.patch
	0008-ipa-simple-Add-tuning-file-for-IMX355.patch
	0009-ipa-simple-Add-tuning-file-for-IMX363.patch
	0010-ipa-simple-Add-tuning-file-for-s5k3l6xx.patch
	0011-ipa-simple-Add-tuning-file-for-hi846.patch
	0012-ipa-simple-Add-tuning-file-for-IMX371.patch
	0013-ipa-simple-Add-tuning-file-for-IMX376.patch
	qcam.desktop
	"
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 \
		-Dtest=true \
		-Dv4l2=true \
		-Dwerror=false \
		. output
	meson compile -C output
}

check() {
	meson test -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
}

ipa() {
	depends=""
	amove usr/lib/libcamera
	# then sign ipa's
	local ipa
	for ipa in "$subpkgdir"/usr/lib/libcamera/ipa/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="
2585f9b9e016fca9ca9e93c0f11129ee83117b7d0498a3c7d6b94de1a5f0a2e97c31fbaaaebfd64b8dba0e686f37bc5e74fb0b5b4f501cb1d1532792f04ffcd7  libcamera-v0.5.1.tar.gz
d4dcad9dbe5c2d4be77d8b66bc064cbed3a87dfafd7969665f7c7063dc25ce5a96aaa0ef27329dc69e0b644f7cb371820f7fd362c7d87b8174c853e0f8e57325  0001-pipeline-simple-Fix-matching-with-empty-media-graphs.patch
46c98267cb36c325335cc33642f66e1690305b1bc28909d11d51c613f41fca57df94cf47b683f602f791d33ba9ceb01e02c3a81dc4a62a4ab49725ef686bcfee  0002-libcamera-simple-Enable-softwareISP-for-the-librem5.patch
c23416c2c3cb29532991f079f44a15ed903f85216be9ac5953c2ccfbb8e7eb79c674a9b05d4dd132db553b7cc9a8fb217c47bb737d2d841a54d5919b3660f16a  0003-libcamera-simple-Force-disable-softwareISP-for-milli.patch
174a2c40106a0c3a65ca72c2de5831e44b3fcb5bf68bf6305b66d4662d3eeaad2e327ab8baf371fca1f21ba00e7c8658533d86060a2e6fc954470d309c7a567f  0004-libcamera-simple-Enable-softISP-for-the-Pinephone.patch
2a7bffe9785b3f99fce5f7d67762f1810b5c8cf34d1743f92e19604e9d44dcd4e4df83d19ccd8d3736a853324b380cdbb42e9bd8925e15a027d3a00bdb2fad62  0005-libcamera-simple-Skip-hwISP-formats-if-swISP-is-acti.patch
e564728d051d53e3b0e25b396a1dd9680bee391dcde3a0c3d4a1e42bce2b7351eb29e9a429508bbf6dd94820d27f98600ed35e54e2f91a26dc999f6a715a0f2d  0006-pipeline-simple-Consider-output-sizes-when-choosing-.patch
14df052835936025e7d9632906831a3309d2564fa7dac6cb0ed977a3e15319c794c27382a1a3395e74437d4914d6db3b39a44327cac7d744c13479e41f6a53a9  0007-pipeline-simple-Increase-internal-buffer-count-to-fo.patch
d0ecf2d8da8d236dfa643c2f1c40ef46a0ce4d33ce6c30ecf80887bd8075c056d75d26afc94bd64fe440d0c0115234516130f65e8795ec31133f448bb782886a  0008-ipa-simple-Add-tuning-file-for-IMX355.patch
38250844de01b52a5a1fe8e28716b199b25a5f6f1cda757cf8c8095442bb659587f095d892489eb4ea1fe249b7661c109bc66b62374d4b2f95d7621e405a4b11  0009-ipa-simple-Add-tuning-file-for-IMX363.patch
0e675182876d1a56fe7fd4d8fae20e1c9e58afa8f98de6b3bcdd5e66be698f22a44f64db45a0947017e0f1028e925605f2cf132a3e759be36dd140a756560d00  0010-ipa-simple-Add-tuning-file-for-s5k3l6xx.patch
58f7ff3d69e0099fa314ca256ec58cbd798f0a3880ada5b3cae7eb6809b9915980877f529a8cbe2908d5241b490cd2023d16d9287265de59a30ac5dfe31b6da5  0011-ipa-simple-Add-tuning-file-for-hi846.patch
173e1188e95d1e750ad1cfb841910b3091688b66cc416ae3a94ce50ac2c9e21945a095cf9225006d34df1532703ecf561c52134ecf00d5b3d53159a6df7ed94f  0012-ipa-simple-Add-tuning-file-for-IMX371.patch
0409ecbb03eac9fd73ec43807db54882f63af9ed71c65d8def63afb2a2b3cd708fda47c5de683218b89bb7417938dc23cab70756baecea1746e757059bae83aa  0013-ipa-simple-Add-tuning-file-for-IMX376.patch
22167a4eceb6d1b40b0b7c45fdf116c71684f5340de7f767535cb8e160ad9d2ae0f00cb3d461f73a344520a48a4641cf46226841d78bee06bfbfd2a91337f754  qcam.desktop
"
