#!/system/bin/sh

dns1="$(getprop net.dns1)"
dns2="$(getprop net.dns2)"
eth0_dns1="$(getprop dhcp.eth0.dns1)"
eth0_dns2="$(getprop dhcp.eth0.dns2)"

RESOLV_CONF="/etc/resolv.conf"
TMP_FILE="/storage/resolv.conf.$$"
DEFAULT_DNS="8.8.8.8 8.8.4.4"

CONTENT="nameserver"
for dns in "$dns1" "$dns2" "$eth0_dns1" "$eth0_dns2"
do
	if [ -z "$dns" ] ; then
		continue
	fi
	if [[ "$CONTENT" == *"$dns"* ]] ; then
		continue
	fi
	if [ "$dns" = "0.0.0.0" ] ; then
		continue
	fi
	CONTENT="$CONTENT $dns"
done

if [ "$CONTENT" = "nameserver" ] ; then
	CONTENT="$CONTENT $DEFAULT_DNS"
fi

echo "$CONTENT" > "$TMP_FILE"

if [ "$CONTENT" != "$(cat $RESOLV_CONF 2> /dev/null)" ] ; then
	cp "$TMP_FILE" "$RESOLV_CONF"
fi
rm -f "$TMP_FILE"

exit 0
