# Reference: <https://postmarketos.org/devicepkg>
maintainer="snlavaise <snlavaise@gmail.com>"
pkgname=device-motorola-rtwo
pkgver=1
pkgrel=37
pkgdesc="Motorola Moto Edge 40 Pro"
url="https://postmarketos.org"
arch="aarch64"
license="MIT"
depends="
	android-tools
	busybox-extras
	dropbear
	firmware-motorola-rtwo-adsp
	firmware-motorola-rtwo-cdsp
	firmware-motorola-rtwo-gpu
	font-jetbrains-mono-nerd
	font-noto-emoji
	foot
	iw
	linux-firmware-ath12k
	linux-firmware-qcom
	linux-firmware-rtw88
	linux-postmarketos-qcom-sm8550
	networkmanager-cli
	postmarketos-base
	python3
	seatd-launch
	soc-qcom
	sway
	"
makedepends="devicepkg-dev"
source="
	deviceinfo
	modules-initfs
	initfs-hook.sh
	initfs-hook-cleanup.sh
	30-dropbear.files
	debug-telnetd.service
	logind-power-key.conf
	rtwo-panel-revive.sh
	rtwo-panel-revive.service
	rtwo-dashboard-launcher.py
	rtwo-dashboard.py
	rtwo-dashboard-sway.config
	rtwo-dashboard-foot.ini
	rtwo-dashboard-consola.ini
	rtwo-dashboard.service
	rtwo-wifi-5ghz-scan.sh
	rtwo-wifi-5ghz-scan.service
	"
options="!archcheck !check"

# TODO (as hardware comes up):
#  - WiFi (WCN7850/ath12k via pcie0): links up, enumerates fine, and the real
#    Motorola calibration (bdwlan.elf.rtwo.epa) is fully extracted and
#    verified byte-perfect -- but it ALWAYS fails at the QMI board-data
#    transfer itself (qmi failed to load board data file:-110, ETIMEDOUT),
#    confirmed a dead end after exhaustive investigation (re-verified
#    2026-09-22, no format/corruption issue found). Workaround: a generic
#    Qualcomm board-id=44 blob as the LEGACY board.bin (not board-2.bin) at
#    ath12k/WCN7850/hw2.0/board.bin gets wlan0 to appear, but hits a
#    separate, deeper WMI/HTC credit-exhaustion bug in ath12k_mac_start()
#    (PMF_QOS never gets a second tx credit) -- 7 hypotheses ruled out with
#    evidence, cross-referenced against a real LKML thread with the exact
#    same symptom on unrelated hardware. Looks like a genuine unfixed
#    upstream kernel bug, not something fixable from this device's side.
#    Draft bug report at ~/pmos/ath12k-bug-report-wmi-credit-exhaustion.md,
#    not sent (needs explicit go-ahead before any public post).
#  - USB WiFi dongle (RTL8723DU) as an alternative to onboard WCN7850:
#    CONFIG_RTW88_8723DU=m already enabled, needs linux-firmware-rtw88
#    (added below) for rtw88/rtw8723d_fw.bin. Requires &usb_1 in host mode
#    (now the default, see the .dts) + a self/externally-powered USB-C hub.
#  - Bluetooth (qcom,wcn7850-bt) not wired in the .dts yet, same PMU node.
#  - firmware-motorola-rtwo: ADSP/CDSP (remoteproc, pmic-glink battmgr/UCSI
#    run on the ADSP) and the GPU zap shader are pulled in as dependencies.
#    -hexagonfs (FastRPC libs for hexagonrpcd, e.g. sensors) is left out
#    until hexagonrpcd is actually set up. Modem blobs still TODO.
#  - GPU (Adreno 740): WORKING 2026-09-25 (freedreno FD740, OpenGL 4.6/GLES).
#    a740_sqe.fw + gmu_gen70200.bin come from linux-firmware-qcom; the zap
#    shader MUST be Motorola's signed one (firmware-motorola-rtwo-gpu,
#    qcom/sm8550/motorola/rtwo/a740_zap.mbn) -- the generic
#    qcom/sm8550/a740_zap.mbn in linux-firmware-qcom is QRD-signed and is
#    rejected on a retail device.
#  - make-dynpart-mappings once super partitions are mapped
#  - soc-qcom-modem, qbootctl (A/B slot handling) once booting

build() {
	devicepkg_build $startdir $pkgname
}

package() {
	devicepkg_package $startdir $pkgname

	# devicepkg_package handles initfs-hook.sh natively (installs it to
	# usr/share/mkinitfs/hooks/00-$pkgname.sh), but has no generic handler
	# for extra ".files" manifests (unlike modules-initfs) -- install this
	# one by hand, same mechanism postmarketos-mkinitfs-hook-debug-shell
	# uses for telnetd's own 20-debug-shell.files.
	install -Dm644 "$srcdir"/30-dropbear.files \
		"$pkgdir"/usr/share/mkinitfs/files/30-dropbear.files

	# Mata el dropbear del debug-shell antes de switch_root -- si no, sigue
	# vivo pegado al puerto 22 y sshd.service de la rootfs real nunca puede
	# bindear (ver comentario grande en el propio initfs-hook-cleanup.sh).
	install -Dm755 "$srcdir"/initfs-hook-cleanup.sh \
		-t "$pkgdir"/usr/share/mkinitfs/hooks-cleanup/

	# Debug telnet shell de la rootfs REAL (no del initramfs -- ver el
	# comentario grande en el propio .service). Se habilita a mano con el
	# symlink porque esto no es un paquete "de verdad" con postinstall,
	# es un hack de bring-up.
	install -Dm644 "$srcdir"/debug-telnetd.service \
		"$pkgdir"/usr/lib/systemd/system/debug-telnetd.service
	install -d "$pkgdir"/usr/lib/systemd/system/sysinit.target.wants
	ln -s ../debug-telnetd.service \
		"$pkgdir"/usr/lib/systemd/system/sysinit.target.wants/debug-telnetd.service

	# Boton de encendido: ignorar la pulsacion corta (ver el .conf).
	install -Dm644 "$srcdir"/logind-power-key.conf \
		"$pkgdir"/usr/lib/systemd/logind.conf.d/50-rtwo-power-key.conf

	# Panel negro en frio: blank/unblank desde el rootfs si el panel salio
	# muerto (el initfs-hook no alcanza cuando msm engancha el display
	# despues del switch_root). Ver rtwo-panel-revive.sh.
	install -Dm755 "$srcdir"/rtwo-panel-revive.sh \
		"$pkgdir"/usr/libexec/rtwo-panel-revive
	install -Dm644 "$srcdir"/rtwo-panel-revive.service \
		"$pkgdir"/usr/lib/systemd/system/rtwo-panel-revive.service
	install -d "$pkgdir"/usr/lib/systemd/system/multi-user.target.wants
	ln -s ../rtwo-panel-revive.service \
		"$pkgdir"/usr/lib/systemd/system/multi-user.target.wants/rtwo-panel-revive.service

	# Dashboard: sway siempre, con el dashboard en el panel (DSI-1) y una
	# consola de login en el monitor (DP-1) mientras haya uno conectado. El
	# launcher sigue el HPD de DP-1 y abre/cierra la consola; ver su docstring.
	install -Dm755 "$srcdir"/rtwo-dashboard-launcher.py \
		"$pkgdir"/usr/libexec/rtwo-dashboard-launcher
	install -Dm755 "$srcdir"/rtwo-dashboard.py \
		"$pkgdir"/usr/libexec/rtwo-dashboard
	install -Dm644 "$srcdir"/rtwo-dashboard-sway.config \
		"$pkgdir"/usr/share/rtwo-dashboard/sway.config
	install -Dm644 "$srcdir"/rtwo-dashboard-foot.ini \
		"$pkgdir"/usr/share/rtwo-dashboard/foot.ini
	install -Dm644 "$srcdir"/rtwo-dashboard-consola.ini \
		"$pkgdir"/usr/share/rtwo-dashboard/consola.ini
	install -Dm644 "$srcdir"/rtwo-dashboard.service \
		"$pkgdir"/usr/lib/systemd/system/rtwo-dashboard.service
	ln -s ../rtwo-dashboard.service \
		"$pkgdir"/usr/lib/systemd/system/multi-user.target.wants/rtwo-dashboard.service

	# WiFi 5 GHz tras el arranque: destraba el escaneo de wpa_supplicant
	# (workaround ath12k). La red sale de NetworkManager o de
	# /etc/default/rtwo-wifi-5ghz-scan; ver el script.
	install -Dm755 "$srcdir"/rtwo-wifi-5ghz-scan.sh \
		"$pkgdir"/usr/libexec/rtwo-wifi-5ghz-scan
	install -Dm644 "$srcdir"/rtwo-wifi-5ghz-scan.service \
		"$pkgdir"/usr/lib/systemd/system/rtwo-wifi-5ghz-scan.service
	ln -s ../rtwo-wifi-5ghz-scan.service \
		"$pkgdir"/usr/lib/systemd/system/multi-user.target.wants/rtwo-wifi-5ghz-scan.service
}

sha512sums="
bc4d018a852d900fd9932bf881e81b1eae49f416dbafe212a4fa6e26c05f812cef64ca19b8adb69225331468ac1535d568f547e2e9f4ea296ed37a8c6340e4ad  deviceinfo
298e836000c92db16ee4fe3ce4899eb13e9099983ee0fabff1e98c494e8a1883cef327a3ddce5c1ef98e0043b804aae8506438630b3bb144f92ded89c129668b  modules-initfs
eccadc2bf5713ff9df16234c0d9daf1364bdcd88fb9709df012035719e2d5e44d658ccaf87a9bb1a76d433861c37162cdfef999fbe1093d6d0d32eb849dbdfe8  initfs-hook.sh
cb8ea3e0da0219fdf77f6def6d7a77157a4ef152cc434e3d282457647784b05e7152cf1028d00a5534ae721a38d3c24047edd1dfbaa6fd34290c3823d4cf54c0  initfs-hook-cleanup.sh
07ceb61f64d99fdbc815795beae9014dc22177d5622d015db79849faed5c14225b280d83dbac174957089344d2e857595b046683cee033d4acd76fdbf7114eab  30-dropbear.files
3511918144a3130d9ac64df78cfd9a02e3ed5ee11f45f0b3b872f2e42504434b3c3197b47797fad2510ff1e7ae7822813a8710eb4830f324b5cb101742fde062  debug-telnetd.service
bab2fbd6078d9ac342708a1e85b7f5d5cff5e1105b5bd633517bf27f9a61499b9903c9aa17d0142c82f8cd874cd9ac03d1ddb33b8c66e40094a7271dd54c0502  logind-power-key.conf
59db456c9ba57352aa8bff0e8ca73bd5b1c5c7f6f9c57e382e5bdd68791c9324d705210e77aa4e945e3869ab11dc1066e1a290c3750a27772c89f83731d28609  rtwo-panel-revive.sh
8e374a7304f8648d33d9087149e53efc5b09ffe1f7bc07b8b10521b7b1163e4787b56ecf3d2871629b1cfcc0037c1c15d084b514412e5e0a90ff66fd8e83b088  rtwo-panel-revive.service
3dfc80a0399e06c9f9708847bfc3e270202e26ecac48e60c0ef8d98a98c9a92b20febb91a90201b6ebd00141c49ec7bd6ad052acfdba587749b71c1f3dd7798c  rtwo-dashboard-launcher.py
f9f51c52280e443010d3664bf390e5b9664c0f8b82bec39cd3e36b2fac742b27d6d2bbc32ee4a48d0296c2919eeee7b4728313cb2da69b598e6b593ea7b4a8a9  rtwo-dashboard.py
31c20c566be562ff6755cfb24915a94d9b3ad95a45e73fe71eda7e74851dd5fbfe20416052bda8adaa174243be9c63d098de9063c092637ec51061937f18ca3a  rtwo-dashboard-sway.config
92a3b263af1aed7f8f808ecc1f7815dc0951f21723c1d0edaeb5b57ae78e505371396824901a97d4521c710fc2f670ef71579bb96b35835726a381648adb8e21  rtwo-dashboard-foot.ini
c38b7942a7697788ffc62c82f560f6cf5e2f963b1d3ca69de8f9b2d1bfe8143e802cc19e782d01cb12c5ae0347e5a082fad2774e777b4a0696e2070e22004bb6  rtwo-dashboard-consola.ini
5fb089bdb6079ec790fb7c1aafad9c3f1a9aa90731c96d42c5bb788b7b44d485bc7a07befa86fba06eafbde9c38d4aaafb56bf752ec289ea5f817465982ffcbd  rtwo-dashboard.service
259ef3adc35e26f263e572a0937e4e701969eaa0c9c6a85cb13f66f3acc3e0395cf5639f882a0196a9881c3cdd7ac505a33f998296af7d11d34fff6e8dddd747  rtwo-wifi-5ghz-scan.sh
0c1bc164f71c10c8b24907d7c9ce91eba8c1983d7d00c5fc4e8c7d3d3b700cb746ad5bece5ba796903ffb3728d8ee108fc08fb95bc8dfe5e2c9311fa829de3d3  rtwo-wifi-5ghz-scan.service
"
