#!/bin/bash
set -e

cmd="$@"

until PGPASSWORD=test psql -h "testdb" -U "test" -c 'SELECT 1 FROM public.container_ready' postgres; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - Waiting for the reboot"
sleep 3 # Wait for the Postgres reboot at the end of setup

until PGPASSWORD=test psql -h "testdb" -U "test" -c 'SELECT 1 FROM public.container_ready' postgres; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done


>&2 echo "Postgres is up - executing command"
exec $cmd
