#!/bin/sh

test "0" = "$(nvram get ff_dnsmasq)" && exit

. /etc/functions.sh

write_config()
{
	WAN_DNS=$(nvram get wan_dns)

	# No DNS: Use a well known NS
	test -z "$WAN_DNS" && WAN_DNS=195.50.140.250

	WAN_DOMAIN_LOCAL=
	WAN_DOMAIN_DOMAIN=
	test -n "$WAN_DOMAIN" && WAN_DOMAIN_LOCAL="local=/.$WAN_DOMAIN/"
	test -n "$WAN_DOMAIN" && WAN_DOMAIN_DOMAIN="domain=$WAN_DOMAIN"

	cat>/var/etc/dnsmasq.conf<<EOF
# filter what we send upstream
#domain-needed
bogus-priv
filterwin2k

# To speed up ssh, all lookups throug dnsmasq
no-resolv
server=$WAN_DNS

# allow /etc/hosts and dhcp lookups via *.lan
addn-hosts=/etc/local.hosts
$WAN_DOMAIN_LOCAL
$WAN_DOMAIN_DOMAIN
expand-hosts

EOF

	if [ -z "$WANOLSR" ]; then
		cat>>/var/etc/dnsmasq.conf<<EOF
# no dns queries from the wan
except-interface=vlan1

EOF
	fi

	DHCPEND=$(nvram get dhcp_num)
	test -z "$DHCPEND" && DHCPEND=4
	if [ $DHCPEND -gt 0 ]; then
		DHCPLEASE=$(nvram get dhcp_lease)
		test -z "$DHCPLEASE" && DHCPLEASE=12h
		test "$DHCPLEASE" = "0" && DHCPLEASE=12h
		DHCPNET=$(echo $LANNET|cut -d'.' -f1-3)
		DHCPBEG=$(nvram get dhcp_start)
		DHCPBEG=${DHCPBEG##*.}
		test -z "$DHCPBEG" && DHCPBEG=100
		DHCPEND=$(( $DHCPBEG + $DHCPEND - 1 ))
		echo "dhcp-range=wired,$DHCPNET.$DHCPBEG,$DHCPNET.$DHCPEND,$LANMSK,$DHCPLEASE">>/var/etc/dnsmasq.conf
	fi

	WLDHCP=$(nvram get ff_wldhcp)
	IFS=\;
	for ENT in $WLDHCP; do
		NET=${ENT%[:,]*}
		MSK=${ENT#*[:,]}
		if [ -n "$NET" ] && [ -n "$MSK" ]; then
			DHCPLEASE=2h
			DHCPBEG=$(ipcalc -n $NET|cut -d'.' -f4)
			DHCPBEG=$(( $DHCPBEG + 2 ))
			DHCPEND=$(ipcalc -b $NET|cut -d'.' -f4)
			DHCPEND=$(( $DHCPEND - 1 ))
			DHCPNET=$(ipcalc -n $NET|cut -d'=' -f2|cut -d'.' -f1-3)
			echo "dhcp-range=wlnat,$DHCPNET.$DHCPBEG,$DHCPNET.$DHCPEND,$MSK,$DHCPLEASE">>/var/etc/dnsmasq.conf
		fi
	done

	WAN_HOSTNAME=$(nvram get wan_hostname)
	if test -n "$WAN_HOSTNAME" && test -n "$LANADR"; then
		echo "address=/$WAN_HOSTNAME/$LANADR">>/var/etc/dnsmasq.conf
		echo "address=/$WAN_HOSTNAME.$WAN_DOMAIN/$LANADR">>/var/etc/dnsmasq.conf
	fi

	cat>>/var/etc/dnsmasq.conf<<EOF
dhcp-leasefile=/var/run/dhcp.leases

# allow a /etc/ethers for static hosts
read-ethers

# other useful options:
#      subnet mask: dhcp-option=wired,1,255.255.255.0
# default route(s): dhcp-option=wired,3,192.168.1.1,192.168.1.2
#    dns server(s): dhcp-option=wired,6,192.168.1.1,192.168.1.2
#   broadcast addr: dhcp-option=wired,28,192.168.1.255
#   broadcast addr: dhcp-option=wlnat,28,172.31.255.255
EOF

	test -f /etc/local.dnsmasq.conf && cat>>/var/etc/dnsmasq.conf</etc/local.dnsmasq.conf
}

case $1 in
	start)
		echo "Starting dnsmasq..."
		WAN_DOMAIN=$(nvram get wan_domain)
		export NVRAM_wan_ifname
		export NVRAM_lan_ifname
		export NVRAM_lan_ifnames
		eval $(/usr/bin/netparam)
		write_config

		rm -f /var/etc/resolv.conf
		echo "search $WAN_DOMAIN">/var/etc/resolv.conf
		echo "nameserver 127.0.0.1">>/var/etc/resolv.conf
		# Compatibility with openwrt
		ln -sf /var/etc/resolv.conf /tmp/resolv.conf

		/usr/sbin/dnsmasq
	;;
	stop)
		echo "Stopping dnsmasq..."
		killall dnsmasq
	;;
	restart)
		$0 stop
		$0 start
	;;
	*)
		echo "Usage: $0 start|stop|restart"
	;;
esac
