#!/bin/sh
set -eu

DOMAIN="https://archicratie.trans-hands.synology.me"
A="http://127.0.0.1:8081"
B="http://127.0.0.1:8082"

etag() { curl -ksI "$1/" | awk -F': ' 'tolower($1)=="etag"{print $2}' | tr -d '\r'; }

E_D="$(etag "$DOMAIN")"
E_A="$(etag "$A")"
E_B="$(etag "$B")"

echo "DOMAIN ETag: $E_D"
echo "8081   ETag: $E_A"
echo "8082   ETag: $E_B"

if [ -n "$E_D" ] && [ "$E_D" = "$E_A" ]; then
  echo "LIVE=8081 (slot blue probable)"
elif [ -n "$E_D" ] && [ "$E_D" = "$E_B" ]; then
  echo "LIVE=8082 (slot green probable)"
else
  echo "LIVE=INCONNU (ETag mismatch)"
fi
