# Reference: <https://postmarketos.org/devicepkg>
# Maintainer: Alexey Min <alexey.min@gmail.com>
# Co-Maintainer: Martijn Braam <martijn@brixit.nl>
pkgname=device-pine64-pinetab
pkgdesc="PINE64 PineTab"
pkgver=0.10
pkgrel=1
url="https://postmarketos.org"
license="MIT"
arch="aarch64"
options="!check !archcheck"
depends="
	alsa-ucm-conf
	mesa-egl
	mesa-dri-gallium
	postmarketos-base
	u-boot-pinephone>=2021.01_git20201228-r2
	u-boot-tools
"
makedepends="devicepkg-dev"
subpackages="
	$pkgname-kernel-allwinner:kernel_allwinner
	$pkgname-kernel-allwinner-dev:kernel_allwinner_dev
	$pkgname-sway
	$pkgname-weston
	$pkgname-phosh
	$pkgname-nonfree-firmware:nonfree_firmware
	$pkgname-plasma-mobile:plasma_mobile
	$pkgname-upower
	"
install="$pkgname.post-install"
source="
	99-pinetab-keyboard.hwdb
	deviceinfo
	gsk-renderer-gl.sh
	kirigami-lowpower.sh
	rootston.ini
	sway.conf
	sysrq.conf
	uboot-script.cmd
	ucm/HiFi.conf
	ucm/PineTab.conf
	weston.ini
	UPower.conf
	"

prepare() {
	default_prepare
	for version in pinetab-dev pinetab; do
		sed "1s;^;\nsetenv pinetabfdt sun50i-a64-$version.dtb\;\n;" \
			"$srcdir"/uboot-script.cmd > "$srcdir/uboot-script-$version.cmd"
	done
}

build() {
	devicepkg_build $startdir $pkgname
}

kernel_allwinner() {
	pkgdesc="Allwinner kernel with support for Pinetab LCD panel (from Early Adopter)"
	depends="linux-postmarketos-allwinner"
	devicepkg_subpackage_kernel $startdir $pkgname $subpkgname

	mkimage \
		-A arm \
		-O linux \
		-T script \
		-C none \
		-a 0 \
		-e 0 \
		-n postmarketos \
		-d "$srcdir"/uboot-script-pinetab.cmd \
		"$srcdir"/boot.scr

	# U-Boot
	install -Dm644 "$srcdir"/boot.scr \
		"$subpkgdir"/boot/boot.scr
}

kernel_allwinner_dev() {
	pkgdesc="Allwinner kernel with support for old Pinetab LCD panel (pre Early Adopter)"
	depends="linux-postmarketos-allwinner"
	devicepkg_subpackage_kernel $startdir $pkgname $subpkgname

	mkimage \
		-A arm \
		-O linux \
		-T script \
		-C none \
		-a 0 \
		-e 0 \
		-n postmarketos \
		-d "$srcdir"/uboot-script-pinetab-dev.cmd \
		"$srcdir"/boot_dev.scr

	# U-Boot
	install -Dm644 "$srcdir"/boot_dev.scr \
		"$subpkgdir"/boot/boot.scr
}

sway() {
	install_if="$pkgname postmarketos-ui-sway"
	install -D -m644 "$srcdir"/sway.conf \
		"$subpkgdir"/etc/sway/config.d/pinetab
}

weston() {
	install_if="$pkgname postmarketos-ui-weston"
	install -Dm644 "$srcdir"/weston.ini \
		"$subpkgdir"/etc/xdg/weston/weston.ini
}

phosh() {
	install_if="$pkgname postmarketos-ui-phosh"
	install -Dm644 "$srcdir"/rootston.ini \
		"$subpkgdir"/etc/phosh/rootston.ini
}

package() {
	devicepkg_package $startdir $pkgname

	# Fix "sysrq: HELP..." messages in dmesg while playing audio
	# (Headphone output interferes with the serial console on the headphone jack)
	install -D -m644 "$srcdir"/sysrq.conf \
		"$pkgdir"/etc/sysctl.d/sysrq.conf

	# Alsa usecase manager config
	install -D -m644 "$srcdir"/PineTab.conf \
		"$pkgdir"/usr/share/alsa/ucm2/PineTab/PineTab.conf
	install -D -m644 "$srcdir"/HiFi.conf \
		"$pkgdir"/usr/share/alsa/ucm2/PineTab/HiFi.conf

	# Fix for the meta key on v1 keyboard dock, probably not needed on newer revisions
	install -Dm644 "$srcdir"/99-pinetab-keyboard.hwdb \
		"$pkgdir"/etc/udev/hwdb.d/99-pinetab-keyboard.hwdb

	# Prevent artifacts in GTK4 apps (pmaports#1069)
	install -Dm755 "$srcdir"/gsk-renderer-gl.sh -t \
		"$pkgdir"/etc/profile.d/
}

nonfree_firmware() {
	pkgdesc="Bluetooth Firmware"
	depends="firmware-pine64-rtl8723bt"
	mkdir "$subpkgdir"
}

plasma_mobile() {
	install_if="$pkgname=$pkgver-r$pkgrel kirigami2"
	install -Dm644 "$srcdir"/kirigami-lowpower.sh "$subpkgdir"/etc/profile.d/kirigami-lowpower.sh
}

upower() {
	install_if="$pkgname=$pkgver-r$pkgrel upower"
	replaces="upower"

	# Shutdown when PMIC reports critical battery level
	install -Dm644 "$srcdir"/UPower.conf \
		"$subpkgdir"/etc/UPower/UPower.conf
}

sha512sums="
8d1ef04911f1cfa7eda8185f4f4d807af23105a7c8f23d8c18f02afeccd8ab124de70c2e2760c154a3128fe3793447039fb0abf37aa496f597d27051e275033e  99-pinetab-keyboard.hwdb
a884cdb677d40f733fde5ce57fc2740a29a9f8a4a62d9ac327710a650d0d21d378226653e50d67cd0765f6e6cb81e6a243a92905d0ce8b8d8ab751abb69a79d5  deviceinfo
9244687bfea20c78120f7c48388d141eff3e23a421e48504eac1479ec7f51197d9ca72468c9cc33c6e3d3fa32ee3514711545816e607d49870226e4a41a60a85  gsk-renderer-gl.sh
6bc22b369b2d9058b702ac7a7862abaefaf8179b43491868bdb008d181b59104f1ded89db1da51a810e1ccdc803a579b924a438674283a65afc3a9c4d2894caf  kirigami-lowpower.sh
38827d9636c48b133eb333764a6799a6e93454e01ecb0cfde1ecad3ee149606ba59eb1a5e6c8366e3b3891e54bd664a0a5acdd89d38ac3a3551260e9b9a6d3fc  rootston.ini
9e71c61bfa72b60e749c4dc04f17eb59d25de822765e81e65c937d6be5236d3951a652b69a1ab973abe6add9f34705be5dbaa98bbfa25daf7504dd50b28524c2  sway.conf
f4b5509fd6a8b23f3667f5e7262b3a19c607a37cb9eaf7d0e93eb826d45c26ec12df4810879bacb8e4042bb83cc80b2b436224c8d47b6d67361369a724bbf7ee  sysrq.conf
0e3f69170072f150f449cd9ccd9897728558a98acf2d5b85484ae8104b97f84e77760d7a30eeeccaf37d12cf870b3107ee34cefe55bdb033f3c7ac87d2093d24  uboot-script.cmd
a2fd8037c9a1af000f31c7005436d791525317f818c553d1e2f472b4df76c71d769ecbf67b369a478bea720a6215eaa9fb6a26ba218ac9410513e2345ebe7b4c  HiFi.conf
3bdf9d4c41058680d488d9b4a2ac5fd693ceddc5f319a577480546f8af7d9d64c9d3196e73cb0c00e793697eef4ad075c88c3d274702a5387c15945bc749d844  PineTab.conf
6374ef977eab14b71fa88057ebd52ca03035e387e5f41504598a990f24b2e92bccd2db476c545dc541000e08fccdecb6f1cd63e25b5835df706269e0384dacc7  weston.ini
ed0e03f7e1c0348c06be9a7922414469245604ce813bbf0c8910ef211cb993bbb0d0d4615c9c961d50712415f85d6b73e0cf02780ac784666aac56e4889341e4  UPower.conf
"
