#!/usr/bin/bash
# $Id$
#
# rb-exporter         Start/Stop the rb-exporter daemon.
#
# chkconfig: 2345 56 60
#
# description: processname: /usr/local/sbin/rb-exporter \
#              config: /etc/rb-exporter/<interface>/rb-exporter.conf \
#              pidfile: /etc/rb-exporter/<interface>/pid 
#
# Created by David Vanhoucke (dvanhoucke@redborder.com)
#            Miguel Negron (manegron@redborder.com)

### BEGIN INIT INFO
# Provides: pmacctd
# Required-Start: 
# Required-Stop: 
# Should-Start: 
# Should-Stop: 
# Default-Start: 
# Default-Stop: 
# Short-Description: start and stop the pmacctd for different network interfaces
# Description: 
### END INIT INFO

# source function library
SYSTEMCTL_SKIP_REDIRECT=true

. /etc/init.d/functions


RETVAL=0
prog="pmacctd"
binary=/usr/sbin/pmacctd

set_pid_file() {
  pidfile=/var/run/rb-exporter-$1.pid 
}

set_lock_file() {
  lockfile=/var/lock/rb-exporter-$1
}

start() {

       if [ -d /etc/rb-exporter/$iface -a -f /etc/rb-exporter/$iface/rb-exporter.conf ]; then
          OPTIONS="-i ${iface} -F ${pidfile} -f /etc/rb-exporter/$iface/rb-exporter.conf"
          [ -x $binary ] || exit 5
          echo -n $"Starting $prog: on $iface "
          daemon --pidfile=$pidfile $binary $OPTIONS
          RETVAL=$?
          [ $RETVAL -eq 0 ] && touch $lockfile
          echo 
          return $RETVAL
        else
          echo "no config file for $prog on $iface"
          echo
          return 0
        fi

}

stop() {
        PID=0
        [ -f $lockfile ] && [ -f $pidfile ] && PID=$(<$pidfile)
        if [ "x$PID" != "x0" -a "x$PID" != "x" ]; then
          echo -n $"Stopping $prog: on $iface"
          killproc -p $pidfile $binary
          RETVAL=$?
          [ $RETVAL -eq 0 ] && rm -f $lockfile && rm -f $pidfile
          echo
          return $RETVAL
        else
          echo -n $"$prog on $iface not running"
          echo
          return 0
        fi

}

restart() {
	stop
	start
}

status() {

        #[ -f $pidfile ] && [ -f $lockfile ] && PID=$(<$pidfile)
        __pids_var_run "$prog" "$pidfile"
        RC=$?
        # echo " and RC is $RC -> $pidfile"
        case "$RC" in
                0)
                        echo $"${prog} on ${iface} (${pid}) is running..."
                        return 0
                        ;;
                1)
                        echo $"${prog} on ${iface} dead but pid file exists"
                        return 1
                        ;;
                3)       
                        if [ -f $lockfile ]; then
                          echo $"${prog} on ${iface} is dead but locked"
                          return 2
                        fi
                        if [ -d /etc/rb-exporter/$iface ]; then
                          echo $"${prog} on ${iface} is not running and it should!"
                          return 2
                        fi
                        echo $"${prog} on ${iface} is not running."
                        return 0
                        ;;
                     
                4)
                        echo $"${prog} on ${iface} status unknown due to insufficient privileges."
                        return 4
                        ;;
                *)      
                        echo $"${prog} on ${iface} is not running."
                        return 0
                        ;;
        esac
}

usage(){
   echo $"Usage: $0 {start|stop|status|restart} [interface|all]"
   RETVAL=2
   exit $RETVAL
}

if [ $# -lt 1 ]; then 
  usage
fi


if [ $# -lt 2 ] || [ $2 == 'all' ]; then 
  interfaces_list=$(ls /sys/class/net/)
  readarray -t interfaces <<<"$interfaces_list"
else
  interfaces=($2)
fi

RETVAL=0

for iface in "${interfaces[@]}"
do
    if [ $iface == 'lo' ]; then
      continue
    fi 

    set_pid_file $iface
    set_lock_file $iface

    case "$1" in
      start)
        start
        RETVAL=$?
        ;;
      stop)
        stop
        RETVAL=$?
        ;;
      restart)
        restart
        RETVAL=$?
        ;;
      status)
        status -p $pidfile $prog
         THISVAL=$?
         RETVAL=$([ $RETVAL -gt $THISVAL ] && echo $RETVAL || echo $THISVAL)
        ;;
      *)
        usage
        ;;
  esac

done
# echo $"RETVAL IS $RETVAL"
exit $RETVAL