#!/usr/bin/env bash # Batch update scripts for IP centralization and error handling # Usage: ./scripts/batch-update-scripts.sh [--ip-only] [--error-only] [--both] set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true MODE="${1:---both}" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } # Find scripts that need updates if [[ "$MODE" == "--ip-only" ]] || [[ "$MODE" == "--both" ]]; then log_info "Finding scripts with hardcoded IPs..." IP_SCRIPTS=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" -exec grep -l "192\.168\.11\." {} \; 2>/dev/null | grep -vE "(centralize|find-hardcoded|check-ip|query-missing|check-vmid)" | head -200) for script in $IP_SCRIPTS; do if ! grep -q "source.*ip-addresses.conf" "$script" 2>/dev/null; then log_info "Updating IP centralization: $script" # Add IP config loading after set -euo pipefail or at top if grep -q "^set -euo pipefail\|^set -uo pipefail\|^set -eo pipefail" "$script"; then sed -i '/^set -[euo]* pipefail/a\ # Load IP configuration\ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\ PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"\ source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true\ ' "$script" elif grep -q "^#!/" "$script"; then # Add after shebang sed -i '1a\ # Load IP configuration\ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"\ PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"\ source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true\ ' "$script" fi fi done fi if [[ "$MODE" == "--error-only" ]] || [[ "$MODE" == "--both" ]]; then log_info "Finding scripts missing error handling..." ERROR_SCRIPTS=$(find "$PROJECT_ROOT/scripts" -name "*.sh" -type f ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/archive/*" -exec sh -c 'head -1 "$1" | grep -q "^#!/" && ! grep -q "set -euo pipefail\|set -uo pipefail\|set -eo pipefail" "$1" && echo "$1"' _ {} \; 2>/dev/null | head -50) for script in $ERROR_SCRIPTS; do log_info "Adding error handling: $script" if grep -q "^#!/" "$script"; then # Add after shebang, before any other code sed -i '1a\ set -euo pipefail\ ' "$script" fi done fi log_success "Batch update complete!"