#!/bin/sh

if test "$1" != "configure"; then
	exit 0
fi

icd2_append_ipv6() {

	modules="$(gconftool -g $1)"

	if echo "$modules" | grep -q libicd_network_ipv6.so; then
		return
	fi

	modules=$(echo "$modules" | sed 's/]/,libicd_network_ipv6.so]/')
	gconftool -s -t list --list-type string "$1" "$modules"

}

# Revert old 50_ipv4_network_setup udhcpc script
if dpkg-divert --list ipv6-support | grep 50_ipv4_network_setup; then
	dpkg-divert --package ipv6-support --rename --remove /etc/maemo-dhcp.d/50_ipv4_network_setup
fi

# ICD2 global
osso-ipv6-enable.sh --enable --interface "wlan0 ppp0"

# ICD2 modules
icd2_append_ipv6 /system/osso/connectivity/network_type/WLAN_INFRA/network_modules
icd2_append_ipv6 /system/osso/connectivity/network_type/WLAN_ADHOC/network_modules

# avahi-daemon
if ! grep -q '^use-ipv6=yes$' /etc/avahi/avahi-daemon.conf; then
	sed 's/.*use-ipv6=.*/use-ipv6=yes/' -i /etc/avahi/avahi-daemon.conf
fi

# mDNS
if grep -q mdns4 /etc/nsswitch.conf; then
	sed 's/mdns4/mdns/g' -i /etc/nsswitch.conf
fi

# DNS
if ! grep -q '::1' /etc/resolv.conf; then
	echo 'nameserver ::1' >> /etc/resolv.conf
fi

# hosts
if ! grep -q 'ip6-localhost' /etc/hosts; then
	echo '::1 localhost ip6-localhost ip6-loopback' >> /etc/hosts
fi
if ! grep -q 'ip6-localnet' /etc/hosts; then
	echo 'fe00::0 ip6-localnet' >> /etc/hosts
fi
if ! grep -q 'ip6-mcastprefix' /etc/hosts; then
	echo 'ff00::0 ip6-mcastprefix' >> /etc/hosts
fi
if ! grep -q 'ip6-allnodes' /etc/hosts; then
	echo 'ff02::1 ip6-allnodes' >> /etc/hosts
fi
if ! grep -q 'ip6-allrouters' /etc/hosts; then
	echo 'ff02::2 ip6-allrouters' >> /etc/hosts
fi

# MicroB
if ! grep -q disableIPv6 /home/user/.mozilla/microb/prefs.js; then
	echo 'user_pref("network.dns.disableIPv6", false)' >> /home/user/.mozilla/microb/prefs.js
	chown user:users /home/user/.mozilla/microb/prefs.js
	chmod 644 /home/user/.mozilla/microb/prefs.js
fi
