#!/bin/bash
# %full_filespec: hmpd~11:ascii:ker#1 %
#
######################################################################
#
# Copyright (C) 2005-2013 Dialogic Corporation.  All Rights Reserved.
# All names, products, and services mentioned herein are the
# trademarks or registered trademarks of their respective
# organizations and are the sole property of their respective
# owners.
#
######################################################################

# Source function library.
#. /etc/init.d/functions
. /lib/lsb/init-functions
# expand_aliases needed by Red Hat for lsb init-functions
shopt -s expand_aliases

# Source networking configuration.
#. /etc/sysconfig/network

# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0     FIXME

RETVAL=0
prog="ssp_x86Linux_boot"

# Check for file containing our environment variables.
# If it is not found, abort the script execution.

OURVARS=/etc/profile.d/ct_intel.sh
if [ ! -x ${OURVARS} ]
then
   echo "$0: FATAL ERROR: Unable to find ${OURVARS}."
   echo "$0: FATAL ERROR: Execution aborted due to invalid installation."
   echo "$0: FATAL ERROR: Please reinstall the software by running install.sh."
   exit $NOTINST_ERROR
fi

if [ -z "${INTEL_DIALOGIC_DIR}" ]
then
   # Get our environment variables
   . ${OURVARS}
fi

reset() {
    # This is a special case that is being added that will reset the MLM without full stop/start
    # Stop the MLM
    PID=`pidof $prog`
    [ "$PID" != "" ] && kill -9 $PID
    rm -f /var/run/$prog.pid
    ${INTEL_DIALOGIC_BIN}/ssp_ipc_rm

	if [ -x ${INTEL_DIALOGIC_BIN}/umbc_ipc_rm ]
    then
		${INTEL_DIALOGIC_BIN}/umbc_ipc_rm -u
    fi

    if [ -x ${INTEL_DIALOGIC_BIN}/tvl2_ipc_rm ]
    then
		${INTEL_DIALOGIC_BIN}/tvl2_ipc_rm
    fi

    # Sleep is to allow cleanup to finish before restarting mlm.
    sleep 4 

    # Restart the MLM
    currMsgmnbVal=`cat /proc/sys/kernel/msgmnb`
    if [ $currMsgmnbVal -lt 65536 ]
    then
      echo 65536 > /proc/sys/kernel/msgmnb
    fi

    ulimit -n 8192
    ulimit -c 0   # unlimited  for SSP daemon core file only

    # cd to the "data" directory before starting the SSP daemon so the core will be dumped there
    cd ${INTEL_DIALOGIC_DIR}/data

    echo -n $"Resetting $prog: "

    # Start SSP as a daemon with core dump enabled
    if [ -x /sbin/start-stop-daemon ]
    then
		DAEMON_COREFILE_LIMIT="unlimited" /sbin/start-stop-daemon -S -q -x ${INTEL_DIALOGIC_BIN}/$prog
		RETVAL=$?
    else
		DAEMON_COREFILE_LIMIT="unlimited" start_daemon ${INTEL_DIALOGIC_BIN}/$prog $OPTIONS
		RETVAL=$?
    fi
    if [ $RETVAL -eq 0 ] ; then
	log_success_msg
	pidof ${prog} > /var/run/${prog}.pid 2> /dev/null
    else
	log_failure_msg
    fi

    return $RETVAL
}

start() {
  PID=`pidof $prog`
	if [ "${PID}" = "" ] ; then
	  mkdir -p  ${DLGCROOT}/hmpipc
	  chmod 777 ${DLGCROOT}/hmpipc
     currMsgmnbVal=`cat /proc/sys/kernel/msgmnb`
     if [ $currMsgmnbVal -lt 65536 ]
     then
     	echo 65536 > /proc/sys/kernel/msgmnb
     fi
          ulimit -n 8192
          ulimit -c unlimited  # for SSP daemon core file only

          # cd to the "data" directory before starting the SSP daemon so the core will be dumped there
          cd ${INTEL_DIALOGIC_DIR}/data

          # Start SSP as a daemon with core dump enabled
          echo -n $"Starting $prog: "

		  if [ -x /sbin/start-stop-daemon ]
		  then
			  DAEMON_COREFILE_LIMIT="unlimited" /sbin/start-stop-daemon -S -q -x ${INTEL_DIALOGIC_BIN}/$prog
			  RETVAL=$?
		  else
			  DAEMON_COREFILE_LIMIT="unlimited" start_daemon ${INTEL_DIALOGIC_BIN}/$prog $OPTIONS
			  RETVAL=$?
		  fi
	  RETVAL=$?
          #[ $RETVAL -eq 0 ]
    if [ $RETVAL -eq 0 ]; then
      log_success_msg
      pidof ${prog} > /var/run/${prog}.pid 2> /dev/null
    else
      log_failure_msg
    fi

	  return $RETVAL
	else
	  echo  "ssp_x86Linux_boot already running PID: $PID "
	  RETVAL=0
	  return $RETVAL
	fi
}

stop() {
  # Stop daemons.
  ulimit -c 0 # Disables coredumps while dlstop
  echo -n $"Shutting down $prog: "
  PID=`pidof $prog`
  [ "$PID" != "" ] && kill -9 $PID
  rm -f /var/run/$prog.pid

  if [ -x ${INTEL_DIALOGIC_BIN}/umbc_ipc_rm ]
  then
      ${INTEL_DIALOGIC_BIN}/umbc_ipc_rm
  fi

  if [ -x ${INTEL_DIALOGIC_BIN}/tvl2_ipc_rm ]
  then
      ${INTEL_DIALOGIC_BIN}/tvl2_ipc_rm
  fi

  ${INTEL_DIALOGIC_BIN}/ssp_ipc_rm
  rm -rf ${DLGCROOT}/hmpipc
  log_success_msg
  return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  reset)
	reset
		;;
  status)
	PID=`pidof ${INTEL_DIALOGIC_BIN}/$prog`
	RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    echo "$prog (pid $PID) is running..."
  else
    echo "$prog is stopped"
  fi
	;;
  restart|reload)
	stop
	sleep 3
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f ${INTEL_DIALOGIC_BIN}/$prog ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  *)
        echo $"Usage: $0 {start|stop|reset|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL
