#!/bin/sh
#
# Author: Pali Rohár <pali.rohar@gmail.com>
# License: GPL
#
# icd2 checking return value of rtsol and doing this:
# 255 - spawn dhcpv6
#   0 - nothing
#   1 - spawn dhcpv6
#   2 - spawn dhcpv6
#   3 - nothing
#   4 - ???
#
# return values means:
# 255 - timeout
#   0 - no known RA flags
#   1 - managed flag
#   2 - other flag
#   3 - managed and other flags
#   4 - home agent flag
#
# correct behaviour is:
# 255 - nothing
#   0 - nothing
#   1 - spawn dhcpv6 and do not ask for DNS servers
#   2 - spawn dhcpv6 in information only mode for DNS servers
#   3 - spawn dhcpv6 and ask also for DNS servers
#   4 - ???
#
# so this script will change return value of rtsol for icd2
# and fix dhcpv6 configuration if information only mode is required or not

"$0.real" "$@"
ret=$?

while test $# -gt 0; do
	if test "$1" != "-d"; then
		shift
		continue
	fi
	shift
	if test -n "$1"; then
		dev="$1"
		break
	fi
done

if test -n "$dev" -a -f /etc/wide-dhcpv6/dhcp6c.conf.$dev; then
	if test "$ret" = "2"; then
		sed 's/send/information-only;\n\tsend/' -i /etc/wide-dhcpv6/dhcp6c.conf.$dev
	else
		sed '/information-only;/d' -i /etc/wide-dhcpv6/dhcp6c.conf.$dev
	fi
fi

if test "$ret" = "3"; then
	ret=1
elif test "$ret" = "255"; then
	ret=0
fi

exit $ret
