#!/usr/bin/env bash
#
# Render the RPM .spec for the Fedora COPR `geolibre` package.
#
# Like the AUR package, this is a binary repackage: it does not build from
# source, it unpacks the official Tauri-built RPM attached to the GitHub release
# and re-emits it, plus an AppStream metainfo file and an app-id-named .desktop
# entry with proper categories. COPR builds the resulting SRPM in mock.
#
# Usage:
#   VERSION=1.5.0 DATE=2026-06-20 scripts/render-copr-spec.sh > geolibre.spec
#
# DATE is the release date (YYYY-MM-DD); it feeds the %changelog entry. Both
# variables are required. REPO defaults to opengeos/GeoLibre.
set -euo pipefail

: "${VERSION:?Set VERSION to the release version, e.g. 1.5.0}"
: "${DATE:?Set DATE to the release date, e.g. 2026-06-20}"

[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "VERSION does not look like a semver string" >&2; exit 1; }
[[ "$DATE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || { echo "DATE must be YYYY-MM-DD" >&2; exit 1; }
# Reject syntactically valid but impossible dates (e.g. 2026-13-40). `date -d`
# is GNU coreutils; these scripts run on Linux/CI (on macOS, use coreutils).
date -u -d "$DATE" +%F >/dev/null 2>&1 || { echo "DATE is not a valid calendar date" >&2; exit 1; }

REPO="${REPO:-opengeos/GeoLibre}"
# RPM %changelog wants "Wdy Mon D YYYY" with a space-padded day (%e, not %d), so
# single-digit days read "Jun  7" rather than "Jun 07" (what rpmlint expects).
CHANGELOG_DATE="$(date -u -d "$DATE" +"%a %b %e %Y")"

# Shell-expanded: ${VERSION}, ${DATE}, ${REPO}, ${CHANGELOG_DATE}. Everything
# escaped with \$ or \%{...} stays literal for rpmbuild.
cat <<SPEC
# Generated by scripts/render-copr-spec.sh; CI regenerates it per release.
%global appid org.geolibre.desktop

# This payload is a pre-built Tauri bundle, not something we compile, so turn
# off the post-build processing (strip, debuginfo, bytecompile, build-id links)
# that assumes a from-source build. Automatic library dependency generation is
# separate and stays on, so Requires on the webkit2gtk/gtk3 sonames are still
# discovered from the binary.
%global debug_package %{nil}
%global __os_install_post %{nil}
%global _build_id_links none

Name:           geolibre
Version:        ${VERSION}
Release:        1%{?dist}
Summary:        Lightweight, cloud-native GIS platform for visualizing and analyzing geospatial data

License:        MIT
URL:            https://geolibre.app/
ExclusiveArch:  x86_64

# Repackage the official release RPM rather than rebuilding from source.
Source0:        https://github.com/${REPO}/releases/download/v%{version}/GeoLibre.Desktop-%{version}-1.x86_64.rpm
Source1:        %{appid}.metainfo.xml
# The upstream bundle ships no license file, so carry the project LICENSE here.
Source2:        LICENSE

Provides:       geolibre-desktop = %{version}-%{release}
# The upstream release RPM (manually installed) is named geo-libre-desktop and
# drops the same files; declare the conflict for a clean dnf message.
Conflicts:      geo-libre-desktop

BuildRequires:  cpio
BuildRequires:  desktop-file-utils
BuildRequires:  appstream

%description
GeoLibre is a lightweight, cloud-native desktop GIS for visualizing and
analyzing geospatial data, built on MapLibre with deck.gl for 3D and
point-cloud overlays. It loads vector and raster files, web services, 3D Tiles,
MBTiles, and cloud-native formats (GeoParquet, FlatGeobuf, PMTiles, COG),
converting local data in the browser with DuckDB-WASM Spatial, and is
extensible through a plugin ecosystem.

%prep
# Nothing to unpack; the payload is handled in %install.

%build
# Nothing to build; this is a repackage of the upstream binary RPM.

%install
# Unpack the upstream RPM payload into the buildroot.
cd %{buildroot}
rpm2cpio %{SOURCE0} | cpio -idmv

# Rename the .desktop to the AppStream app-id (software centers and the metainfo
# launchable expect this), add the Categories the upstream bundle leaves empty,
# and give it a clean Comment. Find the source file rather than hardcoding its
# name, so an upstream rename fails loudly instead of silently dropping it.
_desktop_dir="%{buildroot}%{_datadir}/applications"
_desktop_count="\$(find "\$_desktop_dir" -name '*.desktop' | wc -l)"
[ "\$_desktop_count" -eq 1 ] || { echo "expected exactly one .desktop in the upstream RPM payload, found \$_desktop_count" >&2; exit 1; }
_desktop_src="\$(find "\$_desktop_dir" -name '*.desktop')"
mv "\$_desktop_src" "%{buildroot}%{_datadir}/applications/%{appid}.desktop"
desktop-file-edit \\
  --set-key=Categories --set-value="Science;Geoscience;Geography;" \\
  --set-key=Comment --set-value="Lightweight, cloud-native GIS platform" \\
  "%{buildroot}%{_datadir}/applications/%{appid}.desktop"

# Install the AppStream metainfo.
install -Dm0644 %{SOURCE1} "%{buildroot}%{_metainfodir}/%{appid}.metainfo.xml"

# Ship the license text (the upstream bundle omits it).
install -Dm0644 %{SOURCE2} "%{buildroot}%{_licensedir}/%{name}/LICENSE"

%check
desktop-file-validate "%{buildroot}%{_datadir}/applications/%{appid}.desktop"
appstreamcli validate --no-net "%{buildroot}%{_metainfodir}/%{appid}.metainfo.xml"

%files
%license %{_licensedir}/%{name}/LICENSE
%{_bindir}/geolibre-desktop
# The Tauri bundle hardcodes the /usr/lib path (lib, not lib64); quote it
# because of the space in the directory name.
"/usr/lib/GeoLibre Desktop/"
%{_datadir}/applications/%{appid}.desktop
%{_datadir}/icons/hicolor/*/apps/geolibre-desktop.png
%{_metainfodir}/%{appid}.metainfo.xml

%changelog
* ${CHANGELOG_DATE} Qiusheng Wu <giswqs@gmail.com> - ${VERSION}-1
- Automated release of GeoLibre ${VERSION} (repackaged from the upstream RPM).
SPEC
