#!/bin/sh

echo "enter & execute dhclient-script"

DHCP6_FILE=/etc/dhcp6/dhcp6c_conf
RA_CONF_FILE=/etc/dhcp6/dhcp6c_radvd.conf
RESOLV_FILE=/etc/resolv.conf
PF_FILE=/etc/dhcp6/dhcp6c_pf

#get lan interface name
#LAN_INF=$(uci -q -P /var/state get network.lan.ifname)
LAN_INF=br-lan

make_resolv_conf() {
 if [ "x${new_dhcp6_name_servers}" != x ] ; then
    cat /dev/null > /etc/resolv.conf.dhclient6
    chmod 644 /etc/resolv.conf.dhclient6

    if [ "x${new_dhcp6_domain_search}" != x ] ; then
	  domain_search=$(cat $RESOLV_FILE | grep $new_dhcp6_domain_search)
      if [ "$domain_search" = "" ]; then	  
		echo search ${new_dhcp6_domain_search} >> /etc/resolv.conf.dhclient6
		WRITE=1
	  fi
    fi
	
    for nameserver in ${new_dhcp6_name_servers} ; do
	  name_servers=$(cat $RESOLV_FILE | grep $nameserver)
	  if [ "$domain_search" = "" ]; then
		echo nameserver ${nameserver} >> /etc/resolv.conf.dhclient6
		WRITE=1
	  fi
    done

    #mv /etc/resolv.conf.dhclient6 $RESOLV_FILE
	if [ "$WRITE" = "1" ]; then
	    echo "write data to $RESOLV_FILE"
		cat /etc/resolv.conf.dhclient6 >> $RESOLV_FILE
	fi
  fi
}

exit_with_hook_radvd() { 
  echo $1 > /tmp/dhcp6c_radvd
}

exit_with_hook_dhcp6s() { 
  echo $1 > /tmp/dhcp6c_6s
}

###
### DHCPv6 Handlers
###

config_radvd() {
  new_max_life=$(cat $DHCP6_FILE | grep -m 1 [n]ew_max_life | awk '{print $2}')
  new_preferred_life=$(cat $DHCP6_FILE | grep -m 1 [n]ew_preferred_life | awk '{print $2}')
  
  #echo $new_max_life
  #echo $new_preferred_life
  
  echo "interface $LAN_INF"  > /etc/dhcp6/dhcp6c_radvd.conf
  echo "{"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "   AdvSendAdvert on;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  #echo "   MaxRtrAdvInterval 600;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  #echo "   MinRtrAdvInterval 198;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  #echo "   MaxRtrAdvInterval 30;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  #echo "   MinRtrAdvInterval 20;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "   AdvDefaultLifetime 1800;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "" >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "   prefix $new_ip6_prefix_radvd"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "   {"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "     AdvOnLink on;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "     AdvAutonomous on;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  
  if [ x${new_preferred_life} != x ]; then
    echo "     AdvPreferredLifetime $new_preferred_life;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  fi
  
  if [ x${new_max_life} != x ]; then
     echo "     AdvValidLifetime $new_max_life;"  >> /etc/dhcp6/dhcp6c_radvd.conf
  fi
  
  echo "" >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "   };"  >> /etc/dhcp6/dhcp6c_radvd.conf
  
  echo "" >> /etc/dhcp6/dhcp6c_radvd.conf
  if [ "$new_dhcp6_name_servers" != "" ]; then
	echo "   RDNSS $new_dhcp6_name_servers"  >> /etc/dhcp6/dhcp6c_radvd.conf
    echo "   {"  >> /etc/dhcp6/dhcp6c_radvd.conf
	echo "   };"  >> /etc/dhcp6/dhcp6c_radvd.conf
  fi
  
  echo "" >> /etc/dhcp6/dhcp6c_radvd.conf
  echo "};"  >> /etc/dhcp6/dhcp6c_radvd.conf
}

radvd() {
	radvd_pid=$(ps | grep -m 1 [r]advd | awk '{print $1}')
	#echo $radvd_pid
	if [ "$radvd_pid" != "" ]; then
	    echo "enter radvd 1"
		if [ -f $RA_CONF_FILE ]; then
			old_ip6_prefix=$(cat $RA_CONF_FILE | grep -m 1 [p]refix | awk '{print $2}')
			
			#echo $old_ip6_prefix
			#echo $new_ip6_prefix_radvd
			
			if [ x${old_ip6_prefix} != x${new_ip6_prefix_radvd} ] ; then
			    #echo "kill radvd"
				kill $radvd_pid
				config_radvd
				exit_with_hook_radvd 1
				#radvd -C /etc/dhcp6/dhcp6c_radvd.conf -m stderr_syslog -p /var/run/radvd.pid
			else
			    #echo "don't kill radvd"
				exit_with_hook_radvd 0
			fi
		else
			kill $radvd_pid
			config_radvd
			exit_with_hook_radvd 1
			#radvd -C /etc/dhcp6/dhcp6c_radvd.conf -m stderr_syslog -p /var/run/radvd.pid
		fi
	else
		echo "enter radvd 2"
		config_radvd
		exit_with_hook_radvd 1
		#radvd -C /etc/dhcp6/dhcp6c_radvd.conf -m stderr_syslog -p /var/run/radvd.pid
	fi
}

echo "DHCPv6 Handlers: x$reason"

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

if [ -f $PF_FILE ] ; then
	new_ip6_prefix_radvd=$(cat $PF_FILE | grep -m 1 "$LAN_INF-prefix" | awk '{print $2}')
fi

if [ x$reason = xBOUND6 ]; then
  #echo "In xBOUND6"

  if [ x${new_ip6_address} != x ] && [ x${new_ip6_prefixlen} != x ] ; then
    ifconfig ${interface} inet6 ${new_ip6_address}/${new_ip6_prefixlen} alias
  fi

  # Check for nameserver options.
  #make_resolv_conf
  
  if [ x${new_ip6_prefix} != x ] ; then
    # check radvd config
    radvd
  fi
elif [ x$reason = xRELEASE6 ]; then
  proc_radvd=$(ps | grep radvd | grep [/]etc/dhcp6/dhcp6c_radvd.conf)
  if [ "$proc_radvd" != "" ]; then
    killall radvd
  fi
fi

