#! /bin/bash

log=/var/run/dhclient.log
resolvconf=/var/run/resolv.conf
ypconf=/var/run/yp.conf
ntp_conf=/var/run/ntp.conf
ntp_drift_file=/var/lib/ntp/drift
ntp_step_tickers=/var/lib/ntp/step-tickers

for i in nis_conf resolv_conf ntp_conf hostname set_ip; do
    eval "function ${i}_pre_hook() { :; }"
    eval "function ${i}_post_hook() { :; }"
done

test -e /etc/sysconfig/dhclient-hooks && . /etc/sysconfig/dhclient-hooks

function generateNIS()
{
    local domain=$1
    local servers=$2
    local i

    nis_pre_hook || return
    test "$domain$server" || return

    if test "$domain"; then
        if test "$servers"; then
	    for i in $servers; do
		echo "$domain $i"
	    done
        else
	    echo "domain $domain broadcast"
        fi
    else
	for i in $servers; do
	    echo "ypserver $i"
	done
    fi >$ypconf
    nis_post_hook
}

function generateResolvConf()
{
    local domain=$1
    local servers=$2
    local i

    resolv_conf_pre_hook   || return
    test "$domain$servers" || return
    
    set -- $domain
    {
	test "$1" && {
	    echo "domain $1"
	    test "$domain" = "$1" || echo "search $domain"
	}

	for i in $servers; do
	    echo "nameserver $i"
	done
    } >$resolvconf
    resolv_conf_post_hook
}

function generateNTP()
{
    local servers=$1
    local i

    ntp_conf_pre_hook || return
    test "$servers" || return

    {
	cat <<EOF
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

restrict default ignore
restrict 127.0.0.1
driftfile $ntp_drift_file
broadcastdelay 0.008
authenticate yes
keys /etc/ntp/keys
EOF
	for i in $servers; do
	     echo "restrict $i nomodify notrap noquery"
	     echo "server $i"
	     echo "$i" >> $ntp_step_tickers
	done
    } >$ntp_conf
    ntp_conf_post_hook
}

function setHostName()
{
    local hostname=$1

    hostname_pre_hook || return
    hostname "$hostname"
    hostname_post_hook
}

function setIPAddress()
{
    local interface=$1
    local ip=$2
    local subnet=$3
    local mask=$4
    local bcast=$5
    local routers=$6
    local r

    set_ip_pre_hook || return
    ip link  set "$interface" up
    ip addr  add "$ip/$mask" broadcast "$bcast" dev "$interface"
    ip route add "$subnet/$mask" dev "$interface"
    for r in $routers; do
	ip route add default via "$r" dev "$interface"
    done
    set_ip_post_hook
}

function setParams()
{
    test "$old_nis_domain $old_nis_servers" = "$new_nis_domain $new_nis_servers" || \
	generateNISConf    "$new_nis_domain" "$new_nis_servers"
    test "$old_domain $old_domain_name_servers" = "$new_domain $new_domain_name_servers" || \
	generateResolvConf "$new_domain"     "$new_domain_name_servers"
    test "$old_ntp_servers" = "$new_nis_servers" || \
	generateNTPConf    "$new_ntp_servers"
    test "$old_host_name" = "$new_host_name" || \
	setHostName        "$new_host_name"

    setIPAddress       "$interface" "$new_ip_address" "$new_nework_number" "$new_subnet_arg" "new_broadcast_arg" "new_routers"
}

function callMedium()
{
    echo "medium='$medium', interface='$interface'" >>$log
}

function callPreInit()
{
    echo "medium='$medium', interface='$interface', alias_ip_address='$alias_ip_address'" >>$log
}

function callBound()
{
    echo "medium='$medium', new_ip_address='$new_ip_address', new_domain='$new_domain', interface='$interface', alias_ip_address='$alias_ip_address'" >>$log

    old_nis_domain=
    old_nis_servers=
    old_domain=
    old_domain_name_servers=
    old_ntp_servers=
    old_host_name=

    setParams
}

function callRenew()
{
    setParams
}

function callRebind()
{
    setParams
}

function callReboot()
{
    setParams
}

function callExpire()
{
    setParams
}

function callFail()
{
    setParams
}


function callTimeout()
{
    setParams
}

echo "$reason" >>$log

case "$reason" in
    MEDIUM)	callMedium;;
    PREINIT)	callPreInit;;
    BOUND)	callBound;;
    RENEW)	callRenew;;
    REBIND)	callRebind;;
    REBOOT)	callReboot;;
    EXPIRE)	callExpire;;
    FAIL)	callFail;;
    TIMEOUT)	callTimeout;;
esac
