#!/usr/bin/env bash # Install/refresh the live watcher under $HOME/heavy-duty-watcher and start tmux loops. set -euo pipefail SRC="$(cd "$(dirname "$0")/.." && pwd)" DEST="${DEST:-$HOME/heavy-duty-watcher}" TMUX_SESSION="${TMUX_SESSION:-heavy-duty-watcher}" missing=() for c in bash rsync jq tmux; do command -v "${c}" >/dev/null 2>&1 || missing+=("${c}") done if ((${#missing[@]})); then echo "error: missing required tools: ${missing[*]}" >&2 echo "install them and re-run (e.g. apt-get install rsync jq tmux)" >&2 exit 1 fi # Soft deps depending on backend if [[ "${FORGE_BACKEND:-github}" == "github" ]] && ! command -v gh >/dev/null 2>&1; then echo "warn: gh not found (required for FORGE_BACKEND=github)" >&2 fi if [[ "${FORGE_BACKEND:-github}" == "forgejo" ]] && ! command -v stoke >/dev/null 2>&1; then echo "warn: stoke not found (required for FORGE_BACKEND=forgejo)" >&2 fi echo "Installing watcher: ${SRC} -> ${DEST}" mkdir -p "${DEST}" # Copy tree but preserve local state/logs if DEST already has them rsync -a --exclude '.git' --exclude 'state.json' --exclude 'logs/' --exclude '.poll-prompt.txt' \ "${SRC}/" "${DEST}/" mkdir -p "${DEST}/logs" if [[ ! -f "${DEST}/state.json" ]]; then NOW="$(date -u +%Y-%m-%dT%H:%M:%SZ)" jq --arg t "$NOW" '.created_at=$t | .last_poll_at=null' \ "${DEST}/config/state.template.json" > "${DEST}/state.json" echo "seeded state.json" fi chmod +x "${DEST}/scripts/"*.sh # lib is sourced, not executed chmod +x "${DEST}/scripts/lib/"*.sh 2>/dev/null || true # Compatibility symlink for old path if [[ ! -e "$HOME/rig-watcher" ]]; then ln -s "${DEST}" "$HOME/rig-watcher" echo "linked ~/rig-watcher -> ${DEST}" fi # Ensure tmux loops if tmux has-session -t "${TMUX_SESSION}" 2>/dev/null; then echo "tmux ${TMUX_SESSION} already exists" else tmux new-session -d -s "${TMUX_SESSION}" -n poll-loop -c "${DEST}" \ "bash ${DEST}/scripts/poll-loop.sh" tmux new-window -t "${TMUX_SESSION}" -n health-loop -c "${DEST}" \ "bash ${DEST}/scripts/health-loop.sh" tmux new-window -t "${TMUX_SESSION}" -n tools -c "${DEST}" \ "echo 'heavy-duty-watcher tools'; exec bash" echo "started tmux session ${TMUX_SESSION}" fi bash "${DEST}/scripts/health-check.sh" || true echo "Live install ready. Restore after reboot: ${DEST}/scripts/restore.sh"