#!/bin/sh
ip=/sbin/ip
RESOLV_IPV4="/var/etc/resolv.conf"
RESOLV_IPV6="/var/etc/resolv_ipv6.conf"
RESOLV_CONF="/etc/resolv.conf"


make_resolv_conf() {
  if [ "x${new_dhcp6_name_servers}" != x ] ; then
    cat /dev/null > $RESOLV_IPV6
    chmod 644 $RESOLV_IPV6

    if [ "x${new_dhcp6_domain_search}" != x ] ; then
      echo search ${new_dhcp6_domain_search} >> $RESOLV_IPV6
    fi
    for nameserver in ${new_dhcp6_name_servers} ; do
      echo nameserver ${nameserver} >> $RESOLV_IPV6
    done
    cat $RESOLV_IPV4 $RESOLV_IPV6 > $RESOLV_CONF
  fi
}

exit_with_hooks() {
  exit_status=$1
  if [ -f /etc/dhclient-exit-hooks ]; then
    . /etc/dhclient-exit-hooks
  fi
  exit $exit_status
}

if [ -f /etc/dhclient-enter-hooks ]; then
  exit_status=0
  . /etc/dhclient-enter-hooks
  if [ $exit_status -ne 0 ]; then
    exit $exit_status
  fi
fi

release=`uname -r`
release=`expr $release : '\(.*\)\..*'`
relminor=`echo $release |sed -e 's/[0-9]*\.\([0-9][0-9]*\)\(\..*\)*$/\1/'`
relmajor=`echo $release |sed -e 's/\([0-9][0-9]*\)\..*$/\1/'`

### DHCPv6 Handlers

if [ ${reason} = PREINIT6 ] ; then
  ${ip} link set ${interface} up
  ${ip} -f inet6 addr flush dev ${interface} scope global permanent

  exit_with_hooks 0
fi

if [ ${reason} = BOUND6 ] ; then
  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ] ; then
    exit_with_hooks 2;
  fi

  ${ip} -f inet6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
	dev ${interface} scope global
  make_resolv_conf

  exit_with_hooks 0
fi

if [ ${reason} = RENEW6 ] || [ ${reason} = REBIND6 ] ; then
  if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
     [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ] ; then
    make_resolv_conf
  fi

  exit_with_hooks 0
fi

if [ ${reason} = DEPREF6 ] ; then
  if [ x${new_ip6_prefixlen} = x ] ; then
    exit_with_hooks 2;
  fi

  exit_with_hooks 0
fi

if [ ${reason} = EXPIRE6 -o ${reason} = RELEASE6 -o ${reason} = STOP6 ] ; then
  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ] ; then
    exit_with_hooks 2;
  fi

  ${ip} -f inet6 addr del ${old_ip6_address}/${old_ip6_prefixlen} \
	dev ${interface}

  exit_with_hooks 0
fi

exit_with_hooks 0
