#!/bin/sh

[ -r /etc/options.cfg ] && . /etc/options.cfg

#use fqdn for queries
echo "domain-needed"

#set asynchronous logging
echo "log-async${DNSMASQ_LOG_ASYNC:+=$DNSMASQ_LOG_ASYNC}"

#don't read /etc/resolv.conf, it's pointing local. but allow user-defined files
grep -i "^resolv-file=" /tmp/flash/dnsmasq/dnsmasq.extra -q 2>/dev/null || echo "no-resolv"

#add avm upstream-servers
if [ "$DNSMASQ_AVM_DNS" = yes ]; then
	echo "server=$(echo 'servercfg.dns1' | ar7cfgctl -s)"
	echo "server=$(echo 'servercfg.dns2' | ar7cfgctl -s)"
fi

#add own upstream-servers
if [ -n "$DNSMASQ_UPSTREAM" ]; then
	for dnsserver in $DNSMASQ_UPSTREAM; do
		echo "server=$dnsserver"
	done
fi

[ "$DNSMASQ_BOGUSPRIV" = "yes" ] && echo "bogus-priv"

if [ "$DNSMASQ_DHCP" = "yes" ]; then
	echo "$DNSMASQ_DHCP_RANGE" | grep -v "^#" | while read -r range; do
		[ -n "$range" ] && echo "dhcp-range=$range"
	done
fi

if [ "$DNSMASQ_DOMAIN" ]; then
	echo "domain=$DNSMASQ_DOMAIN"
	echo "expand-hosts"
fi

if [ "$DNSMASQ_TFTP" = 'yes' -a -n "$DNSMASQ_TFTP_TFTPROOT" ]; then
	echo "enable-tftp"
	echo "tftp-root=$DNSMASQ_TFTP_TFTPROOT"
	echo "tftp-no-fail"
fi

if [ "$DNSMASQ_DHCP" = "yes" -a "$DNSMASQ_DHCP_BOOT" = "yes" -a "$DNSMASQ_DHCP_BOOTFILE" ]; then
	echo "dhcp-boot=$DNSMASQ_DHCP_BOOTFILE"
fi

[ "$DNSMASQ_ETHERS" = "yes" ] && echo "read-ethers"

[ "$DNSMASQ_STOP_DNS_REBIND" = "yes" ] && echo "stop-dns-rebind"

[ "$DNSMASQ_LOG_QUERIES" = "yes" ] && echo "log-queries"

if [ "$FREETZ_PACKAGE_DNSMASQ_WITH_DNSSEC" = "y" -a "$DNSMASQ_DNSSEC" = "yes" ]; then
	[ -r /mod/etc/default.dnsmasq/trust-anchors.conf ] && echo "conf-file=/mod/etc/default.dnsmasq/trust-anchors.conf"
	echo "dnssec"
	echo "dnssec-timestamp=/etc/version"
fi

isdhcphost () {
	echo $1 | grep '^dhcp-host$' >/dev/null
}
[ -r /tmp/flash/exhosts ] && mv /tmp/flash/exhosts /tmp/flash/mod/hosts
if [ -r /tmp/flash/mod/hosts ]; then
	egrep -v '^(#|[[:space:]]*$)' /tmp/flash/mod/hosts |
		while read -r ip mac interface host desc; do
			isdhcphost $mac && echo "dhcp-host=${host}"
		done
fi
