#!/bin/sh /etc/rc.common

START=10

# At boot, before dnsmasq starts, DNS can't go through the VPS Pi-hole yet:
# it is reached at the tunnel address of the VPN (10.255.x.y), and the
# tunnel isn't up. Use the local resolver until then. omr-tracker's 003-up
# runs set_pihole once the tunnel is up, while the VPS has Pi-hole and
# 127.0.0.1#5353 is in the list, and that puts the Pi-hole back.
# A list with no upstream at all gets the local resolver too.
#
# start() called disable_pihole_status, which never existed, and _log,
# which init scripts don't have: nothing was done at boot.
start() {
	local _s _removed=0 _servers

	# DNS set by hand: not ours to change
	[ "$(uci -q get openmptcprouter.settings.pihole_auto_conf)" = "0" ] && return 0
	for _s in $(uci -q get dhcp.@dnsmasq[0].server); do
		case "$_s" in
			10.255.*#53)
				uci -q del_list dhcp.@dnsmasq[0].server="$_s"
				_removed=1
				;;
		esac
	done
	_servers="$(uci -q get dhcp.@dnsmasq[0].server)"
	if [ "$_removed" = "1" ] || [ "$_servers" = "/lan/ /use-application-dns.net/" ]; then
		case " $_servers " in
			*" 127.0.0.1#5353 "*) ;;
			*) uci -q add_list dhcp.@dnsmasq[0].server='127.0.0.1#5353' ;;
		esac
		uci -q commit dhcp
		logger -t "OMR-Pi-hole" "Local resolver for the boot, the VPS Pi-hole comes back with the tunnel"
	fi
}
