#!/bin/bash

### BEGIN INIT INFO
# Provides:          cavgoService.service
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: cavgoService
### END INIT INFO


NDHOMEPATH='/home/cavisson/netdiagnostics'
if [ -z $NDHOME ]; then 
  NDHOMEPATH='/home/cavisson/netdiagnostics'
  export NDHOME=$NDHOMEPATH
else
  NDHOMEPATH=$NDHOME
  echo $NDHOMEPATH
fi

#export NDHOME=$NDHOMEPATH
<<comnt
    *****************************************************************************************************
    *  "Proxy" service (1) Start cavgoService service and show new started process id  (2) Stop proxy service  *
    *   running on system (3) show proxy services running and its process id (PID). (4) Restart proxy   *
    *   service after killing all running proxy processes and display new process Id. default path      *
    *   of Control binary is "/opt/cavisson/netdiagnostics"                                             *
    *****************************************************************************************************
comnt

#cavgoService
VAL=$1

path=$NDHOME

if [[ "X$path" == "X" ]];then
  path="/home/cavisson/netdiagnostics"  
fi

touch $path/go/cavgoService.pid

goNoOfControlFile()
{
  no=(`ls -x1 $path/go/bin/goAgent`)

  if [[ ${#no[*]} > 1 ]]; then
    echo -e "Multiple goAgent files.\nIt must be one"
    exit 1
  fi
}




gocontrolServiceStop()
{
  z=( $(ps -ef|grep -v "grep" | grep "gocontrolService" | grep -v "bin/cm_ps_data_ex"| tr -s ' ' | cut -d" " -f2) )
  for i in ${z[*]}
    do
      kill -10 $i
    done
}


goAgentStop()
{
  if [ -f ${path}/go/cavgoService.pid ]; then
    y=(`cat $path/go/cavgoService.pid`)
    if [ -z $y ];then
        y=( $(ps -ef|grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"| tr -s ' ' | cut -d" " -f2) )
    fi
  else
    y=( $(ps -ef|grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"| tr -s ' ' | cut -d" " -f2) )
  fi
  for i in ${y[*]}
    do
      echo -e "Killed process: $i"
      kill -10 $i
    done
}

#Taking user variable from user and match in switch case i.e (1)start (2)stop (3)show (4)restart.
case "$VAL" in
  "start") 
     x=$(ps -ef | grep -v "grep"| grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex" -c)
     #If running proxy process count is 1 than reflect its PID else start a new  one.   
     if [[ $x -eq 1 ]];then 
       y=`ps -ef| grep "bin/goAgent" |grep -v "grep"| grep -v "bin/cm_ps_data_ex"| tr -s ' ' | cut -d" " -f2`
       echo  "cavgoService is already Running with PID ($y)"
       echo $y > $path/go/cavgoService.pid
       ps -ef|grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"
     fi

     if [[ $x -eq 0 ]];then
      goNoOfControlFile
       echo -e "Starting cavgoService"
       nohup /etc/init.d/gocontrolService &>/dev/null &
       #gocontrolServiceStop
       sleep 2
       y=`ps -ef| grep "bin/goAgent" |grep -v "grep"|  grep -v "bin/cm_ps_data_ex" | tr -s ' ' | cut -d" " -f2`
       echo "cavgoService started with PID ($y)"
       echo $y > $path/go/cavgoService.pid
       ps -ef | grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"
     fi
  ;;


  #Killing all the running processes  
  "stop")
    
     gocontrolServiceStop
     x=$(ps -ef | grep -v "grep"| grep "bin/goAgent"|  grep -v "bin/cm_ps_data_ex" -c)     
     if [ $x -eq 0 ];then
       echo "cavgoService is not Running."
     else
       echo "Stopping cavgoService"
       goAgentStop
       > $path/go/cavgoService.pid
     fi
   ;;

   
  "show")
     #Shows all the running process with PIDs if running else show "No proxy is running"   
     y=$(ps -ef | grep -v "grep"| grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex" -c)
     if [ $y -eq 1 ]; then
       x=$(ps -ef|grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex" | tr -s ' ' | cut -d" " -f2) 
       echo -e "Running cavgoService Connection with PID ($x)"
       ps -ef | grep -v "grep" | grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"
     else
       echo -e "cavgoService is not Running"
     fi   
   ;;

  "restart")
    #Kill the running process and start new one and shows newly created PIDs
    # gogocontrolServiceStop
     goNoOfControlFile
     goAgentStop
     nohup /etc/init.d/gocontrolService &>/dev/null &
     sleep 1
     x=`ps -ef| grep "bin/goAgent" |grep -v "grep"|  grep -v "bin/cm_ps_data_ex"| tr -s ' ' | cut -d" " -f2`
     echo -e "Restarting cavgoService with PID ($x)"
     echo $x > $path/go/cavgoService.pid
     ps -ef | grep -v "grep "| grep "bin/goAgent" |  grep -v "bin/cm_ps_data_ex"
   ;;

   *)
     echo -e "Usage: /home/cavisson/etc/init.d/cavgoService   {start|stop|show|restart}"
esac
