#!/usr/bin/env bash
# Download, verify and install a prebuilt package. No user config or microphone changes.
set -euo pipefail
[[ "$(uname -m)" == x86_64 ]] || { echo 'Fab currently supports x86_64 Omarchy.' >&2; exit 1; }
command -v omarchy >/dev/null && command -v pacman >/dev/null || { echo 'This installer is for Omarchy.' >&2; exit 1; }
for program in curl jq sha256sum; do command -v "$program" >/dev/null || { echo "$program is required." >&2; exit 1; }; done
stage="$(mktemp -d)"
trap 'rm -rf "$stage"' EXIT
curl --fail --location --proto '=https' --tlsv1.2 'https://dl.fab.run/linux/latest.json' -o "$stage/manifest.json"
jq -e '.schema == 1 and .platform == "omarchy"' "$stage/manifest.json" >/dev/null
version="$(jq -er '.version' "$stage/manifest.json")"
file="$(jq -er '.assets.x86_64.filename' "$stage/manifest.json")"
url="$(jq -er '.assets.x86_64.url' "$stage/manifest.json")"
digest="$(jq -er '.assets.x86_64.sha256' "$stage/manifest.json")"
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][a-zA-Z0-9]+)*$ ]] && \
[[ "$file" == "fab-linux-$version-1-x86_64.pkg.tar.zst" ]] && \
[[ "$url" == "https://github.com/arach/fab-releases/releases/download/linux-v$version/$file" ]] && \
[[ "$digest" =~ ^[a-f0-9]{64}$ ]] || { echo 'Invalid Linux release manifest.' >&2; exit 1; }
curl --fail --location --proto '=https' --tlsv1.2 "$url" -o "$stage/$file"
printf '%s  %s\n' "$digest" "$stage/$file" | sha256sum --check --status || { echo 'Download checksum failed; nothing installed.' >&2; exit 1; }
sudo pacman -U --needed "$stage/$file"
/usr/bin/fab-linux-onboard
