#!/bin/sh
### BEGIN INIT INFO
# Provides:          iperf
# Required-Start:    $local_fs $network $remote_fs $syslog
# Required-Stop:     $local_fs $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: <Enter a short description of the software>
# Description:       <Enter a long description of the software>
#                    <...>
#                    <...>
### END INIT INFO

# Author: PICCORO Lenz McKAY <mckaygerhard@gmail.com>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="network bandwidth measurement tool"
NAME=iperf
DAEMON=/usr/bin/iperf
DAEMON_ARGS=""
DAEMON_START_INI=false
PIDFILE=/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions


get_pid_from_file ()
{
  PID=""

  if [ -f "/run/$NAME.pid" ]
  then
    PID="$(pidof iperf)"
  fi
}

check_process_list ()
{
  process_list=""

  get_pid_from_file

  if [ "x${PID}" != "x" ]
  then
    process_list=`ps | grep iperf`
  fi
}


case "$1" in
  start)

if [ "$DAEMON_START_INI" != 1 ]; then
	log_success_msg "$DESC not enabled, edit at /etc/default/$NAME"
	exit 0
fi


	log_daemon_msg "Starting $DESC" "$NAME"

                 pid="$(pidof iperf)"
                 if [ ! -z "$pid" ] ; then
                     log_daemon_msg "an iperf are running or not managed" "$NAME"
                     exit 1
                 fi
		start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE  --name $NAME --startas /bin/bash -- -c " $DAEMON -s -D $DAEMON_ARGS >>  /var/log/$NAME.log 2>&1"
                 status=$?
                 pid="$(pidof iperf)"
                 if [ -z "$pid" ] ; then
                     log_daemon_msg "unknow error check /var/log" "$NAME"
                    log_end_msg 1
                 else
                    echo "$pid" > "$PIDFILE"
                    log_end_msg $status
                 fi
	;;
  stop)
	log_daemon_msg "Stopping ALL the $DESC instances" "$NAME"
                 pid="$(pidof iperf)"
                 if [ -z "$pid" ] ; then
                     log_daemon_msg "PID file empty" "$NAME"
                     log_end_msg 1
                     exit 1
                 fi
                 kill -2 "$pid"
                 log_end_msg $?
                 rm -f $PIDFILE
                 exit 0
	;;
  status)
	  get_pid_from_file
	  if [ "x${PID}" != "x" ]
	  then
	    check_process_list
	    if [ "x${process_list}" != "x" ]
	    then
                     log_daemon_msg "$DESC running ${PID}" "$NAME"
                     log_end_msg 0
	    else

                     log_daemon_msg "$DESC not running by ${PID}" "$NAME"
                     log_end_msg 1
	    fi
	  elif [ "x${PID}" = "x" ]
	  then
                     log_daemon_msg "$DESC not running" "$NAME"
                     log_end_msg 3
	  fi
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|status}" >&2
	exit 3
	;;
esac

exit 0
