#!/usr/bin/env bash

# Fix image versions - Replace :latest with specific versions

set -e

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

# Image versions
BESU_VERSION="23.10.0"
BLOCKSCOUT_VERSION="v5.1.5"
PROMETHEUS_VERSION="v2.45.0"
BUSYBOX_VERSION="1.36"

echo "Fixing image versions..."

# Fix Besu images
find "$PROJECT_ROOT/k8s" -name "*.yaml" -type f -exec sed -i "s|hyperledger/besu:latest|hyperledger/besu:${BESU_VERSION}|g" {} \;
find "$PROJECT_ROOT/helm" -name "*.yaml" -type f -exec sed -i "s|hyperledger/besu:latest|hyperledger/besu:${BESU_VERSION}|g" {} \;

# Fix Blockscout images
find "$PROJECT_ROOT/k8s" -name "*.yaml" -type f -exec sed -i "s|blockscout/blockscout:latest|blockscout/blockscout:${BLOCKSCOUT_VERSION}|g" {} \;

# Fix Prometheus images
find "$PROJECT_ROOT/monitoring" -name "*.yaml" -type f -exec sed -i "s|prom/prometheus:latest|prom/prometheus:${PROMETHEUS_VERSION}|g" {} \;

# Fix Busybox images
find "$PROJECT_ROOT/k8s" -name "*.yaml" -type f -exec sed -i "s|busybox:latest|busybox:${BUSYBOX_VERSION}|g" {} \;
find "$PROJECT_ROOT/helm" -name "*.yaml" -type f -exec sed -i "s|busybox:latest|busybox:${BUSYBOX_VERSION}|g" {} \;

# Fix Helm values
sed -i "s|tag: latest|tag: ${BESU_VERSION}|g" "$PROJECT_ROOT/helm/besu-network/values.yaml"

echo "✓ Image versions fixed"
echo "Besu: ${BESU_VERSION}"
echo "Blockscout: ${BLOCKSCOUT_VERSION}"
echo "Prometheus: ${PROMETHEUS_VERSION}"
echo "Busybox: ${BUSYBOX_VERSION}"

