#! /bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by CA, INC.
#* The program, including this material, may not be duplicated,
#* disclosed or reproduced in whole or in part for any purpose without
#* the express written authorization of CA. All authorized reproductions
#* must be marked with this legend.
#*
#*                          Copyright (c) 2007
#*                               CA, Inc.
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#

### BEGIN INIT INFO
# CA Product Entry
# Provides:  CA-cam
# Product Aliases: CAM CCS-CAM ccs-cam ca-cs-cam cam
# Required-Start: $named $remote_fs
# Required-Stop: 
# Default-Start: 
# Default-Stop: 
# chkconfig: 
# Description: CA Messaging Services (CAM)  
### END INIT INFO

if [ "`uname`" = "Linux" ]; then
    ECHO="echo -e"
else
    ECHO="echo"
fi

# Sourcing overwrites our parameters
argv=$1

[ -f /etc/profile.CA ] && . /etc/profile.CA all 2>/dev/null

CAM="$CAI_MSQ/bin/cam"
CAMF="$CAI_MSQ/bin/camf"

test -x "$CAM" || { $ECHO "$CAM does not exist";  exit 5 ; }

COMP=CA-cam

rc=0
case "$argv" in
   start)
      "$CASHCOMP/bin/unisrvcntr" start -X $COMP
      #CHECK STATUS - EXIT IF RUNNING
      $0 status >/dev/null
      if [ $? -eq 0 ];then
         $ECHO "CAM service is already running"
         exit 0;
      fi
      #START COMPONENT
      if [ -f "$CAI_MSQ/scripts/envset" ]; then
         . "$CAI_MSQ/scripts/envset"
      fi
      $CAM
      ;;
   stop)
      "$CASHCOMP/bin/unisrvcntr" shutdown -X $COMP
      #CHECK STATUS - EXIT IF STOPPED
      $0 status >/dev/null
      retstat=$?
      if [ $retstat -ne 0 ]; then
         if [ $retstat -eq 2 ]; then
            $ECHO "Multiple CAM services are running,cannot be stopped"
         else
         $ECHO "CAM service is not running"
         fi
         exit 0;
      fi
      #STOP COMPONENT - should respond to TERM signal and close tidily
      $ECHO "Stopping CAM service. "
      "$CAI_MSQ/bin/camclose"

      # check again to see if it actually closed
      # camclose may be resticted to forbid local control of the CAM service
      # check for multiple cam instances
      if [ -x "$CASHCOMP/bin/parseps" ]; then
         pidcnt1=`"$CASHCOMP/bin/parseps" -v $CAMF |wc -w`
         pidcnt2=`"$CASHCOMP/bin/parseps" -v $CAM |wc -w`
         pidcam=`"$CASHCOMP/bin/parseps" -v $CAMF | cut -d " " -f1`
         # $CAM forks and execs $CAMF - on some OSes the name doesn't change
         [ $pidcam -eq 0 ] && pidcam=`"$CASHCOMP/bin/parseps" -v $CAM | cut -d " " -f1`
      else
         pidcnt1=`ps -efl | grep cam | grep -v grep | grep -v lsm | grep -v rpm | grep -v cam[a-zA-Z0-9] | grep -v [a-zA-Z0-9]cam | grep -v CA-cam  |  awk '{print $4}' | wc -w`
	 pidcnt2=0
         pidcam=`ps -efl | grep cam | grep -v grep | grep -v lsm | grep -v rpm | grep -v cam[a-zA-Z0-9] | grep -v [a-zA-Z0-9]cam | grep -v CA-cam  |  awk '{print $4}' | head -1`
         pidcam=${pidcam:-0}
      fi

      if [ $pidcam -ne 0 ]; then
          # Try to close (tidily) using signal - it may have gone already
          kill -TERM $pidcam 2>/dev/null
	 if [ "$pidcnt1" -gt 1 ] || [ "$pidcnt2" -gt 1 ]; then
            $ECHO "Found multiple CAM instances,unable to kill all" 
         fi      
      fi
      ;;
   restart)
      ## Stop the service and regardless of whether it was
      ## running or not, start it again.
      "$CASHCOMP/bin/unisrvcntr" cycle $COMP
      ;;
   status)
      #OUTPUT STATUS INFORMATION
      if [ -x "$CASHCOMP/bin/parseps" ]; then
         pidcnt1=`"$CASHCOMP/bin/parseps" -v $CAMF |wc -w`
         pidcnt2=`"$CASHCOMP/bin/parseps" -v $CAM |wc -w`
         pidcam=`"$CASHCOMP/bin/parseps" -v $CAMF |cut -d " " -f1`
         [ $pidcam -eq 0 ] && pidcam=`"$CASHCOMP/bin/parseps" -v $CAM |cut -d " " -f1`
      else
         pidcnt1=`ps -efl | grep cam | grep -v grep | grep -v lsm | grep -v rpm | grep -v cam[a-zA-Z0-9] | grep -v [a-zA-Z0-9]cam | grep -v CA-cam  |  awk '{print $4}' | wc -w`
	 pidcnt2=0
         pidcam=`ps -efl | grep cam | grep -v grep | grep -v lsm | grep -v rpm | grep -v cam[a-zA-Z0-9] | grep -v [a-zA-Z0-9]cam | grep -v CA-cam  |  awk '{print $4}' | head -1`
         pidcam=${pidcam:-0}
      fi

      if [ $pidcam -ne 0 ]; then
         $ECHO "CAM service $pidcam running"
	 if [ "$pidcnt1" -gt 1 ] || [ "$pidcnt2" -gt 1 ]; then
		exit 2
      fi      
      else
         $ECHO "CAM Service not running"
         rc=1
      fi
      ;;
   probe)
      ## Optional: Probe for the necessity of a reload,
      ## give out the argument which is required for a reload.      
      ;;
   *)
      echo "Usage: $0 {start|stop|status|restart|probe}"
      exit 1
      ;;
esac

exit $rc
