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

restart_failed() {
    echo E: service restart failed
    if command journalctl 2>/dev/null ; then
	journalctl -n200 --no-pager
    fi
    exit 1
}

sed -e '/recursor:/a \  auth_zones: [{zone: "example.org", file: "/etc/powerdns/example.org.zone"}]' -i /etc/powerdns/recursor.conf

cat <<EOF >/etc/powerdns/example.org.zone
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.123
EOF

service pdns-recursor restart || restart_failed

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

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

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

