#!/bin/sh
exec_option=$1
lldpdsock="/usr/local/data/tmp/lldpd.socket"

mkdir -p /usr/local/data/tmp/lldpd >/dev/null 2>&1

if [ -f /usr/local/data/configuration/Network/LLDPC.cfg ]
then
        . /usr/local/data/configuration/Network/LLDPC.cfg
        if [ X"$exec_option" = X"start" ]
        then
        	pid=`pidof lldpd`
			    if [ X"$pid" != X"" ]
			    then
			    	killall lldpd
			    fi
			    enable_lldp=$(echo $EnableLLDP | tr [A-Z] [a-z])
			    enable_cdp=$(echo $EnableCDP | tr [A-Z] [a-z])
			    if [ X"$enable_lldp" == X"yes" ] && [ X"$enable_cdp" == X"yes" ]
			    then
			    	lldpd -M 3 -H 0 -c -l -t $PacketInterval -T $CDPPacketInterval
			    	if [ "$?" != "0" ]
						then
							exit 1
						else
							count=0 
							while [ $count -le 1000 ]
							do
								if [ -e $lldpdsock ]
								then
									break
								fi
					    	usleep 100
								count=$(($count + 1))
							done
							if [ -e $lldpdsock ]
							then
								lldpctl -O PD:1:1:50
								lldpctl -P 1:1:0:0:0:0
							fi
					  fi
				  elif [ X"$enable_lldp" == X"yes" ]
				  then
					  lldpd -M 3 -H 0 -l -t $PacketInterval
					  if [ "$?" != "0" ]
				    then
					    exit 1
				    else
					    count=0
					    while [ $count -le 1000 ]
					    do
						    if [ -e $lldpdsock ]
						    then
							    break
						    fi
						    usleep 100
						    count=$(($count + 1))
					    done
					    if [ -e $lldpdsock ]
					    then
						    lldpctl -O PD:1:1:50
						    lldpctl -P 1:1:0:0:0:0
					    fi
				    fi
				  elif [ X"$enable_cdp" == X"yes" ]
				  then
					  lldpd -M 3 -H 0 -c -T $CDPPacketInterval
				    if [ "$?" != "0" ]
				    then
					    exit 1
				    else
					    count=0
					    while [ $count -le 1000 ]
					    do
						    if [ -e $lldpdsock ]
						    then
							    break
						    fi
						    usleep 100
						    count=$(($count + 1))
					    done
					    if [ -e $lldpdsock ]
					    then
						    lldpctl -O PD:1:1:50
						    lldpctl -P 1:1:0:0:0:0
					    fi
				    fi			 
		      fi
		    elif [[ X"$exec_option" = X"stop" ]]
				then
			    pid=`pidof lldpd`
					if [ X"$pid" != X"" ]
			    then
			            killall lldpd
			    fi
				else
				  echo 'usage lldpdc start|stop'
				fi
else
  echo 'Hi, guy, i need config file'
fi
