#!/bin/sh
# Agenium on a new machine, in one line:
#
#   curl -fsSL https://get.agenium.dev | sh
#
# Installs uv when it is missing, the manager `agenium` as a uv tool, refreshes
# Agenium's own signed catalogue (the source `agenium`), and installs and allows the
# system capabilities from it. When a store is connected, it also enables them for
# every agent (Global); without one it says how to connect one. Running it again
# changes nothing that is already in place, and updates the manager.
#
# It touches only Agenium's own names - the command `agenium`, ~/.agenium,
# ~/.config/agenium and ~/.cache/agenium - and nothing of another installation.
set -eu

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

command -v curl >/dev/null 2>&1 || fail "curl is needed"

# uv: used where it is, installed by its own installer where it is not.
if ! command -v uv >/dev/null 2>&1; then
    for dir in "${XDG_BIN_HOME:-$HOME/.local/bin}" "$HOME/.cargo/bin"; do
        if [ -x "$dir/uv" ]; then PATH="$dir:$PATH"; fi
    done
fi
if ! command -v uv >/dev/null 2>&1; then
    say "Installing uv ..."
    curl -LsSf https://astral.sh/uv/install.sh | sh
    PATH="${XDG_BIN_HOME:-$HOME/.local/bin}:$HOME/.cargo/bin:$PATH"
    command -v uv >/dev/null 2>&1 || fail "uv did not install; see https://docs.astral.sh/uv/"
fi
export PATH

say "Installing the Agenium manager ..."
uv tool install --quiet --upgrade agenium-manager
AGENIUM="$(uv tool dir --bin)/agenium"
[ -x "$AGENIUM" ] || fail "the manager is not at $AGENIUM"

say "Reading Agenium's catalogue ..."
"$AGENIUM" source refresh agenium >/dev/null

# The system capabilities are the ones the manager's own catalogue names as such.
SYSTEM="$("$AGENIUM" list | awk '$4 == "system" { print $1 }')"
[ -n "$SYSTEM" ] || fail "the manager names no system capabilities"

failed=""
for name in $SYSTEM; do
    if "$AGENIUM" install "$name" >/dev/null && "$AGENIUM" allow "$name" >/dev/null; then
        say "  $name: installed and allowed"
    else
        failed="$failed $name"
    fi
done

if "$AGENIUM" store check >/dev/null 2>&1; then
    for name in $SYSTEM; do
        case " $failed " in *" $name "*) continue ;; esac
        "$AGENIUM" enable "$name" --global >/dev/null || failed="$failed $name"
    done
    stored="yes"
else
    stored=""
fi

say ""
if [ -n "$failed" ]; then
    fail "could not set up:$failed - run the line again, or \`agenium install <name>\` to see why"
fi
if [ -n "$stored" ]; then
    say "Agenium is ready: the system capabilities are enabled for every agent."
    say "Make a folder an agent with \`agenium bootstrap\`, and see \`agenium help\`."
else
    say "Agenium is installed. It keeps agents, their policy and their records in a"
    say "PostgreSQL database, the store, and none is connected yet. Connect one:"
    say ""
    say "  agenium store set main --host <host> --user <user> --database <database> \\"
    say "      --password-file <file> --use"
    say ""
    say "Your own PostgreSQL, a Supabase project or a server of your own all serve. To"
    say "try Agenium on this machine, one command gives a local one, and one connects it:"
    say ""
    say "  docker run -d --name agenium-store -p 127.0.0.1:5432:5432 -e POSTGRES_USER=agenium \\"
    say "      -e POSTGRES_DB=agenium -e POSTGRES_HOST_AUTH_METHOD=trust postgres:17"
    say "  agenium store set local --host 127.0.0.1 --user agenium --database agenium \\"
    say "      --sslmode disable --use"
    say ""
    say "Then run this line again to enable the system capabilities for every agent."
    say "More on the store: https://pypi.org/project/agenium-manager/"
fi
if ! command -v agenium >/dev/null 2>&1; then
    say ""
    say "\`agenium\` is in $(uv tool dir --bin), which is not on your PATH yet:"
    say "run \`uv tool update-shell\` and open a new terminal."
fi
