#!/bin/sh
# The unclosed installer for Linux and macOS:
#   curl -fsSL https://unclosed.ai/install.sh | sh
# It puts the unclosed command in ~/.unclosed/bin (UNCLOSED_HOME moves the folder) and on the PATH of new terminals,
# after checking it against the SHA-256 the same site publishes (/cli/latest). Nothing comes from a public registry,
# nothing else on the computer changes, and nothing asks a question. UNCLOSED_URL points it at another address (a local
# copy); a copy of the site serves this file with its own address below.
# Plain sh (dash on Ubuntu). Windows is untried.
set -eu
URL="${UNCLOSED_URL:-https://unclosed.ai}"; URL="${URL%/}"
HOME_DIR="${UNCLOSED_HOME:-$HOME/.unclosed}"
DIR="$HOME_DIR/bin"
MARK='# added by unclosed'
say(){ for line in "$@"; do printf '%s\n' "$line"; done; }
fail(){ printf '%s\n' "$*" >&2; exit 1; }

# ---- Python 3.9 or newer runs the command
PY=""
for p in python3 python3.13 python3.12 python3.11 python3.10 python3.9; do
  c=$(command -v "$p" 2>/dev/null || true)
  if [ -n "$c" ] && "$c" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 9) else 1)' 2>/dev/null; then PY="$c"; break; fi
done
if [ -z "$PY" ]; then
  if [ "$(uname -s)" = Darwin ]; then
    fail "unclosed needs Python 3.9 or newer. It comes with Apple's command line tools (xcode-select --install) or from https://www.python.org/downloads/. Nothing was changed."
  fi
  fail "unclosed needs Python 3.9 or newer: python3 from this computer's package manager. Nothing was changed."
fi

# ---- The command itself, checked against its published SHA-256
TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT
curl -fsSL "$URL/cli/latest" -o "$TMP/latest.json" || fail "$URL can't be reached right now, or does not offer unclosed. Nothing was changed."
field(){ "$PY" -c 'import json,sys
try: print(json.load(open(sys.argv[1])).get(sys.argv[2]) or "")
except Exception: print("")' "$TMP/latest.json" "$1"; }
CLI_URL=$(field url); CLI_SHA=$(field sha256); CLI_VERSION=$(field version)
[ -n "$CLI_SHA" ] || fail "$URL doesn't offer unclosed. Nothing was changed."
case "$CLI_URL" in "$URL"/*) ;; *) CLI_URL="$URL/cli/unclosed.py" ;; esac
curl -fsSL "$CLI_URL" -o "$TMP/unclosed" || fail "unclosed couldn't be downloaded from $URL. Nothing was changed."
GOT=$("$PY" -c 'import hashlib,sys; print(hashlib.sha256(open(sys.argv[1],"rb").read()).hexdigest())' "$TMP/unclosed")
[ "$GOT" = "$CLI_SHA" ] || fail "unclosed didn't match the SHA-256 $URL publishes, so it was not installed. Nothing was changed."
mkdir -p "$DIR"; chmod 700 "$HOME_DIR"
cp "$TMP/unclosed" "$DIR/unclosed.new" && chmod 755 "$DIR/unclosed.new" && mv "$DIR/unclosed.new" "$DIR/unclosed"
# The command starts with "#!/usr/bin/env python3": a python3 first on the PATH that is too old gets a small runner.
if ! python3 -c 'import sys; sys.exit(0 if sys.version_info >= (3, 9) else 1)' 2>/dev/null; then
  printf '#!/bin/sh\nexec "%s" "%s/unclosed.py" "$@"\n' "$PY" "$DIR" > "$DIR/unclosed.run"
  mv "$DIR/unclosed" "$DIR/unclosed.py" && mv "$DIR/unclosed.run" "$DIR/unclosed" && chmod 755 "$DIR/unclosed"
fi

# ---- On the PATH, for new terminals and for an agent's shells (Codex runs zsh -lc, which reads .zprofile and not
# .zshrc): one marked line in each, which adds the folder once and which unclosed uninstall takes out again (with a
# file made only to hold it: those are named in $HOME_DIR/made)
NEWPATH=""
case ":$PATH:" in
  *":$DIR:"*) ;;
  *) LINE="case \":\$PATH:\" in *\":$DIR:\"*) ;; *) export PATH=\"$DIR:\$PATH\" ;; esac  $MARK"
     for rc in "$HOME/.profile" "$HOME/.bashrc" "$HOME/.zprofile" "$HOME/.zshrc"; do
       case "$rc" in
         */.profile) ;;
         */.zshrc|*/.zprofile) [ -f "$rc" ] || [ "$(basename "${SHELL:-}")" = zsh ] || continue ;;
         *) [ -f "$rc" ] || continue ;;
       esac
       grep -qF "$MARK" "$rc" 2>/dev/null && continue
       if [ -f "$rc" ]; then
         [ ! -s "$rc" ] || [ -z "$(tail -c 1 "$rc")" ] || printf '\n' >> "$rc"
       else
         printf '%s\n' "$rc" >> "$HOME_DIR/made"
       fi
       printf '%s\n' "$LINE" >> "$rc"
     done
     NEWPATH=1 ;;
esac

# ---- An older unclosed (October 2026) left a timed job on macOS that ran "unclosed update" every six hours. This one
# updates only when asked, so that job and its file are taken out. A job of any other name is left alone.
OLDJOB=""
PLIST="$HOME/Library/LaunchAgents/ai.unclosed.update.plist"
if [ -f "$PLIST" ] && grep -q 'ai\.unclosed\.update' "$PLIST" && grep -q '/bin/unclosed' "$PLIST"; then
  if command -v launchctl >/dev/null 2>&1; then
    launchctl bootout "gui/$(id -u)/ai.unclosed.update" 2>/dev/null || launchctl unload "$PLIST" 2>/dev/null || true
  fi
  rm -f "$PLIST" && OLDJOB=1
fi

say "unclosed $CLI_VERSION is installed in $DIR, for $URL."
[ -z "$OLDJOB" ] || say "An older unclosed's timed update (ai.unclosed.update) is taken out: this one updates when you run unclosed update."
[ -z "$NEWPATH" ] || say "New terminals find it. Until one starts, it is $DIR/unclosed (in this one: export PATH=\"$DIR:\$PATH\")."
# The one next step, in the words every agent reads (cli/unclosed.py NEXT_STEP, app/start.py; tests/test_one_way.py).
say "Next: Run unclosed login and give your person the link and the code it prints. On that page they sign in and press Allow, or, if they are new, create their account with one press."
say "Then: unclosed (the Balance, the GPU and the apps) and unclosed --help (every action)."
