#!/bin/sh
#
# (C) Copyright IBM Corporation 2011
#
# chkconfig: 35 95 5
# description: TSM Client Acceptor Daemon
#
### BEGIN INIT INFO
# Provides: dsmcad
# Required-Start: $local_fs $remote_fs $network
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: TSM Client Acceptor Daemon
# Description: Start dsmcad to enable scheduler and Web GUI.
### END INIT INFO

DSMCAD_DIR=/opt/tivoli/tsm/client/ba/bin
DSMCAD_BIN=$DSMCAD_DIR/dsmcad
# 03/09/17 khe - Add these 3 lines permanently
export LD_LIBRARY_PATH=/opt/tivoli/tsm/client/ba/bin
export DSM_CONFIG=$DSMCAD_DIR/dsm_khe.opt	# 03/09/17 khe - Should only have to modify this line in the script
export DSM_DIR=$DSMCAD_DIR

PIDTMPFILE=/var/run/${0##*/}_tmp.pid	# 03/09/17 khe - Add PIDFILE using the script name
PIDFILE=/var/run/${0##*/}.pid		# 03/09/17 khe - Add PIDFILE using the script name
RETVAL=0				# 03/09/17 khe - Add RETVAL
LOCKFILE=/var/lock/subsys/${0##*/}	# 03/09/17 khe - Add LOCKFILE

if [ ! -x $DSMCAD_BIN ]
then
   echo "$DSMCAD_BIN is not installed"
   if [ "$1" = "stop" ]
   then
      exit 0
   else
      exit 5
   fi
fi

if [ ! -e $DSM_CONFIG ]	# 03/09/17 khe - Add this if statement construct for DSM_CONFIG
then
   echo "$DSM_CONFIG file is not found"
   if [ "$1" = "stop" ]
   then
      exit 0
   else
      exit 5
   fi
fi

if [ -f /etc/redhat-release ]
then
   SYSTEMCTL_SKIP_REDIRECT=1
   . /etc/init.d/functions

   start_()
   {
       if status -p "$PIDFILE" -l "$LOCKFILE"  $DSMCAD_BIN > /dev/null 2>&1 ; then	# 03/09/17 khe - Add If
         echo "dsmcad -optfile=$DSM_CONFIG already started, exiting . . . "
         exit 0
      fi
      echo "Starting dsmcad -optfile=$DSM_CONFIG:"			# 03/09/17 khe - Add -optfile
      cd $DSMCAD_DIR
      daemon --pidfile="$PIDFILE" $DSMCAD_BIN -optfile=$DSM_CONFIG	# 03/09/17 khe - Add DSM_CONFIG, --PIDFILE
      RETVAL=$?								# 03/09/17 khe - Add RETVAL
      echo
      ps -C dsmcad -o pid=,args= > $PIDTMPFILE				# 03/09/17 khe - Add ps to capture PID values
      MYPID=$( egrep -i "${DSM_CONFIG##*/}" $PIDTMPFILE | awk '{print $1}' )	# 03/09/17 khe - Capture my PID value
      [ "$MYPID" ]  && echo "$MYPID" > $PIDFILE				# 03/09/17 khe - Write PID to pid file
      rm $PIDTMPFILE > /dev/null 2>&1					# 03/09/17 khe - Remove Temp pid file
      [ $RETVAL -eq 0 ] && touch $LOCKFILE				# 03/09/17 khe - Add touch
      return $RETVAL							# 03/09/17 khe - Add return
   }

   stop_()
   {
      echo "Stopping dsmcad -optfile=$DSM_CONFIG:"			# 03/09/17 khe - Add DSM_CONFIG
      killproc  -p $PIDFILE -d 10 $DSMCAD_BIN				# 03/09/17 khe - Add -p
      RETVAL=$?								# 03/09/17 khe - Add RETVAL
      echo
      [ $RETVAL -eq 0 ] && rm -f $LOCKFILE > /dev/null			# 03/09/17 khe - Add rm -f
      [ $RETVAL -eq 0 ] && rm -f $PIDFILE > /dev/null			# 03/09/17 khe - Add rm -f
      return $RETVAL							# 03/09/17 khe - Add RETVAL
   }
   
   status_()
   {
      status -p "$PIDFILE" -l "$LOCKFILE"  $DSMCAD_BIN			# 03/09/17 khe - Add -p and -l
   }

elif [ -f /etc/SuSE-release ]
then
   . /etc/rc.status

   rc_reset

   export LANG=en_US.UTF-8
   export LC_ALL=en_US.UTF-8

   start_()
   {
      echo -n "Starting dsmcad:"
      cd $DSMCAD_DIR
      startproc $DSMCAD_BIN
      rc_status -v
   }

   stop_()
   {
      echo -n "Stopping dsmcad:"
      killproc $DSMCAD_BIN
      rc_status -v
   }
   
   status_()
   {
      echo -n "Checking dsmcad:"
      checkproc $DSMCAD_BIN
      rc_status -v
   }

elif [ -f /etc/os-release ]
then
   . /etc/os-release

   if [ $NAME = "Ubuntu" ]
   then

      start_()
      {
         cd $DSMCAD_DIR
         if start-stop-daemon --status --exec $DSMCAD_BIN
         then
            echo "dsmcad is already running, pid" `pidof $DSMCAD_BIN`
         else
            if start-stop-daemon --start --exec $DSMCAD_BIN
            then
               echo "dsmcad is started, pid" `pidof $DSMCAD_BIN`
            else
               echo "dsmcad could not be started"
            fi
         fi
      }

      stop_()
      {
         if start-stop-daemon --status --exec $DSMCAD_BIN
         then
            if start-stop-daemon --stop --exec $DSMCAD_BIN
            then
               echo "dsmcad is stopped"
            else
               echo "dsmcad could not be stopped"
            fi
         else
            echo "dsmcad is not running"
         fi
      }

      status_()
      {
         if start-stop-daemon --status --exec $DSMCAD_BIN
         then
            echo "dsmcad is running, pid" `pidof $DSMCAD_BIN`
         else
            echo "dsmcad is not running"
         fi
      }

   fi

else
   echo "This distribution is not supported"
   exit 2
fi



case "$1" in
   start)
      start_
   ;;

   stop)
      stop_
   ;;

   restart)
      stop_
      sleep 2
      start_
   ;;

   status)
      status_
   ;;

   *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
   ;;
esac


