#!/usr/bin/env bash # Safe updater for starter scripts (preview or apply) # Usage: # ./update_starters.sh # preview staged starter updates (no replace) # ./update_starters.sh --apply # backup and replace current starters set -euo pipefail IFS=$'\n\t' AI_BRAIN_DIR="${1:-$HOME/Desktop/AI_Brain}" STAGE_DIR="$AI_BRAIN_DIR/.starter_updates" BACKUP_ROOT="$AI_BRAIN_DIR/starter_backups" TIMESTAMP="$(date +%Y%m%d%H%M%S)" BACKUP_DIR="$BACKUP_ROOT/backup_$TIMESTAMP" FILES=(start_all.sh start_adapter.sh start_orchestrator.sh stop_all.sh status_all.sh restart_all.sh) # small guard to prevent accidental 'source' which can close your shell if [ "${BASH_SOURCE[0]}" != "$0" ]; then cat <&2 ERROR: this script must be executed, not sourced. Run it like: ./$(basename "$0") Refusing to run to avoid closing your interactive shell. MSG return 1 2>/dev/null || exit 1 fi mkdir -p "$STAGE_DIR" "$BACKUP_ROOT" # Proposed updated content for starters (minimal safe template) read -r -d '' STARTER_TEMPLATE <<'TEMPL' #!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' AI_BRAIN_DIR="${1:-$HOME/Desktop/AI_Brain}" LOG_DIR="$AI_BRAIN_DIR/logs" mkdir -p "$LOG_DIR" # Guard against being sourced if [ "${BASH_SOURCE[0]}" != "$0" ]; then echo "This script must be executed, not sourced." >&2 return 1 2>/dev/null || exit 1 fi # Example detached start (no tmux required) nohup bash -lc "cd '$AI_BRAIN_DIR' && python3 adapter/adapter.py >> '$LOG_DIR/adapter.log' 2>&1" >/dev/null 2>&1 & nohup bash -lc "cd '$AI_BRAIN_DIR' && python3 orchestrator/orchestrator.py >> '$LOG_DIR/worker.log' 2>&1" >/dev/null 2>&1 & echo "Start requested; scripts detached. Inspect logs in $LOG_DIR" TEMPL # write staged versions for fname in "${FILES[@]}"; do staged="$STAGE_DIR/$fname" printf '%s\n' "$STARTER_TEMPLATE" > "$staged" chmod 755 "$staged" done # show diffs / status printf 'Staged updates written to: %s\n\n' "$STAGE_DIR" for fname in "${FILES[@]}"; do live="$AI_BRAIN_DIR/$fname" staged="$STAGE_DIR/$fname" if [ ! -f "$live" ]; then printf "%-25s : %s\n" "$fname" "NEW (no existing file)" else if diff -q "$live" "$staged" >/dev/null 2>&1; then printf "%-25s : %s\n" "$fname" "IDENTICAL" else printf "%-25s : %s\n" "$fname" "DIFFER (run --apply to replace)\n" echo "---- diff for $fname ----" diff -u "$live" "$staged" || true echo "-------------------------" fi fi done if [ "${1:-}" = "--apply" ] || [ "${2:-}" = "--apply" ]; then mkdir -p "$BACKUP_DIR" for fname in "${FILES[@]}"; do live="$AI_BRAIN_DIR/$fname" staged="$STAGE_DIR/$fname" if [ -f "$staged" ]; then if [ -f "$live" ]; then cp -p "$live" "$BACKUP_DIR/$fname" || true fi cp -p "$staged" "$live" chmod 755 "$live" echo "Replaced $live (backup at $BACKUP_DIR/$fname)" fi done echo "Updates applied; backups in: $BACKUP_DIR" else echo echo "Preview only. To apply staged updates run:" echo " (cd \"$AI_BRAIN_DIR\" && ./update_starters.sh --apply)" fi