#!/bin/sh

if test "$1" != "remove" -a "$1" != "purge"; then
	exit 0
fi

icd2_delete_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//g')
	gconftool -s -t list --list-type string "$1" "$modules"

}

# ICD2 global
if ! test -z "$(which osso-ipv6-disable.sh)"; then
	osso-ipv6-disable.sh --disable --default
fi

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

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

# DNS
if grep -q '::1' /etc/resolv.conf; then
	sed '/nameserver ::1/d' -i /etc/resolv.conf
fi

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

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