#!/usr/bin/env bash
# Configure BasicAuthPassword for all 15 Cariflex charging stations
# This allows OCPP 2.0.1 simulators to connect via Security Profile 1

set -e

CitRINEOS_URL="http://localhost:8081"
PASSWORD="DEADBEEFDEADBEEF"

echo "=== Configuring BasicAuthPassword for all stations ==="

for i in $(seq 1 15); do
  # Format: CP001, CP002, ... CP015
  CP_ID=$(printf "CP%03d" $i)
  
  response=$(curl -s -o /dev/null -w "%{http_code}" --location --request PUT \
    "${CitRINEOS_URL}/data/monitoring/variableAttribute?stationId=${CP_ID}&setOnCharger=true" \
    --header "Content-Type: application/json" \
    --data-raw "{
      \"component\": { \"name\": \"SecurityCtrlr\" },
      \"variable\": { \"name\": \"BasicAuthPassword\" },
      \"variableAttribute\": [{ \"value\": \"${PASSWORD}\" }],
      \"variableCharacteristics\": { \"dataType\": \"passwordString\", \"supportsMonitoring\": false }
    }")
  
  if [ "$response" -ge 200 ] && [ "$response" -lt 300 ]; then
    echo "✅ ${CP_ID}: BasicAuthPassword configured"
  else
    echo "❌ ${CP_ID}: Failed (HTTP ${response})"
  fi
done

echo "=== Done ==="
