#!/bin/bash
# ub-lldpd init file
#
# chkconfig: 2345 60 20
# description: 802.1ab (LLDP) daemon
#
# processname: ub-lldpd
# pidfile: /var/run/ub-lldpd.pid

### BEGIN INIT INFO
# Provides: ub-lldpd
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $syslog $network $net-snmp
# Should-Stop: $syslog $network $net-snmp
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: LLDP daemon
# Description: UB (LLDP) daemon
### END INIT INFO

# source function library
. /etc/rc.d/init.d/functions

LLDPD_OPTIONS=""
[ -e /etc/sysconfig/ub-lldpd ] && . /etc/sysconfig/ub-lldpd

RETVAL=0
prog="ub-lldpd"
binary=/usr/sbin/ub-lldpd
pidfile=/var/run/ub-lldpd.pid
lockfile=/var/lock/subsys/$prog

# Determine if we can use the -p option to daemon, killproc, and status.
# RHEL < 5 can't.
if status | grep -q -- '-p' 2>/dev/null; then
    daemonopts="--pidfile $pidfile"
    pidopts="-p $pidfile"
fi

start() {
        [ -x $binary ] || exit 5
        echo -n $"Starting $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                daemon $daemonopts $binary $LLDPD_OPTIONS
                RETVAL=$?
                [ $RETVAL -eq 0 ] && touch $lockfile
        fi;
        echo 
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                killproc $pidopts $binary
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f $lockfile
        fi;
        echo
        return $RETVAL
}

restart(){
	stop
	start
}

condrestart(){
    [ -e $lockfile ] && restart
    return 0
}

rh_status_q(){
    status $pidopts $prog >/dev/null 2>&1
}

case "$1" in
  start)
	rh_status_q && exit 0
	start
	RETVAL=$?
	;;
  stop)
	rh_status_q || exit 0
	stop
	RETVAL=$?
	;;
  restart)
	restart
	RETVAL=$?
        ;;
  reload)
        rh_status_q || exit 7
	exit 3
        ;;
  force-reload)
	restart
	RETVAL=$?
        ;;
  condrestart|try-restart)
        rh_status_q || exit 0
	condrestart
	RETVAL=$?
	;;
  status)
        status $pidopts $prog
	RETVAL=$?
        ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|force-reload}"
	RETVAL=2
esac

exit $RETVAL
