#!/bin/sh -e

echo -e "#!/bin/sh \ncat /etc/vpnc/resolv.conf-backup >/etc/resolv.conf ">/etc/network/if-up.d/12_vpnc_resolv
chmod 777 /etc/network/if-up.d/12_vpnc_resolv

#
# Configure UDHCP Client.
#

do_PR11() {
	# Dot this for PR1.1 or newer version.
	
	echo '#!/bin/sh -e' > $FILE
	chmod +x $FILE

	# Correct a possibly failed upgrade from PR1.0 to PR1.1.
	if [ -e "${FILE_PR10}.dpkg-dist" ]; then
		cp "${FILE_PR10}.dpkg-dist" "${FILE_PR10}"
		rm -f "${FILE_PR10}.dpkg-dist"
	fi
}

FILE_PR10=/etc/udhcpc/libicd_network_ipv4.script
FILE_PR11=/etc/maemo-dhcp.d/60_vpnc
FILE=$FILE_PR11

# Make sure we start clean.
[ -e $FILE_PR10 ] && sed -i '/VpnDHCPissue/,/Vpnx/d' $FILE_PR10
[ -e $FILE_PR11 ] && rm -f $FILE_PR11

do_PR11

cat >> $FILE << EOF
#VpnDHCPissue fix
if route -n | grep -e "^0\.0\.0\.0" | grep -q tun0; then
    route -n | grep -e "^0\.0\.0\.0" | grep \$interface | awk '{print \$2}' > /tmp/vpnc_route
    ip=\`cat /var/run/vpnc/defaultroute | cut -f3 -d' '\`
    if [ -z \$ip ]; then
        killall vpnc
    else
        if route -n | grep -e "^0\.0\.0\.0" | grep -q \$ip; then
            route delete default \$interface
        fi
    fi
fi
#Vpnx
EOF

exit 0

