#!/bin/sh
# Install the Adom statusline for Claude Code:
#   1. ~/.claude/statusline.sh  (the render script; existing different file -> .bak)
#   2. ~/.claude/settings.json  (merge the statusLine key; all other keys preserved)
set -e

HERE="$(cd "$(dirname "$0")" && pwd)"
CLAUDE_DIR="$HOME/.claude"
DEST="$CLAUDE_DIR/statusline.sh"
SETTINGS="$CLAUDE_DIR/settings.json"

mkdir -p "$CLAUDE_DIR"

if [ -f "$DEST" ] && ! cmp -s "$HERE/statusline.sh" "$DEST"; then
  cp "$DEST" "$DEST.bak"
  echo "backed up existing $DEST -> $DEST.bak"
fi
cp "$HERE/statusline.sh" "$DEST"
chmod +x "$DEST"
echo "installed $DEST"

# Merge (not clobber) settings.json. python3 is present on macOS and every Adom container.
python3 - "$SETTINGS" <<'PY'
import json, os, sys
path = sys.argv[1]
settings = {}
if os.path.exists(path):
    with open(path, encoding="utf-8") as f:
        settings = json.load(f)
settings["statusLine"] = {"type": "command", "command": "~/.claude/statusline.sh"}
tmp = path + ".new"
with open(tmp, "w", encoding="utf-8") as f:
    json.dump(settings, f, indent=2)
    f.write("\n")
os.replace(tmp, path)
print(f"merged statusLine into {path}")
PY

echo "done — restart Claude Code (or start a new session) to see the status line"
