#!/bin/bash
for MONGODB_URI in "mongodb://localhost:10201,localhost:10202,localhost:10203/paris2024?replicaSet=paris2024&retryWrites=true&readPreference=nearest" "mongodb://sap-p1-1,sap-p1-2/paris2024?replicaSet=security_service&retryWrites=true&readPreference=nearest"; do
  FOUND=$( mongosh --quiet --eval "db.CONNECTIVITY_PARAMS_FOR_RACES_TO_BE_RESTORED.find({type: \"TRAC_TRAC\", useOfficialEventsToUpdateRaceLog: false})" "${MONGODB_URI}" )
  if [ -n "${FOUND}" ]; then
    PARAM_URL=$( mongosh --quiet --eval "db.CONNECTIVITY_PARAMS_FOR_RACES_TO_BE_RESTORED.find({type: \"TRAC_TRAC\", useOfficialEventsToUpdateRaceLog: false}, {_id: 0, paramURL: 1}) "${MONGODB_URI}" )
  echo "The following TracTrac race URL(s) has been connected to without
ticking the \"Automatically update from "official" results published by TracTrac\"
check-box in the ${MONGODB_URI} database:

    ${PARAM_URL}

Please consider removing the tracked race and re-connecting with the corresponding
check-box ticked.
" | notify-operators "Found TRAC_TRAC race in DB without \"official\" checkbox ticked"
    logger -t "sailing" "Found TRAC_TRAC race without \"official\" checkbox in MongoDB ${MONGODB_URI}: ${PARAM_URL}"
  fi
done
