#!/bin/bash

#used for sif 
#$1: "up" or "down"
#$2:  the interface
#no output var

if [ $1 = "up" ]; then
    if [ $2 = "eth0" ]; then
        pid=`ps ax|grep dhclient|grep "\-6"|grep eth0|grep -v grep|awk '{print $1}'`
    elif [ $2 = "eth1" ]; then
        pid=`ps axf|grep dhclient|grep "\-6"|grep eth1|grep -v grep|awk '{print $1}'`
    fi

    [ $pid ] && kill -9 $pid

    rm -fr /run/dhclient6.$2.pid

    dhclient -6 -pf /run/dhclient6.$2.pid -lf /var/lib/dhcp/dhclient6.$2.leases $2 > /dev/null 2>&1 &

    #if [ "$?" = 0 ]; then 
    #    IP=`ifconfig $2 |awk -F: '{print $2}' | awk '{print $1}' | \grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
    #    [ $IP ] && echo "Get IP $IP for $2 via DHCP"
    #else
    #    echo "Failed to configure DHCP on $2, please check your network setup."
    #fi

elif [ $1 = "down" ]; then
    #ifconfig $2 down
    if [ $2 = "eth0" ]; then
        pid=`ps axf|grep dhclient|grep "\-6"|grep eth0|grep -v grep|awk '{print $1}'`
    elif [ $2 = "eth1" ]; then
        pid=`ps axf|grep dhclient|grep "\-6"|grep eth1|grep -v grep|awk '{print $1}'`
    fi

    [ $pid ] && kill -9 $pid

    rm -fr /run/dhclient6.$2.pid

    # TODO: stop the udhcpc process on specified interface according to pid
fi

exit 0
