#!/bin/sh
#
# can run every 60 secs with e.g. in /etc/rc.local
# while :; do sleep 60; /usr/bin/checkdns.sh; done &
#
# download and install:
# wget -O /usr/bin/checkdns.sh http://intercity-vpn.de/files/openwrt/checkdns.sh; chmod +x /usr/bin/checkdns.sh
#
# test via:
# killall dnsmasq; /usr/bin/checkdns.sh
#
# restart-reasons in email, see dnsmasq_check()
# 1 = reserved for future use
# 2 = no dnsmasq process found
# 3 = local query failed
# 4 = internet AND local query failed

log()
{
	local message="$1"
	local debug="$2"
	local file='/www/logfile.txt'
	local mail_to='h2o-post@gmx.net bb@npl.de'
	local uniq_id="$( uci get system.@system[0].hostname )@$( uci get network.lan.ipaddr )"

	set -x
	echo "$(date) $hostname: dnscheck: $message" >>"$file"
	set +x

	if [ -n "$debug" ]; then
		return
	else
		mail_it	"[dnscheck] $uniq_id" "was error: $message" "$mail_to"
		autoupdate
	fi
}

autoupdate()
{
	local url='http://intercity-vpn.de/files/openwrt/checkdns.sh'

	wget -O "$0" "$url"
}

mail_it()
{
	local subject="$( urlencode "$1" )"
	local message="$( urlencode "$2" )"
	local list_recipient="$3"
	local recipient params
	local url='http://bwireless.mooo.com/cgi-bin-tool.sh?OPT=minimail'

	for recipient in $list_recipient; do {
		recipient="$( urlencode "$recipient" )"
		params="&RECIPIENT=${recipient}&SUBJECT=${subject}&MESSAGE=${message}"
		wget -O - "${url}${params}"
	} done
}

urlencode()
{
	local text="$1"
	local pos=0

	while [ $pos -lt ${#text} ]; do {
		set -- $( echo "${text:$pos:1}" | hexdump -C )
		pos=$(( pos + 1 ))
		printf '%s' "%${2}"
	} done
}

dnsmasq_testquery()
{
	local domain="$1"

	nslookup "$domain" 2>/dev/null >/dev/null
}

get_testdomain()
{
	local domain_files word

	domain_files="$( uci -q get 'dhcp.@dnsmasq[0].addnhosts' )"
	domain_files="$domain_files /var/run/hosts_olsr /var/etc/hosts.olsr"
	domain_files="$domain_files /etc/hosts"

	for file in $domain_files; do {
		testdomain="$( grep -s ^"[0-9]" "$file" | head -n1 )"

		for word in $testdomain; do {
			case "$word" in
				[0-9]*)
				;;
				*)
					set -x
					echo "$word"
					set +x
					return
				;;
			esac
		} done
	} done
}

dnsmasq_check()
{
	local testserver='intercity-vpn.de'
	local local_domain_files="$( uci -q get 'dhcp.@dnsmasq[0].addnhosts' ) /etc/hosts"
	local testdomain

	if [ -x '/etc/init.d/dnsmasq' ]; then
		pidof dnsmasq >/dev/null || return 2		# nopid
	else
		return 0					# ok
	fi

	testdomain="$( get_testdomain )"

	if [ -z "$( ip route list exact '0.0.0.0/0' )" ]; then
		# local test
		dnsmasq_testquery "$testdomain" || {
			return 3				# no_query: local
		}
	else
		# internet test
		dnsmasq_testquery "$$.$testserver" || {
			dnsmasq_testquery "$testdomain" || {
				return 4			# no_query: inet + local
			}
		}
	fi

	return 0
}

patch_for_coredump()
{
	local file='/etc/init.d/dnsmasq'
	local pattern='procd_open_instance'

	grep -q "$pattern"$ "$file" && {
		log "patching '$file' for coredump" debug
		sed -i "s/${pattern}$/&; procd_set_param limits core='unlimited'/" "$file"
	}
}

patch_for_coredump

dnsmasq_check || {
	RC=$?
	/etc/init.d/dnsmasq restart
	log "needed restart - reason: $RC"
}
