#!/bin/bash

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

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

    [ $pid ] && kill -9 $pid

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

    dhclient -pf /run/dhclient.$2.pid -lf /var/lib/dhcp/dhclient.$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
    pid=`ps ax | grep dhclient |grep -v "\-6" |grep $2 |grep -v grep|awk '{print $1}'`

    [ $pid ] && kill -9 $pid

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

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

exit 0
