#!/usr/bin/env bash # Cleanup Empty Comment Sections in Besu Config Files # Removes empty comment headers left after deprecated option cleanup set -euo pipefail # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" # Dry-run mode flag DRY_RUN="${1:-}" log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Function to backup a file backup_file() { local file="$1" if [ -f "$file" ]; then local backup="${file}.backup.$(date +%Y%m%d_%H%M%S)" if [ "$DRY_RUN" != "--dry-run" ]; then cp "$file" "$backup" echo "$backup" else echo "${file}.backup.TIMESTAMP" fi fi } # Function to clean empty comment sections clean_empty_comments() { local file="$1" if [ ! -f "$file" ]; then log_warn "File not found: $file (skipping)" return 1 fi # Count empty sections before cleanup local empty_before=$(grep -cE '^#[^#]' "$file" 2>/dev/null | awk '{s+=$1} END {print s+0}' || echo "0") log_info "Cleaning empty comment sections from: $file" if [ "$DRY_RUN" != "--dry-run" ]; then # Backup file local backup=$(backup_file "$file") log_info " Backup created: $backup" # Use Python to clean empty comments more reliably # Pattern: comment line followed by blank lines until next section/key python3 <