#!/usr/bin/env bash
set -euo pipefail

fail() {
    printf '%s\n' "$1" >&2
    if command -v kdialog >/dev/null && [[ -n ${DISPLAY:-} ]]; then
        kdialog --title 'Meowven OS installer' --error "$1" || true
    fi
    exit 1
}

[[ $(uname -m) == x86_64 ]] || fail 'This Meowven OS image installs on x86-64 PCs.'
[[ -r /run/archiso/bootmnt/meowven/x86_64/airootfs.sfs ]] ||
    fail 'Start the installer from the Meowven OS live USB.'
[[ -f /usr/lib/meowven/live-setup ]] || fail 'The Meowven live environment is missing.'
[[ ${XDG_SESSION_TYPE:-x11} == x11 && -n ${DISPLAY:-} ]] ||
    fail 'Log out and choose Meowven installer (X11), or Plasma (X11) on the Plasma edition, at the live login screen before opening the installer.'
command -v calamares >/dev/null || fail 'The Calamares installer package is missing.'

# Upstream's Polkit policy preserves DISPLAY and XAUTHORITY for this exact binary.
# Opening the wizard does not start its partitioning jobs.
exec pkexec /usr/bin/calamares
