#!/usr/bin/env bash
#
# Render the PKGBUILD for the `geolibre-bin` AUR package.
#
# This is a binary AUR package: it repackages the Linux `.deb` that the release
# workflow already attaches to each GitHub release (it does not build from
# source), so installs are fast and need no Rust/Node toolchain. The `.deb`
# already carries the binary, the `.desktop` entry, and the icons, so extracting
# it gives full desktop integration.
#
# Usage:
#   VERSION=1.5.0 \
#   SHA256_AMD64=<sha256 of GeoLibre.Desktop_<version>_amd64.deb> \
#   scripts/render-aur-pkgbuild.sh > PKGBUILD
#
# Both variables are required. REPO defaults to opengeos/GeoLibre. The rendered
# PKGBUILD is written to stdout.
set -euo pipefail

: "${VERSION:?Set VERSION to the release version, e.g. 1.5.0}"
: "${SHA256_AMD64:?Set SHA256_AMD64 to the sha256 of the amd64 .deb}"

# Validate formats so a truncated hash or stray metacharacter can't silently
# produce a malformed PKGBUILD that only fails at install time.
[[ "$SHA256_AMD64" =~ ^[0-9a-f]{64}$ ]] || { echo "SHA256_AMD64 is not a 64-char sha256 hex string" >&2; exit 1; }
# Anchored at both ends: a suffix like 1.2.3-rc1 must NOT pass, because AUR
# pkgver forbids hyphens (the pkgver/pkgrel separator), so makepkg would reject
# the resulting PKGBUILD.
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "VERSION does not look like a semver string" >&2; exit 1; }

REPO="${REPO:-opengeos/GeoLibre}"

# Shell-expanded: ${VERSION}, ${SHA256_AMD64}, ${REPO}. Everything escaped with
# \$ stays literal so it is a normal makepkg variable in the emitted PKGBUILD.
cat <<PKGBUILD
# Maintainer: Qiusheng Wu <giswqs@gmail.com>
#
# Generated by scripts/render-aur-pkgbuild.sh and republished to the AUR by
# .github/workflows/release.yml on every non-prerelease GitHub release. Edit the
# render script rather than this file; CI overwrites it on the next release.
pkgname=geolibre-bin
pkgver=${VERSION}
pkgrel=1
pkgdesc="Lightweight, cloud-native GIS platform for visualizing and analyzing geospatial data"
arch=('x86_64')
url="https://geolibre.app/"
license=('MIT')
depends=('webkit2gtk-4.1' 'gtk3' 'libayatana-appindicator')
optdepends=('xdotool: global keyboard shortcuts')
provides=('geolibre')
conflicts=('geolibre')
options=('!strip' '!debug')
source=("\${pkgname}-\${pkgver}.deb::https://github.com/${REPO}/releases/download/v\${pkgver}/GeoLibre.Desktop_\${pkgver}_amd64.deb")
sha256sums=('${SHA256_AMD64}')
noextract=("\${pkgname}-\${pkgver}.deb")

package() {
	cd "\${srcdir}"
	# A .deb is an \`ar\` archive of control/data tarballs. Unpack it, then
	# extract the data tree (gz/xz/zst, whichever tauri used; bsdtar detects
	# it) straight into the package root, preserving the binary, the .desktop
	# entry, and the icons the .deb already ships.
	bsdtar -xf "\${pkgname}-\${pkgver}.deb"
	# Capture the glob so a corrupt .deb with no data member fails loudly here
	# instead of bsdtar choking on the literal string "data.tar.*".
	local data_tar=(data.tar.*)
	[[ -f \${data_tar[0]} ]] || { echo "no data.tar.* member in the .deb" >&2; return 1; }
	bsdtar -xpf "\${data_tar[0]}" -C "\${pkgdir}/"
}
PKGBUILD
