#!/usr/bin/env bash

# Wrapper script for configure-network.py
# Provides easy access to the configuration tool

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/init.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"


# Check Python version
if ! command -v python3 &> /dev/null; then
    log_error "Error: Python 3 is required"
    exit 1
fi

PYTHON_VERSION=$(python3 --version | cut -d' ' -f2 | cut -d'.' -f1,2)
REQUIRED_VERSION="3.8"

if [ "$(printf '%s\n' "$REQUIRED_VERSION" "$PYTHON_VERSION" | sort -V | head -n1)" != "$REQUIRED_VERSION" ]; then
    log_error "Error: Python 3.8 or higher is required (found $PYTHON_VERSION)"
    exit 1
fi

# Check if script exists
if [ ! -f "$SCRIPT_DIR/configure-network.py" ]; then
    log_error "Error: configure-network.py not found"
    exit 1
fi

# Run configuration tool
log_success "Starting Besu Network Configuration Tool..."

cd "$PROJECT_ROOT"
python3 "$SCRIPT_DIR/configure-network.py" "$@"

exit_code=$?

if [ $exit_code -eq 0 ]; then
    log_success "Configuration complete!"
else
    log_error "Configuration failed with exit code $exit_code"
    exit $exit_code
fi

