#!/usr/bin/env bash
# Poll version.php every 10s for up to 3 minutes; PASS if git_head matches expected commit.

EXPECTED_COMMIT="34ea77e"
URL="https://projectlando.net/genie/dev/version.php"

echo "Polling version endpoint for up to 180 seconds..."
echo "Expected commit: $EXPECTED_COMMIT"
echo "-----------------------------------------"

for i in {1..18}; do
  echo "Attempt $i:"

  RESPONSE=$(curl -s $URL)
  LIVE_HEAD=$(echo "$RESPONSE" | grep -o '"git_head":"[^"]*"' | cut -d'"' -f4)

  echo "Live git_head: $LIVE_HEAD"

  if [[ "$LIVE_HEAD" == "$EXPECTED_COMMIT"* ]]; then
    echo ""
    echo "✅ DEPLOY CONFIRMED — Server updated."
    exit 0
  fi

  echo "Not updated yet. Waiting 10 seconds..."
  sleep 10
done

echo ""
echo "❌ DEPLOY NOT UPDATED AFTER 180 SECONDS"
echo "Server git_head remains at: $LIVE_HEAD"
exit 1
