|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Idempotently add Bux integration around Hermes without touching Hermes auth. |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +WITH_HERMES="${WITH_HERMES:-1}" |
| 6 | +if [ "$WITH_HERMES" = "0" ]; then |
| 7 | + echo "install-hermes: skipped (WITH_HERMES=0)" |
| 8 | + exit 0 |
| 9 | +fi |
| 10 | + |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +REPO_DIR="${BUX_REPO_DIR:-$(cd "$SCRIPT_DIR/.." && pwd)}" |
| 13 | +BUX_HOME="${BUX_HOME:-/home/bux}" |
| 14 | +BUX_USER="${BUX_USER:-bux}" |
| 15 | +BUX_GROUP="${BUX_GROUP:-bux}" |
| 16 | +BUX_BIN_DIR="${BUX_BIN_DIR:-/usr/local/bin}" |
| 17 | +HERMES_HOME="${HERMES_HOME:-$BUX_HOME/.hermes}" |
| 18 | +HERMES_SOUL="${HERMES_SOUL:-$HERMES_HOME/SOUL.md}" |
| 19 | +HERMES_INSTALL_MODE="${HERMES_INSTALL_MODE:-detect}" |
| 20 | +export HERMES_HOME |
| 21 | + |
| 22 | +say() { printf 'install-hermes: %s\n' "$*"; } |
| 23 | +warn() { printf 'install-hermes: WARN %s\n' "$*" >&2; } |
| 24 | + |
| 25 | +user_exists() { |
| 26 | + id -u "$BUX_USER" >/dev/null 2>&1 |
| 27 | +} |
| 28 | + |
| 29 | +chown_bux_if_possible() { |
| 30 | + if user_exists; then |
| 31 | + chown "$BUX_USER:$BUX_GROUP" "$@" 2>/dev/null || true |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +run_as_bux() { |
| 36 | + if user_exists && [ "$(id -u)" -eq 0 ]; then |
| 37 | + sudo -iu "$BUX_USER" bash -lc "$*" |
| 38 | + else |
| 39 | + bash -lc "$*" |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +find_hermes() { |
| 44 | + if [ -n "${HERMES_BIN:-}" ] && [ -x "$HERMES_BIN" ]; then |
| 45 | + printf '%s\n' "$HERMES_BIN" |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + for candidate in \ |
| 49 | + "$BUX_HOME/.local/bin/hermes" \ |
| 50 | + "$BUX_HOME/.npm-global/bin/hermes" \ |
| 51 | + /usr/local/bin/hermes \ |
| 52 | + /usr/bin/hermes |
| 53 | + do |
| 54 | + if [ -x "$candidate" ]; then |
| 55 | + printf '%s\n' "$candidate" |
| 56 | + return 0 |
| 57 | + fi |
| 58 | + done |
| 59 | + return 1 |
| 60 | +} |
| 61 | + |
| 62 | +install -d -m 0755 "$HERMES_HOME" |
| 63 | +chown_bux_if_possible "$HERMES_HOME" |
| 64 | + |
| 65 | +if hermes_bin="$(find_hermes)"; then |
| 66 | + say "found Hermes at $hermes_bin" |
| 67 | +elif [ -n "${HERMES_INSTALL_CMD:-}" ]; then |
| 68 | + say "running HERMES_INSTALL_CMD as $BUX_USER" |
| 69 | + run_as_bux "$HERMES_INSTALL_CMD" || warn "HERMES_INSTALL_CMD failed" |
| 70 | +elif [ "$HERMES_INSTALL_MODE" = "skip" ] || [ "$HERMES_INSTALL_MODE" = "detect" ]; then |
| 71 | + warn "Hermes binary not found; leaving wrapper installed so /hermes reports a clear error" |
| 72 | +else |
| 73 | + warn "unknown HERMES_INSTALL_MODE=$HERMES_INSTALL_MODE; not installing Hermes" |
| 74 | +fi |
| 75 | + |
| 76 | +if [ -d "$BUX_BIN_DIR" ] || mkdir -p "$BUX_BIN_DIR" 2>/dev/null; then |
| 77 | + if ln -sfn "$REPO_DIR/agent/bux-hermes" "$BUX_BIN_DIR/bux-hermes" 2>/dev/null; then |
| 78 | + say "linked $BUX_BIN_DIR/bux-hermes" |
| 79 | + else |
| 80 | + warn "could not link $BUX_BIN_DIR/bux-hermes" |
| 81 | + fi |
| 82 | +else |
| 83 | + warn "could not create $BUX_BIN_DIR" |
| 84 | +fi |
| 85 | + |
| 86 | +python3 - "$REPO_DIR/agent/HERMES.md" "$REPO_DIR/private/hermes/soul.local.md" "$HERMES_SOUL" <<'PY' |
| 87 | +from __future__ import annotations |
| 88 | +
|
| 89 | +import re |
| 90 | +import shutil |
| 91 | +import sys |
| 92 | +from pathlib import Path |
| 93 | +
|
| 94 | +source = Path(sys.argv[1]) |
| 95 | +local = Path(sys.argv[2]) |
| 96 | +dest = Path(sys.argv[3]) |
| 97 | +
|
| 98 | +managed = source.read_text(encoding="utf-8").rstrip() + "\n" |
| 99 | +local_text = local.read_text(encoding="utf-8").rstrip() + "\n" if local.exists() else "" |
| 100 | +
|
| 101 | +block = ( |
| 102 | + "<!-- BEGIN BUX HERMES MANAGED -->\n" |
| 103 | + + managed |
| 104 | + + "<!-- END BUX HERMES MANAGED -->\n\n" |
| 105 | + + "<!-- BEGIN BUX HERMES LOCAL -->\n" |
| 106 | + + local_text |
| 107 | + + "<!-- END BUX HERMES LOCAL -->\n" |
| 108 | +) |
| 109 | +
|
| 110 | +dest.parent.mkdir(parents=True, exist_ok=True) |
| 111 | +existing = dest.read_text(encoding="utf-8") if dest.exists() else "" |
| 112 | +if existing and not (dest.with_suffix(dest.suffix + ".pre-bux")).exists(): |
| 113 | + shutil.copy2(dest, dest.with_suffix(dest.suffix + ".pre-bux")) |
| 114 | +
|
| 115 | +pattern = re.compile( |
| 116 | + r"(?s)<!-- BEGIN BUX HERMES MANAGED -->.*<!-- END BUX HERMES LOCAL -->\n?" |
| 117 | +) |
| 118 | +if pattern.search(existing): |
| 119 | + out = pattern.sub(block, existing) |
| 120 | +elif existing.strip(): |
| 121 | + out = existing.rstrip() + "\n\n" + block |
| 122 | +else: |
| 123 | + out = block |
| 124 | +
|
| 125 | +dest.write_text(out, encoding="utf-8") |
| 126 | +PY |
| 127 | + |
| 128 | +chmod 0644 "$HERMES_SOUL" |
| 129 | +chown_bux_if_possible "$HERMES_SOUL" |
| 130 | +say "updated $HERMES_SOUL" |
| 131 | + |
| 132 | +if hermes_bin="$(find_hermes)"; then |
| 133 | + printf 'HERMES_BIN=%s\n' "$hermes_bin" > "$HERMES_HOME/env" |
| 134 | + chmod 0644 "$HERMES_HOME/env" |
| 135 | + chown_bux_if_possible "$HERMES_HOME/env" |
| 136 | + "$hermes_bin" --version >/dev/null 2>&1 || true |
| 137 | +fi |
0 commit comments