#!/bin/sh 

DOMAIN=nsec.arnor.org

if [ "$#" -ne 1 ]; then
        echo "Illegal number of parameters"
        echo "Syntax is $0 'unwind|dnsdist'"
        exit 1
fi

NAME=$1

#####################################################################################################

if [ $NAME == "unwind" ] 
then
echo '#/etc/unwind.conf'
echo
host -t A ${DOMAIN} | awk '{print $4}' | while read IP  
do
        echo $(host $IP | awk '{print $5}'|sed 's/\.$//')=\"$IP authentication name ${DOMAIN} DoT\"
done
echo
echo forwarder {
host -t A ${DOMAIN} | awk '{print $4}' | while read IP
do
	echo "	"\$$(host $IP | awk '{print $5}'|sed 's/\.$//')
done
echo }
echo
echo preference { DoT forwarder recursor }
fi
#####################################################################################################

if [ $NAME == "dnsdist" ] 
then
echo '#/etc/dnsdist/dnsdist.conf'
echo
cat <<EOF
addACL('127.0.0.1/8')
controlSocket('127.0.0.1:5199')
addLocal('127.0.0.1:53', {reusePort=false})
setServerPolicy(whashed)

EOF

host -t A ${DOMAIN} | awk '{print $4}' | while read IP
do
	echo newserver\(\{address=\"${IP}\",healthCheckMode=\"lazy\", name=\"$(host $IP | awk '{print $5}'|sed 's/\.$//')\", tls=\"openssl\", subjectName=\"${DOMAIN}\", validateCertificates=true\}\)
done
cat <<EOF

pc = newPacketCache(100000, {maxTTL=86400, minTTL=0, temporaryFailureTTL=60, staleTTL=86400, dontAge=false, keepStaleData=true})
getPool(""):setCache(pc)
setStaleCacheEntriesTTL(86400)
EOF
fi
