#!/bin/bash
#
# restores/update original resolv.conf
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
CMDVER="2.2"
CMDSTR="$CMD v.$CMDVER (2023-08-26)"

set -u



# update cache for kuwatcher
#
update_cache_resolv()
{
	[ -d /tmp/kuwatcher ] && {
		cp -a /etc/resolv.conf /tmp/kuwatcher
	}
	return 0
}

cleanup()
{
	rm -f $tmpfile $tmpfile2 $resolv.new
}


# (MAIN)

fixed="/etc/kusa/resolv.conf"
resolv="/etc/resolv.conf"
mydomain=$(uname -n | sed -e 's/[^.]*.//')
tmpfile=$(mktemp /tmp/$CMD-XXXXXXXXX) || exit $?
tmpfile2=$(mktemp /tmp/$CMD-XXXXXXXXX) || exit $?

DEBUG=${DEBUG:-false}

if $DEBUG
then
	pdebug() { echo "D# $*"; }
	dlogger() { echo "L# $*"; }
else
	pdebug() { :; }
	dlogger() { logger -t $CMD "$@"; }
fi

trap 'echo "*INTR*"; cleanup; exit 255' 1 2 3
trap 'cleanup' EXIT
trap 'echo -e "\nunexpected error $? at $LINENO\n"' ERR

[ -f $fixed ] || {
	echo " fixed file '$fixed' not exists, noop"
	update_cache_resolv
	exit 0
}

[ -f $resolv ] || {
	echo " '$resolv' doesn't exists, install kusa one"
	cp -a "$fixed" "$resolv"
	update_cache_resolv
	exit 0
}

cmp $resolv $fixed >/dev/null 2>/dev/null && {
	echo " $resolv == $fixed, noop"
	update_cache_resolv
	exit 0
}

# is really changed?
#
sed -e 's/[ ,	]*#.*//' -e '/^$/d' $resolv >$tmpfile
sed -e 's/[ ,	]*#.*//' -e '/^$/d' $fixed >$tmpfile2
cmp $tmpfile $tmpfile2 >/dev/null 2>/dev/null && {
	echo " $resolv == $fixed (w/out comments), noop"
	update_cache_resolv
	exit 0
}

fixed_domain=$(grep "^domain" $fixed | sed -e 's/domain[ ,	]*//')
fixed_search=$(grep "^search" $fixed | sed -e 's/search[ ,	]*//')
fixed_server=$(grep "^nameserver" $fixed | sed -e 's/nameserver[ ,	]*//')

new_domain=$(grep "^domain" $resolv | sed -e 's/domain[ ,	]*//')
new_search=$(grep "^search" $resolv | sed -e 's/search[ ,	]*//')
new_server=$(grep "^nameserver" $resolv | sed -e 's/nameserver[ ,	]*//')

pdebug "fixed_domain: $fixed_domain"
pdebug "fixed_search: $fixed_search"
pdebug "fixed_server: $fixed_server"
pdebug "new_domain: $new_domain"
pdebug "new_search: $new_search"
pdebug "new_server: $new_server"

echo "$new_server"	>/var/run/resolv-server-dhcp

new=${resolv}.new
rm -f $new

msg=""

# cleanup up search sring
#
newlist="$fixed_search"
autolist=

for domain in $new_search
do
	pdebug "check_new_search: eval $domain"
	must_add=true
	for ckdomain in $newlist
	do
		grep -q "^$domain" /etc/ku-resolv-blacklist && {
			echo " search, don't add '$domain': blacklisted"
			must_add=false
			break
		}
		[ $domain == $ckdomain ] && {
			must_add=false
			break
		}
	done
	$must_add || continue
	pdebug "check_new_search: add new $domain"
	newlist="$newlist $domain"
	autolist="$autolist $domain"
done
new_search=$(echo $newlist)
pdebug "new_search (after check): $new_search"

if [ "$new_search" != "" -a "$new_search" != "$fixed_search" ]
then
	msg="new_search=$new_search "
	sed -e "s/^search .*/search $new_search/" $fixed > $new
else
	cp $fixed $new
fi

echo -e "
# original $resolv
#
" >>$new
sed -e 's/^/# /' $resolv >>$new

set -e
chown --reference=$resolv $new
chmod --reference=$resolv $new
mv $resolv $resolv.bak
mv $new $resolv
set +e

dlogger "$resolv updated: $msg"



# dnslocal (tinydns)?
#
if [ "X$fixed_server" != "X$new_server" ]
then
	dnslocal="/etc/dnslocal/root"
	dnscachex="/etc/dnscachex/root/servers"
	domain=
	server=$(echo $new_server | cut -d' ' -f1)	# keep only first server in the list
	dns_needs_restart=false


	if [ -d $dnslocal ]
	then
		rm -f $dnslocal/auto-*.resolv
		for domain in $autolist 
		do
			[ $domain == $mydomain ] && continue
			grep -q "^$domain" /etc/ku-resolv-blacklist && continue
			dlogger "adding $domain $server to dnslocal"
			echo "$domain $server" >$dnslocal/auto-kusa-$domain.resolv
		done
		dns_needs_restart=true
	elif [ -d $dnscachex ]
	then
		# not dnslocal but has dnscachex?
		#
		for domain in $new_search
		do
			[ $domain == $mydomain ] && continue
			echo "$server" >$dnscachex/$domain
			dlogger "adding $domain $server to dnscachex"
		done
		dns_needs_restart=true
	fi

	$dns_needs_restart && {
		if $DEBUG
		then
			pdebug "(BUILD AND RESTART DNS)"
		else
			[ -d $dnslocal ] && (cd $dnslocal ; rm -f data; make)
			[ -f /etc/init.d/ku-djbdns ] && {
				sh /etc/init.d/ku-djbdns stop
				sleep 1
				sh /etc/init.d/ku-djbdns start
			}
		fi
	}
fi

update_cache_resolv

exit 0
