#!/bin/sh
# Build Quick Setup from this source tree and install the same set of files
# the RPM spec installs, so the app can be launched for testing without
# packaging it. On bootc systems /usr is immutable, so we unlock it first.
#
# NOTE: `bootc usr-overlay` returns a non-zero exit code when the overlay is
# already mounted. That's expected, so this script intentionally does not
# guard against it.

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SOURCE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR="$SOURCE_DIR/builddir"

PREFIX="/usr"
BINDIR="$PREFIX/bin"
DATADIR="$PREFIX/share"
SCHEMADIR="$DATADIR/glib-2.0/schemas"
APPDATADIR="$DATADIR/quick-setup"
APPLICATIONSDIR="$DATADIR/applications"
SYSCONFDIR="/etc"
AUTOSTARTDIR="$SYSCONFDIR/xdg/autostart"

if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run as root (use sudo)." >&2
    exit 1
fi

echo "==> Unlocking /usr via bootc usr-overlay"
# Intentionally no error handling: `bootc usr-overlay` exits non-zero when the
# deployment is already unlocked, which is fine for our purposes.
bootc usr-overlay || true

echo "==> Configuring meson build in $BUILD_DIR"
if [ ! -d "$BUILD_DIR" ]; then
    meson setup "$BUILD_DIR" "$SOURCE_DIR" --prefix="$PREFIX"
else
    meson setup --reconfigure "$BUILD_DIR" "$SOURCE_DIR" --prefix="$PREFIX"
fi

echo "==> Building Quick Setup"
meson compile -C "$BUILD_DIR"

echo "==> Installing binary"
install -Dm 0755 "$BUILD_DIR/quick-setup" "$BINDIR/quick-setup"

echo "==> Installing helper scripts"
install -Dm 0755 "$SOURCE_DIR/scripts/quick-setup-should-run"    "$BINDIR/quick-setup-should-run"
install -Dm 0755 "$SOURCE_DIR/scripts/quick-setup-autostart"     "$BINDIR/quick-setup-autostart"
install -Dm 0755 "$SOURCE_DIR/scripts/quick-setup-driver-helper" "$BINDIR/quick-setup-driver-helper"
install -Dm 0755 "$SOURCE_DIR/scripts/first-boot"                 "$BINDIR/first-boot"

echo "==> Installing autostart desktop file"
install -Dm 0644 "$SOURCE_DIR/data/io.stillhq.quicksetup.desktop" \
    "$AUTOSTARTDIR/io.stillhq.quicksetup.desktop"
install -Dm 0644 "$SOURCE_DIR/data/io.stillhq.quicksetup.desktop" \
    "$APPLICATIONSDIR/io.stillhq.quicksetup.desktop"

echo "==> Installing GSettings schema"
install -Dm 0644 "$SOURCE_DIR/data/io.stillhq.quicksetup.gschema.xml" \
    "$SCHEMADIR/io.stillhq.quicksetup.gschema.xml"

echo "==> Installing polkit policy"
install -Dm 0644 "$SOURCE_DIR/data/io.stillhq.quicksetup.policy" \
    "$DATADIR/polkit-1/actions/io.stillhq.quicksetup.policy"

echo "==> Installing configuration files"
install -Dm 0644 "$SOURCE_DIR/data/page_config.json"         "$APPDATADIR/page_config.json"
install -Dm 0644 "$SOURCE_DIR/data/example_apps_config.json" "$APPDATADIR/example_apps_config.json"

if [ -d "$SOURCE_DIR/data/drivers" ]; then
    echo "==> Installing driver manifests"
    mkdir -p "$APPDATADIR/drivers"
    find "$SOURCE_DIR/data/drivers" -maxdepth 1 -type f -name '*.json' \
        -exec install -Dm 0644 {} "$APPDATADIR/drivers/" \;
fi

echo "==> Installing images"
install -Dm 0644 "$SOURCE_DIR/data/images/samdemo.png" \
    "$APPDATADIR/images/samdemo.png"

echo "==> Installing bundled symbolic icons"
install -Dm 0644 \
    "$SOURCE_DIR/data/icons-symbolic/hicolor/scalable/status/firmware-update-symbolic.svg" \
    "$APPDATADIR/icons-symbolic/hicolor/scalable/status/firmware-update-symbolic.svg"

if [ -d "$SOURCE_DIR/data/icons" ]; then
    echo "==> Installing icons"
    mkdir -p "$APPDATADIR/icons"
    find "$SOURCE_DIR/data/icons" -maxdepth 1 -type f -name '*.png' \
        -exec install -Dm 0644 {} "$APPDATADIR/icons/" \;
fi

echo "==> Recompiling GSettings schemas"
glib-compile-schemas "$SCHEMADIR"

echo "==> Reloading polkit so the new action takes effect"
# Polkit normally picks up new .policy files via inotify, but reloading the
# unit guarantees the action is registered before we launch Quick Setup.
if systemctl is-active --quiet polkit; then
    systemctl reload polkit 2>/dev/null || systemctl restart polkit
fi

if command -v pkaction >/dev/null 2>&1; then
    if pkaction --action-id io.stillhq.quicksetup.run-driver-helper >/dev/null 2>&1; then
        echo "    polkit action io.stillhq.quicksetup.run-driver-helper is registered."
    else
        echo "    WARNING: polkit did not pick up io.stillhq.quicksetup.run-driver-helper" >&2
    fi
fi

echo
echo "Quick Setup installed from source tree."
echo "Run it with: quick-setup"
