#!/bin/bash
exec 2>&1
set -ex

echo /etc/powerdns/pdns.d/pdns.local.gmysql.conf follows:
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
cat  /etc/powerdns/pdns.d/pdns.local.gmysql.conf
echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

# gmysql-dbname=pdnsbackendmysql
DBNAME=$(grep gmysql-dbname /etc/powerdns/pdns.d/pdns.local.gmysql.conf | awk -F= '{print $2}')

cat <<EOF >/etc/powerdns/example.org
example.org.           172800  IN      SOA     ns1.example.org. dns.example.org. 1 10800 3600 604800 3600
example.org.           172800  IN      NS      ns1.example.org.
smoke.example.org.     172800  IN      A       127.0.0.124
EOF

zone2sql --gmysql --zone-name=example.org --zone=/etc/powerdns/example.org | \
    mysql -uroot $DBNAME

service pdns restart

TMPFILE=$(mktemp)
cleanup() {
  rm -f "$TMPFILE"
  service pdns stop
}
trap cleanup EXIT

dig @127.0.0.1 smoke.example.org 2>&1 | tee "$TMPFILE"

if grep -c '127\.0\.0\.124' "$TMPFILE"; then
    echo success
else
    echo smoke.example.org could not be resolved
    exit 1
fi

