#!/bin/sh
# dhclient-script for Linux. Dan Halbert, March, 1997.
# Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
# No guarantees about this. I'm a novice at the details of Linux
# networking.

# Notes:

# 0. This script is based on the netbsd script supplied with dhcp-970306.

# 1. ifconfig down apparently deletes all relevant routes and flushes
# the arp cache, so this doesn't need to be done explicitly.

# 2. The alias address handling here has not been tested AT ALL.
# I'm just going by the doc of modern Linux ip aliasing, which uses
# notations like eth0:0, eth0:1, for each alias.

# 3. I have to calculate the network address, and calculate the broadcast
# address if it is not supplied. This might be much more easily done
# by the dhclient C code, and passed on.

# 4. TIMEOUT not tested. ping has a flag I don't know, and I'm suspicious
# of the $1 in its args.

# 'ip' just looks too weird.  /sbin/ip looks less weird.
ip=/bin/ip

echo $reason
echo $new_ip6_prefix
echo $new_ip6_address
echo $new_dhcp6_name_servers
echo $new_domain_name_servers


if [ x$reason = xPREINIT6 ]; then
  # Ensure interface is up.
  ${ip} link set ${interface} up

  # Remove any stale addresses from aborted clients.
  ${ip} -f inet6 addr flush dev ${interface} scope global permanent

  exit 0
fi



if [ x$reason = xBOUND6 ]; then

  # Check for nameserver options.
  
  if [ x"$new_domain_name_servers" != x ]; then
    cat /dev/null > /var/fyi/sys/dns6
    if [ x"$new_domain_search" != x ]; then
      echo search $new_domain_search >> /var/fyi/sys/dns6
    elif [ x"$new_domain_name" != x ]; then
      # Note that the DHCP 'Domain Name Option' is really just a domain
      # name, and that this practice of using the domain name option as
      # a search path is both nonstandard and deprecated.
      echo search $new_domain_name >> /var/fyi/sys/dns6
    fi
    for nameserver in $new_domain_name_servers; do
      echo nameserver $nameserver >> /var/fyi/sys/dns6
    done
  
    #sed -f /etc/dup.sh /var/fyi/sys/dns > /var/fyi/sys/dns6
    #system not support mv command
    #cp /var/fyi/sys/dns6 /var/fyi/sys/dns

   
  elif [ "x$new_dhcp6_name_servers" != x ]; then
    cat /dev/null > /var/fyi/sys/dns6
    if [ "x$new_dhcp6_domain_search" != x ]; then
      echo search $new_dhcp6_domain_search >> /var/fyi/sys/dns6
    fi
    for nameserver in $new_dhcp6_name_servers; do
      echo nameserver $nameserver >> /var/fyi/sys/dns6
    done
  
    #sed -f /etc/dup.sh /var/fyi/sys/dns > /var/fyi/sys/dns6
    #cat /var/fyi/sys/dns6
    #system not support mv command
    #cp /var/fyi/sys/dns6 /var/fyi/sys/dns

  fi
  
  #if [ x${old_ip6_prefix} != x ] || [ x${new_ip6_prefix} != x ]; then
  #  echo Prefix ${reason} old=${old_ip6_prefix} new=${new_ip6_prefix}

  #  exit 0
  #fi
  if [ x${new_ip6_address} = x ] || [ x${new_ip6_prefixlen} = x ]; then
    exit 0
  fi

  ${ip} -6 addr add ${new_ip6_address}/${new_ip6_prefixlen} \
	dev ${interface} scope global


  exit 0
fi

if [ x$reason = xRENEW6 ] || [ x$reason = xREBIND6 ]; then
  # Make sure nothing has moved around on us.

  # Nameservers/domains/etc.
    if [ "x${new_dhcp6_name_servers}" != "x${old_dhcp6_name_servers}" ] ||
       [ "x${new_dhcp6_domain_search}" != "x${old_dhcp6_domain_search}" ]; then
      if [ x"$new_domain_name_servers" != x ]; then
      cat /dev/null > /var/fyi/sys/dns6
      if [ x"$new_domain_search" != x ]; then
        echo search $new_domain_search >> /var/fyi/sys/dns6
      elif [ x"$new_domain_name" != x ]; then
        # Note that the DHCP 'Domain Name Option' is really just a domain
        # name, and that this practice of using the domain name option as
        # a search path is both nonstandard and deprecated.
        echo search $new_domain_name >> /var/fyi/sys/dns6
      fi
      for nameserver in $new_domain_name_servers; do
        echo nameserver $nameserver >> /var/fyi/sys/dns6
      done
    
      #sed -f /etc/dup.sh /var/fyi/sys/dns > /var/fyi/sys/dns.bak
      #system not support mv command
      #cp /var/fyi/sys/dns.bak /var/fyi/sys/dns
     
    elif [ "x${new_dhcp6_name_servers}" != x ]; then
      cat /dev/null > /var/fyi/sys/dns6
      if [ "x${new_dhcp6_domain_search}" != x ]; then
        echo search ${new_dhcp6_domain_search} >> /var/fyi/sys/dns6
      fi
      for nameserver in ${new_dhcp6_name_servers}; do
        echo nameserver ${nameserver} >> /var/fyi/sys/dns6
      done
    
      #sed -f /etc/dup.sh /var/fyi/sys/dns > /var/fyi/sys/dns.bak
      #system not support mv command
      #cp /var/fyi/sys/dns.bak /var/fyi/sys/dns
      #rm /var/fyi/sys/dns.bak -rf
    fi
  fi

  exit 0
fi

if [ x$reason = xDEPREF6 ]; then
  if [ x${new_ip6_prefixlen} = x ]; then
    exit 0
  fi

  ${ip} -6  addr change ${new_ip6_address}/${new_ip6_prefixlen} \
       dev ${interface} scope global preferred_lft 0

  exit  0
fi


if [ x$reason = xEXPIRE6 -o x$reason = xRELEASE6 -o x$reason = xDECLINE6 -o x$reason = xSTOP6 ]; then
  if [ x${old_ip6_address} = x ] || [ x${old_ip6_prefixlen} = x ]; then
    exit 0
  fi

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

  exit 0
fi

exit  0

