#!/bin/bash
set -e

function wait_for_db() {
  echo "wait for db container.."
  timeout_seconds=10
  max_retries=10
  retry_count=0
  until pg_isready --host gubrist; do
    if ((retry_count >= max_retries)); then
      echo "Timeout reached, shutting down.."
      exit 1
    fi
    retry_count=$(($retry_count + 1))
    echo "Postgres is unavailable - sleeping for $(($timeout_seconds / $max_retries)) second ($retry_count)"
    sleep $(($timeout_seconds / $max_retries))
  done
}

wait_for_db
