#!/usr/bin/env bash
# Install, update or remove the Hoard plugin for Decky Loader.
#
#   ./decky-install.sh              install from this folder, or, when the folder has
#                                   no built plugin, the build that matches the
#                                   installed Hoard client (`hoard --version`)
#   ./decky-install.sh --version 1.3.0-sm.123   that build instead
#   ./decky-install.sh --uninstall
#
# Run it from Desktop Mode, as your normal user (not with sudo): it asks for the
# password itself, because Decky's plugins folder belongs to root.
set -euo pipefail

# The forge's package registry: CI uploads a plugin build with every client build,
# under the same version (saves-manager-decky/<version>/).
REGISTRY="https://git.mighty.cb.it/api/packages/mighty/generic/saves-manager-decky"
NAME="hoard"
PLUGINS="$HOME/homebrew/plugins"
HERE="$(cd "$(dirname "$0")" && pwd)"

say() { printf '%s\n' "$*"; }
die() { printf 'decky-install: %s\n' "$*" >&2; exit 1; }

[ "$(id -u)" -ne 0 ] || die "run it as your normal user, without sudo"
[ -d "$PLUGINS" ] || die "Decky Loader is not installed (no $PLUGINS). Install it first: https://decky.xyz"

# On a Steam Deck the default user has no password until you set one, and
# without it sudo cannot ask for anything.
if ! sudo -n true 2>/dev/null; then
    say "Decky's plugin folder belongs to root, so this needs your password."
    say "On a Steam Deck with no password yet, set one first with: passwd"
    sudo -v || die "sudo is needed to write to $PLUGINS"
fi

restart_decky() {
    sudo systemctl restart plugin_loader 2>/dev/null \
        || say "Restart Decky (or reboot) to load the change."
}

if [ "${1:-}" = "--uninstall" ]; then
    sudo rm -rf "${PLUGINS:?}/$NAME"
    restart_decky
    say "Hoard plugin removed."
    exit 0
fi

VERSION=""
if [ "${1:-}" = "--version" ]; then
    VERSION="${2:-}"
    [ -n "$VERSION" ] || die "--version needs a version, like 1.3.0-sm.123"
fi

SRC="$HERE"
if [ -n "$VERSION" ] || [ ! -f "$SRC/plugin.json" ] || [ ! -f "$SRC/dist/index.js" ]; then
    if [ -z "$VERSION" ]; then
        # The plugin talks to hoardd over its socket, so it is taken from the same
        # build as the client installed here.
        HOARD="$(command -v hoard || echo "$HOME/.local/bin/hoard")"
        [ -x "$HOARD" ] || die "Hoard is not installed here; install it first, or pass --version"
        VERSION="$("$HOARD" --version | awk '{print $2}')"
        [ -n "$VERSION" ] || die "could not read the version of $HOARD"
    fi
    ASSET="hoard-decky-$VERSION.tar.gz"
    TMP="$(mktemp -d)"
    trap 'rm -rf "$TMP"' EXIT
    say "Downloading the Hoard plugin $VERSION..."
    curl -fsSL "$REGISTRY/$VERSION/$ASSET" -o "$TMP/$ASSET" \
        || die "could not download $ASSET (no plugin build for $VERSION in the registry?)"
    curl -fsSL "$REGISTRY/$VERSION/$ASSET.sha256" -o "$TMP/$ASSET.sha256" \
        || die "could not download the checksum of $ASSET"
    ( cd "$TMP" && sha256sum -c --quiet "$ASSET.sha256" ) || die "$ASSET does not match its checksum"
    tar -xzf "$TMP/$ASSET" -C "$TMP"
    SRC="$TMP/hoard-decky"
    [ -f "$SRC/dist/index.js" ] || die "the download does not contain a built plugin"
fi

sudo rm -rf "${PLUGINS:?}/$NAME"
sudo mkdir -p "$PLUGINS/$NAME/dist"
sudo cp "$SRC/plugin.json" "$SRC/package.json" "$SRC/main.py" "$PLUGINS/$NAME/"
sudo cp "$SRC/dist/index.js" "$PLUGINS/$NAME/dist/"
[ -f "$SRC/LICENSE" ] && sudo cp "$SRC/LICENSE" "$PLUGINS/$NAME/"
restart_decky

say "Hoard plugin installed. Open the Quick Access menu (the ... button) and look under Decky."
if ! systemctl --user is-active --quiet hoard-sync 2>/dev/null; then
    say "Note: Hoard's sync service is not running here, so the plugin will show it as asleep."
    say "Install Hoard first if you have not (README, \"Install the client\")."
fi
