#! /usr/bin/env bash

sleep 5

export PDNS_RECURSOR_HOST="$(getent hosts ${PDNS_RECURSOR_HOST} | cut -d' ' -f1)"

. /common.sh

replace_files

for index in {1..2}
do
  index=SLAVE"${index}"
  pg_disabled=POSTGRES_DISABLED_"${index}"
  pg_host=POSTGRES_HOST_"${index}"

  if [ "${!pg_disabled}" != 1 ] && [ "${!pg_host}" != "" ]
  then
    echo "Found slave ${index}"
    pg_db=POSTGRES_DB_"${index}"
    pg_user=POSTGRES_USER_"${index}"
    pg_pwd=POSTGRES_PASSWORD_"${index}"
    cat << EOF >> /etc/powerdns/pdns.conf
launch+=gpgsql:slave${index}
gpgsql-slave${index}-host=${!pg_host}
gpgsql-slave${index}-port=
gpgsql-slave${index}-dbname=${!pg_db}
gpgsql-slave${index}-user=${!pg_user}
gpgsql-slave${index}-password=${!pg_pwd}
gpgsql-slave${index}-dnssec=yes
EOF
  fi
done

pdns_server --daemon=no
