#!/bin/sh
# Pulsed Media AI credits (Virsilipas): set up your AI tools on this machine. Runs as you; needs no root.
# How to run it: https://virsilipas.mcx.fi/#install
# Built from install-src/ by tools/virsilipas/build-installers.php; edit the source, not this file.

VS_TOOLS="codex claude gemini opencode pi hermes qwen copilot crush goose kilo cline kimi vibe aider openhands openclaw zeroclaw picoclaw nullclaw ironclaw moltis pikkumies"
set -eu

ENDPOINT=https://virsilipas.mcx.fi/v1
KEYFILE="$HOME/.virsilipasKey"
BIN="$HOME/.local/bin"
SHARE="$HOME/.local/share/virsilipas"
WIKI=https://wiki.pulsedmedia.com/index.php/Using_Pulsed_Media_AI_Credits_on_Any_Linux_Server

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

# Fetch a URL to a file with whichever of wget or curl is installed.
fetch() {
	if command -v wget >/dev/null 2>&1; then wget -qO "$2" "$1"
	elif command -v curl >/dev/null 2>&1; then curl -fsSLo "$2" "$1"
	else die "Neither wget nor curl is installed."
	fi
}

# Major version of the installed Node.js, or 0.
node_major() { node -v 2>/dev/null | sed -n 's/^v\([0-9][0-9]*\).*/\1/p' | grep . || echo 0; }

# npm install into ~/.local (needs Node.js $2 or newer): npm_get <package> <min-node> <tool name>
npm_get() {
	[ "$(node_major)" -ge "$2" ] && command -v npm >/dev/null 2>&1 \
		|| die "$3 needs Node.js $2 or newer with npm. How to get it: $WIKI"
	say "Installing $3 with npm..."
	npm install -g --prefix "$HOME/.local" "$1" >/dev/null
}

# A wrapper's first lines: read the key from ~/.virsilipasKey into $key or stop.
WRAP_KEY='key=$(sed -n '"'"'s/^key=//p'"'"' "$HOME/.virsilipasKey" 2>/dev/null | head -n 1)
[ -n "$key" ] || { echo "no key in ~/.virsilipasKey: run the installer again" >&2; exit 1; }'

# Write ~/.local/bin/virsilipas-<tool> from stdin (the part after the key lines).
wrapper() {
	{ printf '#!/bin/sh\n# %s on your Pulsed Media AI credits (key from ~/.virsilipasKey).\n%s\n' "$2" "$WRAP_KEY"; cat; } >"$BIN/virsilipas-$1.new"
	chmod 755 "$BIN/virsilipas-$1.new"
	mv -f "$BIN/virsilipas-$1.new" "$BIN/virsilipas-$1"
}

# Arguments: an optional key (it starts with vsk-), then optional tool names.
vs_setup() {
	[ -n "${HOME:-}" ] && [ -d "$HOME" ] || die "HOME is not set to a directory."
	key=
	keyarg=no
	if [ $# -gt 0 ]; then
		case " $VS_TOOLS " in *" $1 "*) ;; *) key=$1; keyarg=yes; shift ;; esac
	fi
	TOOLS="$*"
	for t in $TOOLS; do
		case " $VS_TOOLS " in *" $t "*) ;; *) die "Unknown tool: $t. Tools: $VS_TOOLS" ;; esac
	done

	# The key: from the command line, else the existing ~/.virsilipasKey, else asked for on the terminal.
	if [ -z "$key" ] && [ -r "$KEYFILE" ]; then
		key=$(sed -n 's/^key=//p' "$KEYFILE" | head -n 1)
	fi
	if [ -z "$key" ]; then
		[ -r /dev/tty ] && { : </dev/tty; } 2>/dev/null || die "No key given and no terminal to ask on. Add your key at the end of the command."
		printf 'Your key (Pulsed Media client area, AI key page): ' >/dev/tty
		stty -echo </dev/tty 2>/dev/null || true
		IFS= read -r key </dev/tty || true
		stty echo </dev/tty 2>/dev/null || true
		printf '\n' >/dev/tty
	fi
	rest=${key#vsk-}
	case "$key" in vsk-*) ;; *) die "That is not a Virsilipas key (it starts with vsk-)." ;; esac
	case "$rest" in *[!A-Za-z0-9_-]*) die "That key has characters a key never has." ;; esac
	[ ${#rest} -ge 16 ] && [ ${#rest} -le 128 ] || die "That key has the wrong length."

	# Save it in the same file and format Pulsed Media seedboxes use, readable only by you.
	umask 077
	if [ "$(sed -n 's/^key=//p' "$KEYFILE" 2>/dev/null | head -n 1)" != "$key" ]; then
		printf 'endpoint=%s\nkey=%s\n' "$ENDPOINT" "$key" >"$KEYFILE.new.$$"
		chmod 600 "$KEYFILE.new.$$"
		mv -f "$KEYFILE.new.$$" "$KEYFILE"
		say "Saved your key in ~/.virsilipasKey."
	fi
	# A key given on the command line is visible to other users of this machine while this runs: when this
	# script is a file, start it again without the key. (Piped into a shell, there is no file to start again.)
	# (Piped, $0 is the shell's own name: never run a file of that name.)
	case "$0" in virsilipas*.sh|*/virsilipas*.sh) [ "$keyarg" = no ] || [ ! -f "$0" ] || exec sh "$0" $TOOLS ;; esac
	umask 022

	mkdir -p "$BIN" "$SHARE"
	work=$(mktemp -d)
	trap 'rm -rf "$work"' EXIT
	READY=
}

# Install the tool if it is missing, then point it at your credits. A wire_ step that cannot point the tool at the
# credits without changing your own setup says why and returns 1; the tool is then not listed as ready.
vs_tool() {
	if ! "have_$1"; then "install_$1"; fi
	if "wire_$1"; then READY="$READY $1"; fi
}

vs_done() {
	[ -n "$READY" ] || return 0
	for t in $READY; do eval "run=\${RUN_$t:-virsilipas-$t}"; say "Ready: $run"; done
	case ":$PATH:" in
		*":$BIN:"*) ;;
		*) say "Add ~/.local/bin to your PATH to run these by name: export PATH=\"\$HOME/.local/bin:\$PATH\"" ;;
	esac
}

# Universal: tools named on the command line are installed if missing; every other tool already on this
# machine is pointed at your credits too.
vs_universal() {
	missing=
	for t in $VS_TOOLS; do
		case " $TOOLS " in
			*" $t "*) vs_tool "$t" ;;
			*) if "have_$t"; then if "wire_$t"; then READY="$READY $t"; fi; else missing="$missing $t"; fi ;;
		esac
	done
	[ -n "$READY" ] || say "No AI tool found on this machine yet."
	vs_done
	[ -z "$missing" ] || say "To add one, run this again with its name at the end (after -s --):$missing"
}

# Codex: only release 0.93.0 works with the Virsilipas chat API (newer releases dropped wire_api="chat").
# A pin bump needs a live Virsilipas check first (same rule as PMSS aiToolsCodex.php).
have_codex() { [ -x "$SHARE/codex-0.93.0" ]; }

install_codex() {
	case "$(uname -m)" in
		x86_64|amd64) arch=x86_64; sum=3574eef71b062c17904b0761c397a97709ef28e99c616e2d1db261b2ea293d07 ;;
		aarch64|arm64) arch=aarch64; sum=8ae775950876be6867446a73762fcb9f484f4ef76e510b2f59eb0b3979be86ae ;;
		*) die "Codex has no build for $(uname -m)." ;;
	esac
	for tool in sha256sum tar; do command -v $tool >/dev/null 2>&1 || die "$tool is needed to install Codex."; done
	say "Downloading Codex 0.93.0..."
	fetch "https://github.com/openai/codex/releases/download/rust-v0.93.0/codex-$arch-unknown-linux-musl.tar.gz" "$work/codex.tar.gz"
	printf '%s  %s\n' "$sum" "$work/codex.tar.gz" | sha256sum -c - >/dev/null 2>&1 || die "The Codex download did not match its checksum; nothing was installed."
	tar -xzf "$work/codex.tar.gz" -C "$work"
	chmod 755 "$work/codex-$arch-unknown-linux-musl"
	mv -f "$work/codex-$arch-unknown-linux-musl" "$SHARE/codex-0.93.0"
}

wire_codex() {
	wrapper codex Codex <<'EOF'
VIRSILIPAS_API_KEY=$key
export VIRSILIPAS_API_KEY
exec "$HOME/.local/share/virsilipas/codex-0.93.0" \
	-c 'model_providers.virsilipas={name="Pulsed Media AI credits",base_url="https://virsilipas.mcx.fi/v1",env_key="VIRSILIPAS_API_KEY",wire_api="chat"}' \
	-c 'model_provider="virsilipas"' -c 'model="default"' "$@"
EOF
	# Plain "codex" too, when this machine has no codex of its own.
	if [ -L "$BIN/codex" ] && [ "$(readlink "$BIN/codex")" = virsilipas-codex ]; then return 0; fi
	if [ ! -e "$BIN/codex" ] && ! command -v codex >/dev/null 2>&1; then ln -sfn virsilipas-codex "$BIN/codex"; fi
}

# Claude Code: installed with its own official installer if missing; the wrapper points it at Virsilipas.
have_claude() { command -v claude >/dev/null 2>&1 || [ -x "$BIN/claude" ]; }

install_claude() {
	command -v bash >/dev/null 2>&1 || die "The Claude Code installer needs bash."
	say "Installing Claude Code with its official installer..."
	fetch https://claude.ai/install.sh "$work/claude-install.sh"
	bash "$work/claude-install.sh"
}

wire_claude() {
	wrapper claude 'Claude Code' <<'EOF'
claude=$(command -v claude 2>/dev/null || echo "$HOME/.local/bin/claude")
unset ANTHROPIC_API_KEY
ANTHROPIC_BASE_URL=https://virsilipas.mcx.fi ANTHROPIC_AUTH_TOKEN=$key
ANTHROPIC_MODEL=default ANTHROPIC_DEFAULT_OPUS_MODEL=default ANTHROPIC_DEFAULT_SONNET_MODEL=default
ANTHROPIC_DEFAULT_HAIKU_MODEL=default ANTHROPIC_SMALL_FAST_MODEL=default CLAUDE_CODE_AUTO_MODE_SERVER=${CLAUDE_CODE_AUTO_MODE_SERVER:-0}
export ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN ANTHROPIC_MODEL ANTHROPIC_DEFAULT_OPUS_MODEL \
	ANTHROPIC_DEFAULT_SONNET_MODEL ANTHROPIC_DEFAULT_HAIKU_MODEL ANTHROPIC_SMALL_FAST_MODEL CLAUDE_CODE_AUTO_MODE_SERVER
exec "$claude" "$@"
EOF
}

# Gemini CLI: installed with npm if missing (needs Node.js 20 or newer); the wrapper points it at Virsilipas.
have_gemini() { command -v gemini >/dev/null 2>&1 || [ -x "$BIN/gemini" ]; }

install_gemini() { npm_get @google/gemini-cli 20 'Gemini CLI'; }

wire_gemini() {
	# Gemini CLI reads a system-defaults file only from a root-owned folder, so the wrapper uses its own Gemini home
	# (GEMINI_CLI_HOME) with these settings; your own ~/.gemini is not touched.
	mkdir -p "$SHARE/gemini/.gemini"
	cat >"$SHARE/gemini/.gemini/settings.json" <<'EOF'
{
  "security": {"auth": {"selectedType": "gemini-api-key"}},
  "model": {"name": "default"},
  "modelConfigs": {"customOverrides": [{"match": {"overrideScope": "core"}, "modelConfig": {"model": "default"}}]},
  "agents": {"overrides": {"codebase_investigator": {"modelConfig": {"model": "inherit"}},
                           "cli_help": {"modelConfig": {"model": "inherit"}}}}
}
EOF
	wrapper gemini 'Gemini CLI' <<'EOF'
gemini=$(command -v gemini 2>/dev/null || echo "$HOME/.local/bin/gemini")
unset GOOGLE_API_KEY
GOOGLE_GEMINI_BASE_URL=https://virsilipas.mcx.fi GEMINI_API_KEY=$key
GEMINI_CLI_HOME="$HOME/.local/share/virsilipas/gemini"
export GOOGLE_GEMINI_BASE_URL GEMINI_API_KEY GEMINI_CLI_HOME
exec "$gemini" "$@"
EOF
}

# OpenCode: installed with npm (opencode-ai); the wrapper points it at Virsilipas with a custom-provider
# config (@ai-sdk/openai-compatible = Chat Completions, not @ai-sdk/openai which is the Responses API) in its
# own OPENCODE_CONFIG file, so your own ~/.config/opencode is untouched. First run downloads that AI SDK
# provider package from npm (~160MB, one time).
have_opencode() { command -v opencode >/dev/null 2>&1 || [ -x "$BIN/opencode" ]; }

install_opencode() { npm_get opencode-ai 20 OpenCode; }

wire_opencode() {
	mkdir -p "$SHARE/opencode"
	cat >"$SHARE/opencode/opencode.json" <<'EOF'
{
	"$schema": "https://opencode.ai/config.json",
	"provider": {
		"virsilipas": {
			"npm": "@ai-sdk/openai-compatible",
			"name": "Pulsed Media AI credits",
			"options": {
				"baseURL": "https://virsilipas.mcx.fi/v1",
				"apiKey": "{env:VIRSILIPAS_KEY}"
			},
			"models": {
				"default": {"name": "Pulsed Media default"},
				"large": {"name": "Pulsed Media large"}
			}
		}
	},
	"model": "virsilipas/default"
}
EOF
	wrapper opencode OpenCode <<'EOF'
opencode=$(command -v opencode 2>/dev/null || echo "$HOME/.local/bin/opencode")
VIRSILIPAS_KEY=$key
OPENCODE_CONFIG="$HOME/.local/share/virsilipas/opencode/opencode.json"
export VIRSILIPAS_KEY OPENCODE_CONFIG
exec "$opencode" "$@"
EOF
}

# Pi coding agent: installed with npm if missing (needs Node.js 22.19 or newer); the wrapper points it at
# Virsilipas through its own agent directory (PI_CODING_AGENT_DIR), so your own ~/.pi is untouched.
have_pi() { command -v pi >/dev/null 2>&1 || [ -x "$BIN/pi" ]; }

install_pi() { npm_get @earendil-works/pi-coding-agent 22 Pi; }

wire_pi() {
	mkdir -p "$SHARE/pi/agent"
	cat >"$SHARE/pi/agent/models.json" <<'EOF'
{
  "providers": {
    "virsilipas": {
      "baseUrl": "https://virsilipas.mcx.fi/v1",
      "api": "openai-completions",
      "apiKey": "$VIRSILIPAS_API_KEY",
      "models": [{"id": "default"}, {"id": "large"}]
    }
  }
}
EOF
	cat >"$SHARE/pi/agent/settings.json" <<'EOF'
{"defaultProvider": "virsilipas", "defaultModel": "default"}
EOF
	wrapper pi Pi <<'EOF'
pi=$(command -v pi 2>/dev/null || echo "$HOME/.local/bin/pi")
VIRSILIPAS_API_KEY=$key
PI_CODING_AGENT_DIR="$HOME/.local/share/virsilipas/pi/agent"
export VIRSILIPAS_API_KEY PI_CODING_AGENT_DIR
exec "$pi" "$@"
EOF
}

# Hermes Agent (NousResearch): official installer, downloaded then run; isolated from your own ~/.hermes via
# HERMES_HOME. The wrapper's config.yaml points a custom provider at Virsilipas with Chat Completions and model
# "default" (never codex_responses). Needs git; on glibc Linux it also needs libatomic1 for the managed Node.js.
have_hermes() { command -v hermes >/dev/null 2>&1 || [ -x "$BIN/hermes" ]; }

install_hermes() {
	command -v git >/dev/null 2>&1 || die "Hermes Agent needs git (Debian and Ubuntu: apt install git libatomic1)."
	say "Installing Hermes Agent (this downloads about 500MB: Python, Node.js, ripgrep, FFmpeg)..."
	fetch https://hermes-agent.nousresearch.com/install.sh "$work/hermes-install.sh"
	HERMES_HOME="$SHARE/hermes" bash "$work/hermes-install.sh" --non-interactive --skip-browser --skip-computer-use
}

wire_hermes() {
	mkdir -p "$SHARE/hermes"
	cat >"$SHARE/hermes/config.yaml" <<EOF
model:
  default: default
  provider: custom
  base_url: https://virsilipas.mcx.fi/v1
  api_mode: chat_completions
  key_env: VIRSILIPAS_KEY
EOF
	wrapper hermes 'Hermes Agent' <<'EOF'
hermes=$(command -v hermes 2>/dev/null || echo "$HOME/.local/bin/hermes")
VIRSILIPAS_KEY=$key
HERMES_HOME="$HOME/.local/share/virsilipas/hermes"
export VIRSILIPAS_KEY HERMES_HOME
exec "$hermes" "$@"
EOF
}

# Qwen Code: installed with npm if missing (needs Node.js 22 or newer; the npm registry is pinned here because
# Qwen's own installer falls back to a China mirror otherwise). The wrapper points it at Virsilipas with plain
# OPENAI_* env vars and --auth-type openai, so your own ~/.qwen is never written to.
have_qwen() { command -v qwen >/dev/null 2>&1 || [ -x "$BIN/qwen" ]; }

install_qwen() {
	[ "$(node_major)" -ge 22 ] && command -v npm >/dev/null 2>&1 \
		|| die "Qwen Code needs Node.js 22 or newer with npm. How to get it: $WIKI"
	say "Installing Qwen Code with npm..."
	npm install -g --prefix "$HOME/.local" --registry https://registry.npmjs.org @qwen-code/qwen-code >/dev/null
}

wire_qwen() {
	wrapper qwen 'Qwen Code' <<'EOF'
qwen=$(command -v qwen 2>/dev/null || echo "$HOME/.local/bin/qwen")
OPENAI_API_KEY=$key
OPENAI_BASE_URL=https://virsilipas.mcx.fi/v1
OPENAI_MODEL=default
export OPENAI_API_KEY OPENAI_BASE_URL OPENAI_MODEL
exec "$qwen" --auth-type openai "$@"
EOF
}

# GitHub Copilot CLI: installed with npm if missing (needs Node.js 22 or newer). The wrapper's bring-your-own-key
# env vars point it at Virsilipas; GitHub login is not required when a custom model provider is configured.
have_copilot() { command -v copilot >/dev/null 2>&1 || [ -x "$BIN/copilot" ]; }

install_copilot() { npm_get @github/copilot 22 'GitHub Copilot CLI'; }

wire_copilot() {
	wrapper copilot 'GitHub Copilot CLI' <<'EOF'
copilot=$(command -v copilot 2>/dev/null || echo "$HOME/.local/bin/copilot")
COPILOT_PROVIDER_BASE_URL=https://virsilipas.mcx.fi/v1
COPILOT_PROVIDER_TYPE=openai
COPILOT_PROVIDER_API_KEY=$key
COPILOT_MODEL=default
export COPILOT_PROVIDER_BASE_URL COPILOT_PROVIDER_TYPE COPILOT_PROVIDER_API_KEY COPILOT_MODEL
exec "$copilot" "$@"
EOF
}

# Crush (Charm): installed with npm (@charmland/crush); the wrapper points it at Virsilipas with an
# openai-compat provider in its own crushrc, loaded from a separate CRUSH_GLOBAL_CONFIG/CRUSH_GLOBAL_DATA pair
# so your own ~/.config/crush and ~/.local/share/crush are untouched.
have_crush() { command -v crush >/dev/null 2>&1 || [ -x "$BIN/crush" ]; }

install_crush() { npm_get @charmland/crush 20 Crush; }

wire_crush() {
	mkdir -p "$SHARE/crush"
	cat >"$SHARE/crush/crushrc" <<'EOF'
provider add virsilipas --type openai-compat --base-url "https://virsilipas.mcx.fi/v1" --api-key "$VIRSILIPAS_KEY"
model add virsilipas/default --name "Pulsed Media default"
model add virsilipas/large --name "Pulsed Media large"
EOF
	wrapper crush Crush <<'EOF'
crush=$(command -v crush 2>/dev/null || echo "$HOME/.local/bin/crush")
VIRSILIPAS_KEY=$key
CRUSH_GLOBAL_CONFIG="$HOME/.local/share/virsilipas/crush"
CRUSH_GLOBAL_DATA="$HOME/.local/share/virsilipas/crush-data"
export VIRSILIPAS_KEY CRUSH_GLOBAL_CONFIG CRUSH_GLOBAL_DATA
exec "$crush" "$@"
EOF
}

# Goose (Block/AAIF): official release-binary installer, downloaded then run with CONFIGURE=false so it never
# starts the interactive wizard. Wired entirely through env vars (Goose has no config-dir override env var, so
# this never touches your own ~/.config/goose/config.yaml, which the installer's CONFIGURE=false also never writes).
have_goose() { command -v goose >/dev/null 2>&1 || [ -x "$BIN/goose" ]; }

install_goose() {
	for tool in curl tar bzip2; do command -v $tool >/dev/null 2>&1 || die "Goose needs $tool."; done
	say "Installing Goose..."
	fetch https://github.com/aaif-goose/goose/releases/download/stable/download_cli.sh "$work/goose-install.sh"
	GOOSE_BIN_DIR="$BIN" CONFIGURE=false bash "$work/goose-install.sh"
}

wire_goose() {
	wrapper goose Goose <<'EOF'
goose=$(command -v goose 2>/dev/null || echo "$HOME/.local/bin/goose")
GOOSE_PROVIDER=openai
OPENAI_HOST=https://virsilipas.mcx.fi
OPENAI_BASE_PATH=v1/chat/completions
OPENAI_API_KEY=$key
GOOSE_MODEL=default
export GOOSE_PROVIDER OPENAI_HOST OPENAI_BASE_PATH OPENAI_API_KEY GOOSE_MODEL
exec "$goose" "$@"
EOF
}

# Kilo Code CLI: an OpenCode fork, installed with npm (@kilocode/cli); the wrapper points it at Virsilipas
# with the same custom-provider config shape as OpenCode (@ai-sdk/openai-compatible) in its own kilo.json,
# loaded through KILO_CONFIG (Kilo-Org/kilocode packages/opencode/src/config/config.ts), so your own
# ~/.config/kilo is untouched and the tools Kilo runs keep your normal XDG_CONFIG_HOME.
have_kilo() { command -v kilo >/dev/null 2>&1 || [ -x "$BIN/kilo" ]; }

install_kilo() { npm_get @kilocode/cli 20 'Kilo Code CLI'; }

wire_kilo() {
	mkdir -p "$SHARE/kilo"
	cat >"$SHARE/kilo/kilo.json" <<'EOF'
{
	"provider": {
		"virsilipas": {
			"npm": "@ai-sdk/openai-compatible",
			"options": {
				"baseURL": "https://virsilipas.mcx.fi/v1",
				"apiKey": "{env:VIRSILIPAS_KEY}"
			},
			"models": {
				"default": {"name": "Pulsed Media default"},
				"large": {"name": "Pulsed Media large"}
			}
		}
	},
	"model": "virsilipas/default"
}
EOF
	wrapper kilo 'Kilo Code CLI' <<'EOF'
kilo=$(command -v kilo 2>/dev/null || echo "$HOME/.local/bin/kilo")
VIRSILIPAS_KEY=$key
KILO_CONFIG="$HOME/.local/share/virsilipas/kilo/kilo.json"
export VIRSILIPAS_KEY KILO_CONFIG
exec "$kilo" "$@"
EOF
}

# Cline CLI: installed with npm (needs Node.js 22 or newer); the wrapper points it at Virsilipas with the
# openai-compatible provider, isolated under its own CLINE_DIR so your own ~/.cline is not touched.
have_cline() { command -v cline >/dev/null 2>&1 || [ -x "$BIN/cline" ]; }

install_cline() { npm_get cline 22 'Cline CLI'; }

wire_cline() {
	mkdir -p "$SHARE/cline"
	chmod -R go-rwx "$SHARE/cline" 2>/dev/null || true
	# `cline auth` takes the key only as --apikey, where other users could read it in the process list. The wrapper
	# writes the provider file `cline auth` would write (mode 600, Cline 3.0.73) with the shell's own printf instead,
	# again whenever ~/.virsilipasKey holds a new key.
	wrapper cline 'Cline CLI' <<'EOF'
CLINE_DIR="$HOME/.local/share/virsilipas/cline"
export CLINE_DIR
cline=$(command -v cline 2>/dev/null || echo "$HOME/.local/bin/cline")
sum=$(printf '%s' "$key" | sha256sum | cut -c1-16)
if [ "$(cat "$CLINE_DIR/.keysum" 2>/dev/null)" != "$sum" ]; then
	(umask 077; mkdir -p "$CLINE_DIR/data/settings" && printf '{"version":1,"lastUsedProvider":"openai-compatible","modes":{},"providers":{"openai-compatible":{"settings":{"provider":"openai-compatible","apiKey":"%s","model":"default","baseUrl":"https://virsilipas.mcx.fi/v1"},"updatedAt":"%s","tokenSource":"manual"}}}\n' \
		"$key" "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)" >"$CLINE_DIR/data/settings/providers.json" \
		&& printf '%s\n' "$sum" >"$CLINE_DIR/.keysum") || { echo "could not write the Cline settings" >&2; exit 1; }
fi
exec "$cline" "$@"
EOF
}

# Kimi Code CLI: installed with npm (needs Node.js 22 or newer); the wrapper points it at Virsilipas with the
# openai provider (Chat Completions, never openai_responses), isolated under its own KIMI_CODE_HOME so your
# own ~/.kimi-code is not touched. The key is read from VIRSILIPAS_KEY at request time; it is never written to disk.
have_kimi() { command -v kimi >/dev/null 2>&1 || [ -x "$BIN/kimi" ]; }

install_kimi() { npm_get @moonshot-ai/kimi-code 22 'Kimi Code CLI'; }

wire_kimi() {
	mkdir -p "$SHARE/kimi"
	cat >"$SHARE/kimi/config.toml" <<EOF
default_model = "virsilipas"

[providers.virsilipas]
type = "openai"
base_url = "$ENDPOINT"
api_key_env = "VIRSILIPAS_KEY"

[models.virsilipas]
provider = "virsilipas"
model = "default"
max_context_size = 131072
EOF
	wrapper kimi 'Kimi Code CLI' <<'EOF'
KIMI_CODE_HOME="$HOME/.local/share/virsilipas/kimi"
VIRSILIPAS_KEY=$key
export KIMI_CODE_HOME VIRSILIPAS_KEY
kimi=$(command -v kimi 2>/dev/null || echo "$HOME/.local/bin/kimi")
exec "$kimi" "$@"
EOF
}

# Mistral Vibe: installed with its own official installer (bootstraps uv, then "uv tool install mistral-vibe");
# the wrapper gives it its own home (VIBE_HOME) under $SHARE so your own ~/.vibe is never touched.
have_vibe() { command -v vibe >/dev/null 2>&1 || [ -x "$BIN/vibe" ]; }

install_vibe() {
	command -v bash >/dev/null 2>&1 || die "The Mistral Vibe installer needs bash."
	command -v curl >/dev/null 2>&1 || die "The Mistral Vibe installer needs curl."
	say "Installing Mistral Vibe with its official installer..."
	fetch https://mistral.ai/vibe/install.sh "$work/vibe-install.sh"
	# The official installer itself exits 1 when ~/.local/bin was not already on PATH at its start -- even
	# though that is exactly where it puts vibe. Only die if the binary is truly missing afterward.
	bash "$work/vibe-install.sh" || [ -x "$BIN/vibe" ] || die "The Mistral Vibe installer failed."
}

wire_vibe() {
	mkdir -p "$SHARE/vibe"
	cat >"$SHARE/vibe/config.toml" <<'EOF'
enable_telemetry = false
active_model = "default"

[[providers]]
name = "virsilipas"
api_base = "https://virsilipas.mcx.fi/v1"
api_key_env_var = "VIRSILIPAS_API_KEY"
api_style = "openai"
backend = "generic"

[[models]]
name = "default"
provider = "virsilipas"
EOF
	wrapper vibe 'Mistral Vibe' <<'EOF'
vibe=$(command -v vibe 2>/dev/null || echo "$HOME/.local/bin/vibe")
VIBE_HOME="$HOME/.local/share/virsilipas/vibe"
VIRSILIPAS_API_KEY=$key
export VIBE_HOME VIRSILIPAS_API_KEY
exec "$vibe" "$@"
EOF
}

# Aider: installed with its official installer (bootstraps uv, then uses it to install aider-chat); the
# wrapper points it at Virsilipas with plain OpenAI-compatible env vars. No Node needed. Last tagged
# release 2025-08-09 (Aider-AI/aider is going stale) -- it still installs and answers through Virsilipas.
have_aider() { command -v aider >/dev/null 2>&1 || [ -x "$BIN/aider" ]; }

install_aider() {
	say "Installing Aider with its official installer (uv)..."
	fetch https://aider.chat/install.sh "$work/aider-install.sh"
	UV_NO_MODIFY_PATH=1 sh "$work/aider-install.sh" >/dev/null
}

wire_aider() {
	wrapper aider Aider <<'EOF'
OPENAI_API_BASE=https://virsilipas.mcx.fi/v1 OPENAI_API_KEY=$key AIDER_MODEL=openai/default
export OPENAI_API_BASE OPENAI_API_KEY AIDER_MODEL
aider=$(command -v aider 2>/dev/null || echo "$HOME/.local/bin/aider")
exec "$aider" "$@"
EOF
}

# OpenHands CLI: installed with uv (needs Python 3.12+); the wrapper runs it headless, pointed at Virsilipas with
# LLM_MODEL/LLM_API_KEY/LLM_BASE_URL plus --override-with-envs (not persisted, your own ~/.openhands/ is untouched).
have_openhands() { command -v openhands >/dev/null 2>&1 || [ -x "$BIN/openhands" ]; }

install_openhands() {
	if ! command -v uv >/dev/null 2>&1; then
		command -v sha256sum >/dev/null 2>&1 || die "OpenHands CLI needs sha256sum to verify the uv installer."
		say "Installing uv (needed for OpenHands CLI)..."
		fetch https://astral.sh/uv/0.11.26/install.sh "$work/uv-install.sh"
		printf '%s  %s\n' 92fa9085d24c214bb4445cc1da8c15ca9cca8cffb34726240fa08c5302e94ccc "$work/uv-install.sh" \
			| sha256sum -c - >/dev/null 2>&1 || die "The uv installer download did not match its checksum; nothing was installed."
		sh "$work/uv-install.sh" >/dev/null
		PATH="$HOME/.local/bin:$PATH"
	fi
	command -v uv >/dev/null 2>&1 || die "uv installed but is not on PATH. How to get it: $WIKI"
	say "Installing OpenHands CLI with uv..."
	uv tool install openhands --python 3.12 >/dev/null
}

wire_openhands() {
	wrapper openhands 'OpenHands CLI' <<'EOF'
openhands=$(command -v openhands 2>/dev/null || echo "$HOME/.local/bin/openhands")
LLM_MODEL=openai/default
LLM_API_KEY=$key
LLM_BASE_URL=https://virsilipas.mcx.fi/v1
export LLM_MODEL LLM_API_KEY LLM_BASE_URL
exec "$openhands" --override-with-envs "$@"
EOF
}

# OpenClaw: npm package, installed into its own prefix (needs Node engines >=24.16.0 <25 or >=26.1.0; we never
# upgrade your Node). Onboarded once, non-interactively, with provider "virsilipas" (Chat Completions) and model
# "default"; the key is a ref to CUSTOM_API_KEY, so no secret lands in its config file. OpenClaw has no state-dir
# override env var, so if you already have ~/.openclaw this is left untouched and the wrapper just runs your own.
have_openclaw() { command -v openclaw >/dev/null 2>&1 || [ -x "$SHARE/openclaw/bin/openclaw" ]; }

install_openclaw() {
	major=$(node_major)
	minor=$(node -v 2>/dev/null | sed -n 's/^v[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/p'); minor=${minor:-0}
	ok=no
	[ "$major" -eq 24 ] && [ "$minor" -ge 16 ] && ok=yes
	[ "$major" -ge 27 ] && ok=yes
	[ "$major" -eq 26 ] && [ "$minor" -ge 1 ] && ok=yes
	command -v npm >/dev/null 2>&1 || ok=no
	[ "$ok" = yes ] || die "OpenClaw needs Node.js >=24.16.0 <25 or >=26.1.0 with npm (we never upgrade it for you); found $(node -v 2>/dev/null || echo 'no node')."
	say "Installing OpenClaw..."
	mkdir -p "$SHARE/openclaw-npm-cache"
	npm install --prefix "$SHARE/openclaw" --cache "$SHARE/openclaw-npm-cache" -g \
		--allow-scripts='@google/genai,esbuild,koffi,protobufjs,openclaw' openclaw >/dev/null
}

wire_openclaw() {
	oc="$SHARE/openclaw/bin/openclaw"; command -v openclaw >/dev/null 2>&1 && oc=openclaw
	# Your own OpenClaw setup (~/.openclaw not made by this installer) is never changed.
	if [ -e "$HOME/.openclaw" ] && [ ! -e "$SHARE/openclaw-gateway.env" ]; then
		say "OpenClaw: your own ~/.openclaw stays as it is, so it was not pointed at your credits."
		return 1
	fi
	if [ ! -e "$HOME/.openclaw" ]; then
		umask 077
		token=$(od -An -tx1 -N32 /dev/urandom | tr -d ' \n')
		printf 'OPENCLAW_GATEWAY_TOKEN=%s\n' "$token" >"$SHARE/openclaw-gateway.env"
		umask 022
		OPENCLAW_GATEWAY_TOKEN=$token CUSTOM_API_KEY=$key "$oc" onboard --non-interactive --accept-risk --mode local \
			--auth-choice custom-api-key --custom-base-url https://virsilipas.mcx.fi/v1 --custom-model-id default \
			--custom-provider-id virsilipas --custom-compatibility openai --secret-input-mode ref \
			--gateway-auth token --gateway-token-ref-env OPENCLAW_GATEWAY_TOKEN --gateway-bind loopback \
			--skip-daemon --skip-channels --skip-skills --skip-ui --skip-health --suppress-gateway-token-output \
			|| die "OpenClaw onboarding failed."
		# The endpoint takes text only and always streams token usage; tell OpenClaw so (same fields as PMSS).
		"$oc" config set 'models.providers.virsilipas.models[0].input' '["text"]' --strict-json >/dev/null \
			&& "$oc" config set 'models.providers.virsilipas.models[0].compat' '{"supportsUsageInStreaming":true}' --strict-json --merge >/dev/null \
			|| say "OpenClaw: could not mark the model as text-only with usage reporting; it still works."
	fi
	wrapper openclaw OpenClaw <<'EOF'
oc=$(command -v openclaw 2>/dev/null || echo "$HOME/.local/share/virsilipas/openclaw/bin/openclaw")
envfile="$HOME/.local/share/virsilipas/openclaw-gateway.env"
if [ -r "$envfile" ]; then . "$envfile"; export OPENCLAW_GATEWAY_TOKEN; fi
CUSTOM_API_KEY=$key
export CUSTOM_API_KEY
exec "$oc" "$@"
EOF
}

# ZeroClaw (zeroclaw-labs): the prebuilt static (musl) release binary from GitHub, put in ~/.local/bin. The wrapper runs
# it with its own private config directory (ZEROCLAW_CONFIG_DIR) holding one agent, "assistant"; the "custom"
# OpenAI-compatible provider pointed at your credits, and your key, reach ZeroClaw only through its environment
# overrides, so the key is never written to a file and your own ~/.zeroclaw stays as it is.
RUN_zeroclaw='virsilipas-zeroclaw agent -a assistant'
have_zeroclaw() { command -v zeroclaw >/dev/null 2>&1 || [ -x "$BIN/zeroclaw" ]; }

install_zeroclaw() {
	[ "$(uname -m)" = x86_64 ] || die "ZeroClaw: only x86_64 Linux is set up here."
	command -v tar >/dev/null 2>&1 || die "ZeroClaw needs tar."
	say "Installing ZeroClaw..."
	fetch https://github.com/zeroclaw-labs/zeroclaw/releases/latest/download/zeroclaw-x86_64-unknown-linux-musl.tar.gz "$work/zeroclaw.tgz"
	mkdir -p "$work/zeroclaw" "$BIN"
	tar -xzf "$work/zeroclaw.tgz" -C "$work/zeroclaw"
	zc=$(find "$work/zeroclaw" -type f -name zeroclaw | head -n 1)
	[ -n "$zc" ] || die "ZeroClaw: no zeroclaw program in the release archive."
	cp "$zc" "$BIN/zeroclaw.new" && chmod 755 "$BIN/zeroclaw.new" && mv -f "$BIN/zeroclaw.new" "$BIN/zeroclaw"
}

wire_zeroclaw() {
	# ZeroClaw refuses a config or data directory other users can write to, so it gets its own, private one.
	mkdir -p "$SHARE/zeroclaw"
	chmod 700 "$SHARE/zeroclaw"
	cat >"$SHARE/zeroclaw/config.toml" <<'EOF'
[risk_profiles.default]
level = "supervised"

[agents.assistant]
model_provider = "custom.virsilipas"
risk_profile = "default"
EOF
	wrapper zeroclaw ZeroClaw <<'EOF'
zeroclaw=$(command -v zeroclaw 2>/dev/null || echo "$HOME/.local/bin/zeroclaw")
ZEROCLAW_CONFIG_DIR="$HOME/.local/share/virsilipas/zeroclaw"
ZEROCLAW_providers__models__custom__virsilipas__uri=https://virsilipas.mcx.fi/v1
ZEROCLAW_providers__models__custom__virsilipas__model=default
ZEROCLAW_providers__models__custom__virsilipas__wire_api=chat_completions
ZEROCLAW_providers__models__custom__virsilipas__api_key=$key
export ZEROCLAW_CONFIG_DIR ZEROCLAW_providers__models__custom__virsilipas__uri ZEROCLAW_providers__models__custom__virsilipas__model
export ZEROCLAW_providers__models__custom__virsilipas__wire_api ZEROCLAW_providers__models__custom__virsilipas__api_key
umask 077
exec "$zeroclaw" "$@"
EOF
}

# PicoClaw (Sipeed): the prebuilt release binary from GitHub, put in ~/.local/bin. The wrapper runs it with its own private
# home (PICOCLAW_HOME) and config (PICOCLAW_CONFIG): one model, "virsilipas", on the OpenAI-compatible provider pointed at
# your credits. PicoClaw reads the key from .security.yml there (mode 0600), which the wrapper rewrites from your key on
# every run; your own ~/.picoclaw stays as it is.
RUN_picoclaw='virsilipas-picoclaw agent'
have_picoclaw() { command -v picoclaw >/dev/null 2>&1 || [ -x "$BIN/picoclaw" ]; }

install_picoclaw() {
	[ "$(uname -m)" = x86_64 ] || die "PicoClaw: only x86_64 Linux is set up here."
	command -v tar >/dev/null 2>&1 || die "PicoClaw needs tar."
	say "Installing PicoClaw..."
	fetch https://github.com/sipeed/picoclaw/releases/latest/download/picoclaw_Linux_x86_64.tar.gz "$work/picoclaw.tgz"
	mkdir -p "$work/picoclaw" "$BIN"
	tar -xzf "$work/picoclaw.tgz" -C "$work/picoclaw"
	pc=$(find "$work/picoclaw" -type f -name picoclaw | head -n 1)
	[ -n "$pc" ] || die "PicoClaw: no picoclaw program in the release archive."
	cp "$pc" "$BIN/picoclaw.new" && chmod 755 "$BIN/picoclaw.new" && mv -f "$BIN/picoclaw.new" "$BIN/picoclaw"
}

wire_picoclaw() {
	mkdir -p "$SHARE/picoclaw"
	chmod 700 "$SHARE/picoclaw"
	cat >"$SHARE/picoclaw/config.json" <<'EOF'
{"agents": {"defaults": {"model_name": "virsilipas"}},
 "model_list": [{"model_name": "virsilipas", "provider": "openai", "model": "default", "api_base": "https://virsilipas.mcx.fi/v1"}]}
EOF
	wrapper picoclaw PicoClaw <<'EOF'
picoclaw=$(command -v picoclaw 2>/dev/null || echo "$HOME/.local/bin/picoclaw")
PICOCLAW_HOME="$HOME/.local/share/virsilipas/picoclaw"
PICOCLAW_CONFIG="$PICOCLAW_HOME/config.json"
export PICOCLAW_HOME PICOCLAW_CONFIG
umask 077
printf 'model_list:\n  virsilipas:\n    api_keys: ["%s"]\n' "$key" >"$PICOCLAW_HOME/.security.yml"
exec "$picoclaw" "$@"
EOF
}

# NullClaw: the prebuilt static release binary from GitHub, put in ~/.local/bin. The wrapper runs it with its own private
# home (NULLCLAW_HOME) and points it at your credits through its environment settings: an OpenAI-compatible "custom:"
# provider, model "default" and your key, so the key is never written to a file and your own ~/.nullclaw stays as it is.
RUN_nullclaw='virsilipas-nullclaw agent'
have_nullclaw() { command -v nullclaw >/dev/null 2>&1 || [ -x "$BIN/nullclaw" ]; }

install_nullclaw() {
	[ "$(uname -m)" = x86_64 ] || die "NullClaw: only x86_64 Linux is set up here."
	command -v curl >/dev/null 2>&1 || die "NullClaw needs curl (it sends its model requests with it)."
	say "Installing NullClaw..."
	mkdir -p "$BIN"
	fetch https://github.com/nullclaw/nullclaw/releases/latest/download/nullclaw-linux-x86_64.bin "$BIN/nullclaw.new"
	chmod 755 "$BIN/nullclaw.new" && mv -f "$BIN/nullclaw.new" "$BIN/nullclaw"
}

wire_nullclaw() {
	mkdir -p "$SHARE/nullclaw/workspace"
	chmod 700 "$SHARE/nullclaw"
	wrapper nullclaw NullClaw <<'EOF'
nullclaw=$(command -v nullclaw 2>/dev/null || echo "$HOME/.local/bin/nullclaw")
NULLCLAW_HOME="$HOME/.local/share/virsilipas/nullclaw"
NULLCLAW_WORKSPACE="$NULLCLAW_HOME/workspace"
NULLCLAW_PROVIDER=custom:https://virsilipas.mcx.fi/v1
NULLCLAW_MODEL=default
NULLCLAW_API_KEY=$key
export NULLCLAW_HOME NULLCLAW_WORKSPACE NULLCLAW_PROVIDER NULLCLAW_MODEL NULLCLAW_API_KEY
umask 077
exec "$nullclaw" "$@"
EOF
}

# IronClaw (NEAR AI): the prebuilt static (musl) release binary from GitHub, put in ~/.local/bin. The wrapper runs it with
# its own private home (IRONCLAW_REBORN_HOME) whose config.toml holds one model slot, the OpenAI-compatible route to your
# credits (named zai-org/GLM-5.3-Flash, the default model until 2026-10-30, because IronClaw reads "default" as its own profile name);
# the key reaches it only through LLM_API_KEY (IronClaw refuses a key written in the file), and your own
# ~/.ironclaw stays as it is. Its "onboard" step, which installs a background service, is never run. IronClaw takes the
# current folder as its workspace and refuses to start when that folder holds its own home ("workspace root must not
# overlap default skill root /skills"), as "/" and your home folder do, so from there the wrapper moves to ~/ironclaw-work.
RUN_ironclaw='virsilipas-ironclaw run --message'
have_ironclaw() { command -v ironclaw >/dev/null 2>&1 || [ -x "$BIN/ironclaw" ]; }

install_ironclaw() {
	[ "$(uname -m)" = x86_64 ] || die "IronClaw: only x86_64 Linux is set up here."
	command -v tar >/dev/null 2>&1 || die "IronClaw needs tar."
	say "Installing IronClaw..."
	fetch https://github.com/nearai/ironclaw/releases/latest/download/ironclaw-x86_64-unknown-linux-musl.tar.gz "$work/ironclaw.tgz"
	mkdir -p "$work/ironclaw" "$BIN"
	tar -xzf "$work/ironclaw.tgz" -C "$work/ironclaw"
	ic=$(find "$work/ironclaw" -type f -name ironclaw | head -n 1)
	[ -n "$ic" ] || die "IronClaw: no ironclaw program in the release archive."
	cp "$ic" "$BIN/ironclaw.new" && chmod 755 "$BIN/ironclaw.new" && mv -f "$BIN/ironclaw.new" "$BIN/ironclaw"
}

wire_ironclaw() {
	mkdir -p "$SHARE/ironclaw"
	chmod 700 "$SHARE/ironclaw"
	cat >"$SHARE/ironclaw/config.toml" <<'EOF'
[llm.default]
provider_id = "openai_compatible"
base_url = "https://virsilipas.mcx.fi/v1"
model = "zai-org/GLM-5.3-Flash"
api_key_env = "LLM_API_KEY"
EOF
	wrapper ironclaw IronClaw <<'EOF'
ironclaw=$(command -v ironclaw 2>/dev/null || echo "$HOME/.local/bin/ironclaw")
IRONCLAW_REBORN_HOME="$HOME/.local/share/virsilipas/ironclaw"
LLM_API_KEY=$key
export IRONCLAW_REBORN_HOME LLM_API_KEY
umask 077
case $IRONCLAW_REBORN_HOME in "${PWD%/}"/*) mkdir -p "$HOME/ironclaw-work" && cd "$HOME/ironclaw-work" && echo "IronClaw works in ~/ironclaw-work (it cannot run in a folder that holds its own home)" >&2 ;; esac
exec "$ironclaw" "$@"
EOF
}

# Moltis (moltis-org): the prebuilt glibc release (one program plus its web files) from GitHub, kept together in
# ~/.local/share/virsilipas/moltis-app with the program linked into ~/.local/bin. The wrapper runs it with its own
# private config and data directories; it rewrites moltis.toml there on every run with the "custom-virsilipas"
# OpenAI-compatible provider and your key, so your own ~/.config/moltis stays as it is. Its web gateway listens on
# 127.0.0.1 only, on a port taken from your user id, and the wrapper gives it a password (kept in moltis.password
# there) because Moltis lets any local connection in without one, which on a shared machine means other users too.
RUN_moltis='virsilipas-moltis (its web UI; or virsilipas-moltis acp for an editor)'
have_moltis() { command -v moltis >/dev/null 2>&1 || [ -x "$BIN/moltis" ]; }

install_moltis() {
	[ "$(uname -m)" = x86_64 ] || die "Moltis: only x86_64 Linux is set up here."
	command -v tar >/dev/null 2>&1 || die "Moltis needs tar."
	say "Installing Moltis..."
	fetch https://api.github.com/repos/moltis-org/moltis/releases/latest "$work/moltis.json"
	tag=$(sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' "$work/moltis.json" | head -n 1)
	[ -n "$tag" ] || die "Moltis: could not find the latest release."
	fetch "https://github.com/moltis-org/moltis/releases/download/$tag/moltis-$tag-x86_64-unknown-linux-gnu.tar.gz" "$work/moltis.tgz"
	rm -rf "$SHARE/moltis-app.new"
	mkdir -p "$SHARE/moltis-app.new" "$BIN"
	tar -xzf "$work/moltis.tgz" -C "$SHARE/moltis-app.new"
	[ -x "$SHARE/moltis-app.new/moltis" ] || die "Moltis: no moltis program in the release archive."
	if command -v ldd >/dev/null 2>&1 && ldd "$SHARE/moltis-app.new/moltis" | grep -q 'not found'; then
		die "Moltis needs these system libraries, which only an administrator can install: $(ldd "$SHARE/moltis-app.new/moltis" | sed -n 's/^[[:space:]]*\([^ ]*\) => not found.*/\1/p' | tr '\n' ' ')"
	fi
	rm -rf "$SHARE/moltis-app"
	mv "$SHARE/moltis-app.new" "$SHARE/moltis-app"
	ln -sf "$SHARE/moltis-app/moltis" "$BIN/moltis"
}

wire_moltis() {
	mkdir -p "$SHARE/moltis/config" "$SHARE/moltis/data"
	chmod 700 "$SHARE/moltis"
	[ -s "$SHARE/moltis/moltis.password" ] || (umask 077; od -An -N18 -tx1 /dev/urandom | tr -d ' \n' >"$SHARE/moltis/moltis.password")
	wrapper moltis Moltis <<'EOF'
moltis=$(command -v moltis 2>/dev/null || echo "$HOME/.local/bin/moltis")
dir="$HOME/.local/share/virsilipas/moltis"
port=$((20000 + $(id -u) % 30000))
umask 077
cat >"$dir/config/moltis.toml" <<TOML
[server]
bind = "127.0.0.1"
port = $port

[providers]
offered = ["custom-virsilipas"]

[providers.custom-virsilipas]
enabled = true
api_key = "$key"
base_url = "https://virsilipas.mcx.fi/v1"
models = ["default"]

[tools.exec.sandbox]
network = "blocked"
TOML
MOLTIS_CONFIG_DIR="$dir/config"
MOLTIS_DATA_DIR="$dir/data"
MOLTIS_SHARE_DIR="$HOME/.local/share/virsilipas/moltis-app/share/moltis"
MOLTIS_PASSWORD=$(cat "$dir/moltis.password")
export MOLTIS_CONFIG_DIR MOLTIS_DATA_DIR MOLTIS_SHARE_DIR MOLTIS_PASSWORD
[ $# -gt 0 ] && [ "$1" != gateway ] || echo "Moltis web UI: https://127.0.0.1:$port/ on this machine (reach it with ssh -L $port:127.0.0.1:$port); password in $dir/moltis.password" >&2
exec "$moltis" "$@"
EOF
}

# Pikkumies: Pulsed Media's own AI helper (github.com/MagnaCapax/mcxPikkumies). It reads ~/.virsilipasKey itself,
# so it needs no wrapper; its installer is downloaded to a file and run.
RUN_pikkumies=pikkumies
have_pikkumies() { command -v pikkumies >/dev/null 2>&1 || [ -x "$HOME/.mcxPikkumies/pikkumies/bin/pikkumies" ]; }

install_pikkumies() {
	# curl: the agent runtime build downloads its own tools with it (live check 2026-10-20: wget alone fails).
	for tool in bash git php curl; do
		command -v $tool >/dev/null 2>&1 || die "Pikkumies needs bash, git, curl and PHP 7.3 or newer with curl (Debian and Ubuntu: apt install git curl php-cli php-curl libatomic1)."
	done
	say "Installing Pikkumies..."
	fetch https://raw.githubusercontent.com/MagnaCapax/mcxPikkumies/main/install.sh "$work/pikkumies-install.sh"
	bash "$work/pikkumies-install.sh" </dev/null
}

wire_pikkumies() { :; }

main() { vs_setup "$@"; vs_universal; }
main "$@"
