#!/bin/sh
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)i.dhcpagent	1.1	07/01/29 SMI"
#

while read src dest
do
	if [ ! -f $dest ] ; then
		cp $src $dest
	else
		tmpdst=/var/run/dhcpagent.dst.$$

		# Changes are applied separately to accomodate user updates to
		# the file.

		# If the target has the old v4 comments, then update them
		# to describe v6.
		grep '# All parameters can be tuned for ' $dest >/dev/null &&
		( grep '# An interface name alone ' $dest >/dev/null || (
			nawk '
			/# All parameters can be tuned for / { flag = 1; }
			/^$/ && flag == 1 {
print "#";
print "# An interface name alone specifies IPv4 DHCP.  For DHCPv6, append \""\
    ".v6\".";
print "# Some examples:";
print "#";
print "# hme0.RELEASE_ON_SIGTERM=no	specify hme0 v4 behavior";
print "# hme0.v6.RELEASE_ON_SIGTERM=no	specify hme0 v6 behavior";
print "# RELEASE_ON_SIGTERM=no		match all v4 interfaces";
print "# .v6.RELEASE_ON_SIGTERM=no	match all v6 interfaces";
				flag = 2;
			}
			{ print $0; }
			' $dest > $tmpdst && cp $tmpdst $dest
		) )

		# If the target has the old SIGTERM documentation, update.
		if grep ' is sent a SIGTERM, all managed' $dest >/dev/null &&
		    grep 'parameter-value pair, all managed' $dest >/dev/null
		then
			nawk '
			/ is sent a SIGTERM, all managed/ { flag = 1; }
			/parameter-value pair, all managed/ && flag == 1 {
print "# By default, when the DHCP agent is sent a SIGTERM (typically when";
print "# the system is shut down), all managed addresses are dropped rather";
print "# than released.  Dropping an address does not notify the DHCP server";
print "# that the address is no longer in use, leaving it possibly available";
print "# for subsequent use by the same client.  If DHCP is later restarted";
print "# on the interface, the client will ask the server if it can continue";
print "# to use the address.  If the server either grants the request, or";
print "# does not answer (and the lease has not yet expired), then the client";
print "# will use the original address.";
print "#";
print "# By uncommenting the following parameter-value pairs, all managed";
print "# interfaces are released on SIGTERM instead.  In that case, the DHCP";
print "# server is notified that the address is available for use.  Further,";
print "# if DHCP is later restarted on the interface, the client will not";
print "# request its previous address from the server, nor will it attempt to";
print "# reuse the previous lease.  This behavior is often preferred for";
print "# roaming systems.";
				flag = 2;
				next;
			}
			flag == 1 { next; }
			{ print $0; }
			' $dest > $tmpdst && cp $tmpdst $dest
		fi

		# If the target lacks a v6 PARAM_REQUEST_LIST entry, then
		# add it.
		fgrep '.v6.PARAM_REQUEST_LIST' $dest >/dev/null ||
			cat >> $dest <<EOF

# The default DHCPv6 parameter request list has preference (7), unicast (12),
# DNS addresses (23), DNS search list (24), NIS addresses (27), and
# NIS domain (29).  This may be changed by altering the following parameter-
# value pair.  The numbers correspond to the values defined in the IANA
# dhcpv6-parameters registry at the time of this writing.
.v6.PARAM_REQUEST_LIST=7,12,23,24,27,29
EOF
	fi
done
exit 0
