#!/usr/bin/env bash
# harness-init.sh — generate a per-repo thin wrapper (<repoRoot>/.claude/scripts/runplan).
# audience: AI coding agents first.
#
# WHAT IT EMITS: a thin wrapper that resolves HARNESS_HOME/engine/CURRENT fresh on every
# invocation and execs the matching runner.js. No logic lives in the wrapper — regenerating
# it is always cheap/safe (unlike a drift-checked wrapper), so this generator is unconditional
# and idempotent.
#
# Usage:
#   harness-init.sh <repoRoot> [--systemd-user-install|--systemd-user-status|--systemd-user-uninstall]
set -uo pipefail

die() { echo "harness-init: $1" >&2; exit "${2:-3}"; }

[[ $# -ge 1 && $# -le 2 ]] || die "usage: harness-init.sh <repoRoot> [--systemd-user-install|--systemd-user-status|--systemd-user-uninstall]"
ROOT=$1
MODE="${2:-}"

ROOT=$(cd "$ROOT" 2>/dev/null && pwd) || die "repo root does not exist: $1"
[[ -e "$ROOT/.git" ]] || die "not a git repository: $ROOT"

HARNESS_HOME_ABS="${HARNESS_HOME:-${HOME}/.harness}"
XDG_CONFIG_HOME_ABS="${XDG_CONFIG_HOME:-${HOME}/.config}"
SYSTEMD_USER_DIR="$XDG_CONFIG_HOME_ABS/systemd/user"
SYSTEMD_TEMPLATE_DIR="$ROOT/systemd"

SCRIPTS_DIR="$ROOT/.claude/scripts"
WRAPPER="$SCRIPTS_DIR/runplan"

install_unit() {
  local src=$1
  local dest=$2
  mkdir -p "$(dirname "$dest")" || die "failed to create $(dirname "$dest")"
  sed \
    -e "s|@ROOT@|$ROOT|g" \
    -e "s|@HARNESS_HOME@|$HARNESS_HOME_ABS|g" \
    "$src" > "$dest" || die "failed to render $dest"
}

systemctl_user() {
  if command -v systemctl >/dev/null 2>&1; then
    systemctl --user "$@" >/dev/null 2>&1 || true
  fi
}

systemd_install() {
  install_unit "$SYSTEMD_TEMPLATE_DIR/harnessd.service" "$SYSTEMD_USER_DIR/harnessd.service"
  install_unit "$SYSTEMD_TEMPLATE_DIR/harnessd-daily.service" "$SYSTEMD_USER_DIR/harnessd-daily.service"
  install_unit "$SYSTEMD_TEMPLATE_DIR/harnessd-daily.timer" "$SYSTEMD_USER_DIR/harnessd-daily.timer"
  systemctl_user daemon-reload
  printf '%s\n' "$SYSTEMD_USER_DIR"
}

systemd_status() {
  local name
  for name in harnessd.service harnessd-daily.service harnessd-daily.timer; do
    if [[ -f "$SYSTEMD_USER_DIR/$name" ]]; then
      printf '%s: installed (%s)\n' "$name" "$SYSTEMD_USER_DIR/$name"
    else
      printf '%s: missing\n' "$name"
    fi
  done
}

systemd_uninstall() {
  rm -f \
    "$SYSTEMD_USER_DIR/harnessd.service" \
    "$SYSTEMD_USER_DIR/harnessd-daily.service" \
    "$SYSTEMD_USER_DIR/harnessd-daily.timer"
  systemctl_user daemon-reload
}

case "$MODE" in
  --systemd-user-install)
    systemd_install
    exit 0
    ;;
  --systemd-user-status)
    systemd_status
    exit 0
    ;;
  --systemd-user-uninstall)
    systemd_uninstall
    exit 0
    ;;
  "")
    ;;
  *)
    die "unknown mode: $MODE"
    ;;
esac

mkdir -p "$SCRIPTS_DIR" || die "failed to create $SCRIPTS_DIR"

cat > "$WRAPPER" <<EOF
#!/usr/bin/env bash
set -euo pipefail
HARNESS_HOME="\${HARNESS_HOME:-${HARNESS_HOME_ABS}}"
CURRENT="\$(cat "\$HARNESS_HOME/engine/CURRENT")"
exec node "\$HARNESS_HOME/engine/versions/\${CURRENT}/src/runner.js" run --repo "${ROOT}" "\$@"
EOF

chmod +x "$WRAPPER" || die "failed to chmod $WRAPPER"

EXCLUDE_FILE="$ROOT/.git/info/exclude"
mkdir -p "$(dirname "$EXCLUDE_FILE")" || die "failed to create $(dirname "$EXCLUDE_FILE")"
touch "$EXCLUDE_FILE"
grep -qxF '.claude/scripts/' "$EXCLUDE_FILE" || echo '.claude/scripts/' >> "$EXCLUDE_FILE"

printf '%s\n' "$WRAPPER"
