# Reference: <https://postmarketos.org/devicepkg>
# Maintainer:  Clayton Craft <clayton@craftyguy.net>
# Co-Maintainer: Alistair Francis <alistair@alistair23.me>
# Co-Maintainer: Newbyte <newbyte@postmarketos.org>
pkgname=device-purism-librem5
pkgdesc="Purism Librem 5 Phone"
pkgver=1.42
pkgrel=0
_confver=56
_purismrel=1
# <librem5-base ver>.<purism release>
_purismver=${_confver}pureos$_purismrel
url="https://postmarketos.org"
license="MIT"
arch="aarch64"
options="!check !archcheck"
depends="
	alsa-ucm-conf
	gnss-share
	i2c-tools
	iw
	linux-purism-librem5
	mesa-egl
	postmarketos-base
	u-boot-librem5
	u-boot-tools
"
makedepends="devicepkg-dev"
builddir=$srcdir/librem5-base-pureos-$_confver
source="
	https://source.puri.sm/Librem5/librem5-base/-/archive/pureos/$_purismver/librem5-base-pureos-$_purismver.tar.gz
	00-mesa.sh
	70-wifi-pm.rules
	77-mm-broadmobi-port-types.rules
	deviceinfo
	flash_script.lst
	modprobe.d_rsi.conf
	modules-load.d_librem5.conf
	pcscd-librem5-reader.conf
	pcscd-librem5.initd
	uboot-script.cmd
	xorg.conf
	70-librem5-xwayland-accel.sh
	0001-librem5-pm.udev-disable-PM-for-broadmobi-modem.patch
	0002-udev-cameras-use-correct-path-for-modprobe-on-Alpine.patch
	l5-ship-mode.initd
	l5-lockdown-support.initd
	org.freedesktop.GeoClue2.service
"
subpackages="
	$pkgname-camera
	$pkgname-geoclue
	$pkgname-gnome
	$pkgname-nonfree-firmware:nonfree_firmware
	$pkgname-pulseaudio
	$pkgname-smartcard
	$pkgname-tinydm
	$pkgname-usbguard
	$pkgname-x11
"
install="$pkgname.post-install $pkgname.post-upgrade"

builddir="$srcdir/librem5-base-pureos-$_purismver"

build() {
	mkimage -A arm64 -C none -O linux -T script -d "$srcdir/uboot-script.cmd" "$srcdir/boot.scr"
	devicepkg_build $startdir $pkgname
}

geoclue() {
	pkgdesc="geoclue configuration for the Librem 5"
	install_if="$pkgname geoclue"
	replaces="geoclue"
	install -Dm644 "$srcdir"/org.freedesktop.GeoClue2.service \
		-t "$subpkgdir"/usr/share/dbus-1/system-services/
}

gnome() {
	install_if="$pkgname=$pkgver-r$pkgrel postmarketos-base-ui-gnome"
	depends="
		wys
	"
	install -D -m644 "$builddir"/default/org.freedesktop.ModemManager1.pkla \
		"$subpkgdir"/var/lib/polkit-1/localauthority/10-vendor.d/org.freedesktop.ModemManager1.pkla
}

camera() {
	install_if="$pkgname=$pkgver-r$pkgrel postmarketos-default-camera"
	depends="millipixels"
	mkdir -p "$subpkgdir"
}

pulseaudio() {
	pkgdesc="Pulseaudio support for the Librem 5"
	install_if="$pkgname pulseaudio"
	# install config files from upstream Purism
	mkdir -p "$subpkgdir"/etc/pulse/
	cp -r "$builddir"/default/audio/pulse/daemon.conf.d "$subpkgdir"/etc/pulse/
	install -D -m644 "$builddir"/default/audio/pulse/librem5.pa "$subpkgdir"/etc/pulse/librem5.pa
	mkdir -p "$subpkgdir"/usr/share/pulseaudio/alsa-mixer
	cp -r "$builddir"/default/audio/profile-sets "$subpkgdir"/usr/share/pulseaudio/alsa-mixer/
}

tinydm() {
	pkgdesc="Tinydm config for the Librem 5"
	install_if="$pkgname tinydm"
	install -Dm755 "$srcdir"/70-librem5-xwayland-accel.sh \
		"$subpkgdir"/etc/tinydm.d/env-wayland.d/70-librem5-xwayland-accel.sh
}

smartcard() {
	pkgdesc="OpenPGP SmartCard support for the Librem 5"
	depends="$pkgname ccid gnupg-scdaemon pcsc-lite pcsc-tools"
	install_if="$pkgname pcsc-tools"
	install="$install $subpkgname.post-install"

	install -D -m644 "$srcdir"/pcscd-librem5-reader.conf \
		"$subpkgdir"/etc/reader.conf.d/librem5-ttxs-reader

	install -D -m755 "$srcdir"/pcscd-librem5.initd \
		"$subpkgdir"/etc/init.d/pcscd-librem5
}

usbguard() {
	pkgdesc="USB guard support for the Librem 5"
	install_if="$pkgname usbguard"
	install -Dm600 "$builddir"/default/usbguard/librem5.conf \
		-t "$subpkgdir"/etc/usbguard/rules.d/
}

x11() {
	install_if="$pkgname xorg-server"
	install -Dm644 "$srcdir"/xorg.conf \
		"$subpkgdir"/etc/X11/xorg.conf.d/10-video.conf
}

package() {
	install -D -m644 "$srcdir"/boot.scr \
		"$pkgdir"/boot/boot.scr
	install -D -m644 "$srcdir"/modprobe.d_rsi.conf "$pkgdir"/etc/modprobe.d/rsi.conf
	install -D -m644 "$srcdir"/modules-load.d_librem5.conf "$pkgdir"/etc/modules-load.d/librem5.conf
	install -D -m644 "$srcdir"/00-mesa.sh "$pkgdir"/etc/profile.d/00-mesa.sh
	install -D -m644 "$srcdir"/flash_script.lst "$pkgdir"/usr/share/uuu/flash_script.lst

	# udev
	for f in "$builddir"/debian/*.udev; do
		# rename the udev rules, dropping the default prefix and (arbitrarily...) using "85-"
		install -Dm644 "$f" \
			"$pkgdir"/usr/lib/udev/rules.d/85-"$(basename ${f//librem5-base-defaults./}).rules"
	done

	# Wifi / PM
	install -Dm644 "$srcdir"/70-wifi-pm.rules -t "$pkgdir"/usr/lib/udev/rules.d/

	# install audio config files from Purism
	install -Dm644 "$builddir"/default/audio/ucm2/simple-card/Librem\ 5.conf \
		"$pkgdir"/usr/share/alsa/ucm2/NXP/iMX8/Librem_5/Librem\ 5.conf
	install -Dm644 "$builddir"/default/audio/ucm2/simple-card/HiFi.conf \
		"$pkgdir"/usr/share/alsa/ucm2/NXP/iMX8/Librem_5/HiFi.conf
	mkdir -p "$pkgdir"/usr/share/alsa/ucm2/conf.d/simple-card
	# relative path seems to be required or else alsa-lib can't figure it out
	ln -s ../../NXP/iMX8/Librem_5/Librem\ 5.conf \
		"$pkgdir"/usr/share/alsa/ucm2/conf.d/simple-card/Librem\ 5.conf

	# ship mode service
	install -Dm755 "$builddir"/default/shipmode/l5-poweroff-shipmode \
		-t "$pkgdir"/usr/sbin/

	install -D -m755 "$srcdir"/l5-ship-mode.initd \
		"$pkgdir"/etc/init.d/l5-ship-mode

	# lockdown support
	install -Dm755 "$builddir"/default/lockdown-support/lockdown-support.sh \
		-t "$pkgdir"/usr/sbin/
	install -Dm755 "$builddir"/default/lockdown-support/modprobe.d/librem5-lsm9ds1.conf \
		-t "$pkgdir"/etc/modprobe.d/
	install -Dm755 "$srcdir"/l5-lockdown-support.initd \
		"$pkgdir"/etc/init.d/l5-lockdown-support

	devicepkg_package $startdir $pkgname
}

nonfree_firmware() {
	pkgdesc="Wifi and Bluetooth firmware"
	depends="firmware-siliconlabs-rs9116 firmware-brcm43752"
	mkdir "$subpkgdir"
}

sha512sums="
d241cceff51ffbcbddb6134ddb9752150c063346ef49805e092acb55ace174c3dbc7732f6e2254b68652e0eb1aaee2a96ac6955554a26a100c3a9960bf7e96c9  librem5-base-pureos-56pureos1.tar.gz
2e72d9a084ecd6c96cafbbd8fda461ae8b2ebd14ffda601d9f36d73f08f04088213de17da078293ddd958d4c76d33148c7af6e252eb0709f6934855a55578509  00-mesa.sh
8b6fa7b12c15f439c561901f3ffe24a1ef2a0c343401c0a79281060854ab29e08a1fb5e7adae8684b12aa9f535ed99e2dad993dce9724822cb0fbf2497dd86e5  70-wifi-pm.rules
00355d9ad085a77e157f3c62eb4f1bcac2823d3b341e34d1128edbfb1444fba83e8eacd263ea61da9f6d853f1328ab7fbe5ea63bd08c8a1bc9b8eaf44ae1f1c6  77-mm-broadmobi-port-types.rules
3a5ded0cc86a0c3cb0b1fd91816a0b3479ab8eb8eae09494b7db5b778fc73e449bec29d983930e0b1425826a47fd91c3f093e1aff7eb877f7993701ea5cca70c  deviceinfo
ccb47fd421c5e35a8def8e6fb61dad99e42cf542690068a68f90c257e41d33f45f1069db0a8d89372d9029681812957033995639a6a8a5e3eed48019325e0f4d  flash_script.lst
3f62c04eb92d850e32d5e1b17534dec753563342ea6c4f9dc2572700d4f6eeb8ae8cd8e8e4f1b0f5c61f21d87bc61c02212f27313986157220003d78e2d69505  modprobe.d_rsi.conf
a0740e405781ec6ef765fdc9f5700c95adbb241c45c97b9384ba68cdece662cb216ca5918640042f6e65a5d1b1a9099936382c99b49a44303f7b6d77a075a471  modules-load.d_librem5.conf
8ef4ec7a9e9a4fb801d207e7d94b627aacc9243a93b0ec28e0823a6f44b8102416c040e4ddf499f87bb9dfa960cf6f627b008b79f197f6814b3fd881e18ae8ef  pcscd-librem5-reader.conf
2618b40eebd3602066ee75ab85cbdbb4cfee498def9879c95a304f05a6eeedf1ab28c5dd9fa5efc8f88c790f49c96458c61d5dfe8a3ca0f96bcc0e244313ab01  pcscd-librem5.initd
659c1801b6172c137db091a5850b467b5c667a5d0392c3d4f9cd767916ded20f09a494ad3c07cae09e9151909aca13208d3d0c681e9d4ec15681078c7046444b  uboot-script.cmd
1cbc65cf2cb8fad4b0332d012a0256a266205b9b64371f5c0dae09dbb7f1e45b254e8c43993b896097e9d7035a72d547d9bce1ab2133addc56a5abdb80b44616  xorg.conf
fad386071fc45f9880a94cc8004fabd5d98b3e6a12606c98d8e72e0208f22ab54f03b921f5967a959782507e2bff67d2fa92e46d8cd242011a3afcdbda529279  70-librem5-xwayland-accel.sh
dcb97b75336e7a3b2933091e483313fcab464cb1820378a6bb45b245cb4c1fd5485630ba9c18bce9b79950a734470f5a1fc858745030267bc601666ad3409edb  0001-librem5-pm.udev-disable-PM-for-broadmobi-modem.patch
b45749fcddfd65a302b502bcabfa5828d1b63e45d844fd50e44009a756b919db20fe9526102947c0f0dfcdffe4a037e5050fe1b24fb915933ce0837f49759378  0002-udev-cameras-use-correct-path-for-modprobe-on-Alpine.patch
c723764d507ccd4b04aa5ce79b1d1ebc131873cdc37a943cb0f92b8a08a3c69eb9e0d31fcd4589fd8c40a9676bae9897e4f12ec2ebe4db8214ac85669ef35a53  l5-ship-mode.initd
f4b314d553655f7975445ac0a33099923e20dabb082c07e2cf6a78e4118c143d777dd4ad2c09cbc388600bdefe8deb3073fbd15b238772ddf09c66ac46415590  l5-lockdown-support.initd
fd91bcd8cecd509587f0a26d454157a1df094d2d86c75329d1002e9474946494d4d1b5407e055fbd5f21e22394eeb1bbd4b3439ae6925db50a3427b4fac101cf  org.freedesktop.GeoClue2.service
"
