#!/bin/sh
#
# Controls lldpd.
#

LLDPD_CONFIG=/etc/lldpd.conf
LLDPD=/usr/sbin/lldpd
LLDPD_CLI=/usr/sbin/lldpcli
# -x -- Enable SNMP subagent.
LLDPD_OPT=-x
MONIT=/usr/bin/monit

dotconfig=/wr/etc/dot-config


start_counter() {
    # increase start counter
    COUNTER_FILE="/tmp/start_cnt_lldpd"
    START_COUNTER=1
    if [ -f "$COUNTER_FILE" ];
    then
	read -r START_COUNTER < $COUNTER_FILE
	START_COUNTER=$((START_COUNTER+1))
    fi
    echo "$START_COUNTER" > $COUNTER_FILE
}

start() {


    echo -n "Starting lldpd: "

    if [ -f $dotconfig ]; then
	. $dotconfig
    else
	echo "$0 unable to source dot-config ($dotconfig)!"
    fi

    if [ "$CONFIG_LLDPD_DISABLE" = "y" ]; then
	echo "lldpd disabled in dot-config!"
	if [ "$1" != "force" ]; then
	    # Unmonitor lldpd, ignore all printouts from monit.
	    # Run in background since monit may wait for a timeout.
	    $MONIT unmonitor lldpd &>/dev/null &
	    exit 0
	fi
	echo -n "Force start of lldpd: "
    fi

    # Creating lldpd config
    echo "# This configuration file was generated by $0 at startup" > $LLDPD_CONFIG
    echo "# If you would like to change a lldpd configuration in runtime please consider using lldpcli" >> $LLDPD_CONFIG
    echo "configure system hostname '$(hostname)'" >> $LLDPD_CONFIG

    tx_interval=5
    if [ ! -z "$CONFIG_LLDPD_TX_INTERVAL" ]; then
	tx_int=$CONFIG_LLDPD_TX_INTERVAL
	# check if given value is an int
	case $tx_int in
	    ''|*[!0-9]*) echo -n "wrong CONFIG_LLDPD_TX_INTERVAL "$tx_int". Using 5 as the default interval. " ;;
	    *) echo tx_interval=$tx_int ;;
	esac
    fi
    echo "configure lldp tx-interval $tx_interval" >> $LLDPD_CONFIG

    if [ "$CONFIG_LLDPD_MINIMUM_FRAME_SIZE" = "y" ]; then 
	echo "configure system description  'WR-SWITCH'" >> $LLDPD_CONFIG
	# disable capabilities-advertisements
	echo "unconfigure ports all lldp capabilities-advertisements" >> $LLDPD_CONFIG
	# disable management ip advertisements
	echo "unconfigure ports all lldp management-addresses-advertisements" >> $LLDPD_CONFIG
    else
	echo "configure system description  'WR-SWITCH: $(/wr/bin/wrsw_version)'" >> $LLDPD_CONFIG
    fi
    if [ "$CONFIG_LLDPD_MANAGEMENT_PORT_DISABLE" = "y" ]; then
	echo "configure system interface pattern '!eth*'" >> $LLDPD_CONFIG
    fi
    echo "resume" >> $LLDPD_CONFIG

    start-stop-daemon -S -q -p /var/run/lldpd.pid --exec $LLDPD -- $LLDPD_OPT
    ret=$?
    if [ $ret -eq 0 ]; then
	start_counter
	echo "OK"
    elif [ $ret -eq 1 ]; then
	echo "Failed (already running?)"
    else
	echo "Failed"
    fi

    # check whether the process was monitored
    $MONIT summary 2>&1 | grep lldpd | grep "Not monitored" &> /dev/null
    if [ $? -eq 0 ]; then
	echo "lldpd was not monitored, enabling monitoring"
	# the process was not monitored, enable monitoring
	# this will generate extra log entries from monit
	$MONIT monitor lldpd
    fi
}

stop() {
    echo -n "Stopping lldpd: "
    start-stop-daemon -K -q -p /var/run/lldpd.pid
    if [ $? -eq 0 ]; then
	echo "OK"
    else
	echo "Failed"
    fi
}

restart() {
    stop
    start
}

case "$1" in
  start)
	start "$2"
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start <force>|stop|restart}"
	echo "    start force -- enable lldpd even it is disabled in the dot-config"
	exit 1
	;;
esac
