#!{{ bash_bin }}

#set -e

curl () {
  {{ curl_bin }} -k --silent -H "Accept: application/json" "$@"
}

get_token () {
  curl -s -X POST --data '{"username":"{{ username }}","password":"{{ password}}"}' {{ secure_url }}/api/login
}

wait_database () {
  NAME=$1
  for retry in {1..50}; do
    echo ">> Wait until $NAME databases are ready...";
    CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" {{ secure_url }}/api/clusters/$NAME/actions/waitdatabases);
    if [ $CODE -eq 504 ]; then
      # We have a timeout try again
      echo ">> [$retry] Timeout on {{ secure_url }}/api/clusters/$NAME/actions/waitdatabases, trying again...";
    else
      if [ $CODE -eq 200 ]; then
        break;
      else
        if [ $CODE -eq 401 ]; then
          # try again with new token
          TOKEN=$(get_token | {{ jq_bin }} -r '.token')
        fi
      fi
      echo ">> [$retry] waitdatabases returned code $CODE...";
    fi
    sleep 30
  done
}

activate_proxy () {
  NAME=$1
  URL="{{ secure_url }}/api/clusters/$NAME/settings/actions/switch/database-hearbeat"
  echo ">> Calling $URL...";
  CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" $URL)
  if [ $CODE -eq 200 ]; then
    return 0;
  else
    echo ">> ERROR: failed to activate proxy: $URL returned code $CODE"
    return 1;
  fi
}

TOKEN=$(get_token | {{ jq_bin }} -r '.token')

# Always reload cluster configuration to apply recent changes
{% for name in cluster_name_list -%}
# reload {{ name }} settings
echo "Reloading settings for {{ name }}..."
curl -H "Authorization: Bearer ${TOKEN}" \
  {{ secure_url }}/api/clusters/{{ name }}/settings/actions/reload

# Start Replication on {{ name }}
if [ ! -f "{{ parameter_dict['bootstrap'] }}/{{ name }}_bootstrapped" ]; then

  wait_database {{ name }}

  echo "Bootstrap replication on {{ name }}..."
  TOKEN=$(get_token | {{ jq_bin }} -r '.token')

  curl -H "Authorization: Bearer ${TOKEN}" \
    {{ secure_url }}/api/clusters/{{ name }}/actions/replication/cleanup
  CODE=$(curl -H "Authorization: Bearer ${TOKEN}" -o /dev/null -w "%{http_code}" {{ secure_url }}/api/clusters/{{ name }}/actions/replication/bootstrap/master-slave)
  SUCCESS=0
  if [ $CODE -eq 200 ]; then
    activate_proxy {{ name }}
    if [ $? -eq 0 ]; then
      # Mark boostrap done!
      echo "Cluster {{ name }} replication bootstrapped"
      echo "DO NOT REMOVE THIS FILE" > {{ parameter_dict['bootstrap'] }}/{{ name }}_bootstrapped
    fi
  else
    echo "ERROR: Failed to bootstrap cluster {{ name }}... http_code $CODE"
  fi
fi

{% endfor %}
