# Contributor: Christian Ceelen <christian.ceelen+ux8406@gmail.com>
# Maintainer: Christian Ceelen <christian.ceelen+ux8406@gmail.com>
pkgname=asus-zenbook-duo-ux8406-second-screen
# The name of the thermal guard package; it is used in this one place.
_guard=asus-zenbook-duo-ux8406-tcc-guard
pkgver=0.5.0
pkgrel=0
pkgdesc="Lower panel of the Zenbook Duo UX8406 off while the keyboard lies on it"
url="https://github.com/cceelen/asus-zenbook-duo-ux8406"
arch="x86_64"
license="MIT"
makedepends="abuild-meson cargo glib-dev meson nodejs python3 samurai"
# The scripts of the -openrc subpackage are named here as well: abuild reads
# the install scripts of all subpackages from this list, by the name of the
# package each belongs to. They start the guard, and stop it, which puts the
# CPU thermal offset back.
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-deinstall
	$_guard-openrc.post-install $_guard-openrc.pre-deinstall"
# Alpine keeps udev files in -udev subpackages, and OpenRC scripts in -openrc
# ones, which are installed by themselves where udev or OpenRC is. What no
# subpackage takes stays in the main package: the panel's helper and its
# licence files.
subpackages="
	$pkgname-udev:panel_udev:noarch
	gnome-shell-extension-asus-zenbook-duo-ux8406-keys:gnome_keys:noarch
	gnome-shell-extension-builtin-screen-rotation:gnome_rotation:noarch
	$_guard:guard
	$_guard-openrc:guard_openrc
	$_guard-doc:guard_doc:noarch
	"
# The archive of the GitHub release (meson dist, with the crates in vendor/),
# which the release workflow attests. The container build puts the archive
# of the tree into SRCDEST, so abuild does not download it.
source="https://github.com/cceelen/asus-zenbook-duo-ux8406/releases/download/v$pkgver/asus-zenbook-duo-ux8406-$pkgver.tar.gz"
builddir="$srcdir/asus-zenbook-duo-ux8406-$pkgver"

# abuild-meson is Alpine's wrapper for `meson setup`: it sets the prefix, the
# directories and the build type the way Alpine wants them. Alpine has no
# systemd, so the guard is installed with its OpenRC script. udev.pc is in
# eudev-dev, which is not needed for anything else here: the udev directory is
# given. The licence texts go to /usr/share/licenses/<package>/.
#
# The keyboard part (the HID-BPF program and its state helper) is not built:
# Alpine has no udev-hid-bpf, which loads the program. The GNOME extension
# still gives each touchscreen its panel; only its two display keys need the
# keyboard program. Refer to issue 43.
build() {
	abuild-meson \
		-Dinit=openrc \
		-Dkeyboard=false \
		-Dudevdir=/usr/lib/udev \
		-Dlicensedir=share/licenses \
		. output
	meson compile -C output
}

# As the ordinary user abuild builds as. No test is skipped: the timeout is
# raised because the Rust tests are built first.
check() {
	meson test -C output --no-rebuild --print-errorlogs -t 3
}

package() {
	DESTDIR="$pkgdir" meson install -C output --no-rebuild
	# Meson's list of the licence files it installed; not a licence file.
	rm -f "$pkgdir"/usr/share/licenses/depmf.json
}

panel_udev() {
	pkgdesc="$pkgdesc (udev rule)"
	license="MIT"
	install_if="$pkgname=$pkgver-r$pkgrel udev"

	amove usr/lib/udev/rules.d/90-asus-ux8406-second-screen.rules
}

gnome_keys() {
	pkgdesc="GNOME Shell extension for the display keys of the Zenbook Duo UX8406"
	license="MIT"
	depends="gnome-shell"

	amove usr/share/gnome-shell/extensions/asus-zenbook-duo-ux8406-keys@cceelen.github.io
	amove usr/share/licenses/gnome-shell-extension-asus-zenbook-duo-ux8406-keys
}

gnome_rotation() {
	pkgdesc="GNOME Shell extension that turns the built-in screens with the laptop"
	license="MIT"
	depends="gnome-shell iio-sensor-proxy"

	amove usr/share/gnome-shell/extensions/builtin-screen-rotation@cceelen.github.io
	amove usr/share/licenses/gnome-shell-extension-builtin-screen-rotation
}

guard() {
	pkgdesc="CPU thermal-offset guard for the ASUS Zenbook Duo UX8406"
	license="MIT"

	amove usr/sbin/asus-ux8406-tcc-guard
	# The licence and the notices of the crates in the binary stay with it,
	# not in the -doc package: the MIT licence wants them to accompany it.
	amove usr/share/licenses/$_guard
}

# Not default_doc: that would also take usr/share/licenses, which holds the
# licence files of every package that has not been split off yet.
guard_doc() {
	pkgdesc="CPU thermal-offset guard for the ASUS Zenbook Duo UX8406 (documentation)"
	license="MIT"
	install_if="docs $_guard=$pkgver-r$pkgrel"

	# The example configuration belongs to the guard, not to the panel.
	amove usr/share/doc/asus-ux8406-tcc-guard
	amove usr/share/man
	# abuild refuses uncompressed man pages.
	gzip -9n "$subpkgdir"/usr/share/man/man8/*.8
}

guard_openrc() {
	default_openrc
}
