#!/bin/bash
# vim:set ts=3 sw=3:
#
######################################################################
# Copyright (C) 2008-2011 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.
#####################################################################

# Global Return Codes
PROG_RUNNING=0
PROG_DEADPID=1
PROG_DEADLOC=2
PROG_STOPPED=3
PROG_UNKNOWN=4

####################################################################
# Defines
####################################################################

MASTERCFG=${INTEL_DIALOGIC_CFG}/.mastercfg
TEMP_FILE="/tmp/.temp"
SUCCESS=$PROG_RUNNING
FAILURE=$PROG_STOPPED

####################################################################
# cleanup before exit
####################################################################
clean_up_before_exit() {

	delete_file "$TEMP_FILE"

	# Kill all processes started before
	stop_log # Stops teelogger
}

####################################################################
# delete temporary file
####################################################################
delete_file () {

	if [ -e "$1" ]
	then
		rm -f "$1"
	fi
}

####################################################################
# strip comments from a line
####################################################################
strip_comment () {

	IS_COMMENT=$2
	TEMP_LINE="$1"

	# Remove c style comments that start and end in the same line
	TEMP_LINE=`echo "$TEMP_LINE" | sed "s%\/\*.*\*\/%%"`

	TEMP_VAR=`echo "$TEMP_LINE" | grep "\/\*.*"`
	if [ -n "$TEMP_VAR" ] # beginning of a comment
	then
	   TEMP_LINE=`echo "$TEMP_LINE" | sed "s%\/\*.*%%"`
	   echo "$TEMP_LINE"
	   exit 1 # Indicates beginning of a comment
	else
	   TEMP_VAR=`echo "$TEMP_LINE" | grep "\*\/.*"`
	   if [ -n "$TEMP_VAR" ] # end of a comment
	   then
	      TEMP_LINE=`echo "$TEMP_LINE" | sed "s%.*\*\/%%"`
	      echo "$TEMP_LINE"
	      exit 0 # Indicates end of a comment
	   else # uncommented line
	      if [ "$IS_COMMENT" -eq 0 ] # not part of a comment
	      then 
	# Remove other types of comments here
		 TEMP_LINE=`echo "$TEMP_LINE" | sed "s%#.*%%" | sed "s%\/\/.*%%" | sed "s%;.*%%"`
		 echo "$TEMP_LINE"
		 exit 0
	      else # part of a comment started earlier
		 echo ""
		 exit 1
	      fi
	   fi
	fi
}

####################################################################
# check if argument is a number
####################################################################
isdigit () {
	[ $# -eq 1 ] || return $FAILURE
	case $1 in
	   *[!0-9]*|"") return $FAILURE;;
	   *) return $SUCCESS;;
	esac
}

####################################################################
# settings for RedHat Linux Interface
# used as Default Interface
####################################################################
set_redhat_lsb_interface()
{
	dl_start_daemon="/etc/redhat-lsb/lsb_start_daemon"
	dl_stop_daemon="/etc/redhat-lsb/lsb_killproc"
	dl_pidofproc="/etc/redhat-lsb/lsb_pidofproc"
	dl_log_success_msg="/etc/redhat-lsb/lsb_log_message success"
	dl_log_failure_msg="/etc/redhat-lsb/lsb_log_message failure"
	dl_log_warning_msg="/etc/redhat-lsb/lsb_log_message warning"
	. /etc/rc.d/init.d/functions &> /dev/null
	dl_status="status"
}

####################################################################
# settings for SuSe Linux Interface
####################################################################
set_suse_lsb_interface()
{
	. /lib/lsb/init-functions
	dl_start_daemon="/sbin/start_daemon"
	dl_stop_daemon="/sbin/killproc"
	dl_pidofproc="/sbin/pidofproc"
	dl_status="/sbin/checkproc"
	dl_log_success_msg=log_success_msg
	dl_log_failure_msg=log_failure_msg
	dl_log_warning_msg=log_warning_msg
}
####################################################################
# settings for Turbilinux Linux Interface
####################################################################
set_turbo_lsb_interface()
{
	dl_start_daemon="/etc/turbo-lsb/lsb_start_daemon"
	dl_stop_daemon="/etc/turbo-lsb/lsb_killproc"
	dl_pidofproc="/etc/turbo-lsb/lsb_pidofproc"
	dl_log_success_msg="/etc/turbo-lsb/lsb_log_message success"
	dl_log_failure_msg="/etc/turbo-lsb/lsb_log_message failure"
	dl_log_warning_msg="/etc/turbo-lsb/lsb_log_message warning"
	. /etc/rc.d/init.d/functions &> /dev/null
	dl_status="status"
}	
####################################################################
# settings for Debian Linux Interface
####################################################################
set_debian_lsb_interface()
{
	. /lib/lsb/init-functions
	dl_start_daemon="start_daemon"
	dl_stop_daemon="killproc"
	dl_pidofproc="pidofproc"
	dl_status=pid_status
	dl_log_success_msg=log_success_msg
	dl_log_failure_msg=log_failure_msg
	dl_log_warning_msg=log_warning_msg
}

####################################################################
# functions for Debian Linux
####################################################################
pid_status()
{
  . /lib/lsb/init-functions
  BASENAME=${1##*/}
  VARRUN=0
  VARLOCK=0
  PID=`${dl_pidofproc} $BASENAME 2>/dev/null`
  RETC=$?
  if [ -f "/var/run/$BASENAME.pid" ]; then
    VARRUN=1
  fi
  if [ -f "/var/lock/subsys/$BASENAME" ]; then
    VARLOCK=1
  fi
  if [ -n "$PID" ]; then
    echo "$1 (pid $PID) is running..."
    return 0
  fi
  if [ $VARRUN = 1 ]; then
    echo "$1 is dead but /var/run/$BASENAME.pid exists"
    return 1
  fi
  if [ $VARLOCK = 1 ]; then
    echo "$1 is dead but /var/lock/subsys/$BASENAME exists"
    return 2
  fi
  echo "$1 is stopped"
  return 3
}

####################################################################
# log messages functions for Mandrake Linux, since Mandrake's
# log functions only log success or failure, no user data is 
# logged.
####################################################################
dl_success_msg() {
	echo -n $*
	log_success_msg 
	echo
}

dl_failure_msg() {
	echo 
	echo -n $*
	log_failure_msg
	echo
}

dl_warning_msg() {
	echo -n $*
	log_warning_msg
	echo
}

####################################################################
# settings for Mandrake Linux Interface
####################################################################
set_mandrake_lsb_interface()
{
	. /lib/lsb/init-functions
	dl_start_daemon=start_daemon
	dl_stop_daemon=killproc
	dl_pidofproc=pidofproc
	dl_status=status
	dl_log_success_msg=dl_success_msg
	dl_log_failure_msg=dl_failure_msg
	dl_log_warning_msg=dl_warning_msg
}
####################################################################
# set the proper lsb interface
# Add Different Versions of Linux Here
####################################################################
set_lsb_interface()
{
	if [ "${DIST}" = "SuSE" ]
	then
		set_suse_lsb_interface	
	elif [ "${DIST}" = "RedHat" ]
	then
		set_redhat_lsb_interface
	elif [ "${DIST}" = "Debian" ]
	then
		set_debian_lsb_interface
	elif [ "${DIST}" = "Turbolinux" ]
	then
		set_turbo_lsb_interface			
	else
		echo "Error Unrecognized Linux OS Release"
		exit 1
	fi
}

####################################################################
# set the rmem_max setting in the /proc filesystem
####################################################################

set_rmem_max()
{
	RMEMSIZE_TO_SET=1024000
	RMEMSIZE=`cat /proc/sys/net/core/rmem_max 2>/dev/null`

	[ ${RMEMSIZE_TO_SET} -gt ${RMEMSIZE} ] &&
		echo ${RMEMSIZE_TO_SET} > /proc/sys/net/core/rmem_max
}

####################################################################
# set the shmmax setting in the /proc filesystem
####################################################################

set_shmem_max()
{
	SHMEMSIZE_TO_SET=100663296
	SHMEMSIZE=`cat /proc/sys/kernel/shmmax 2>/dev/null`

	[ ${SHMEMSIZE_TO_SET} -gt ${SHMEMSIZE} ] &&
		echo ${SHMEMSIZE_TO_SET} > /proc/sys/kernel/shmmax
}

################################################################
# Generic Function to Start Server Pass in Program Name and Message 
################################################################
dlg_server_start() {
	PROG="$1"
	PROGNAME=${1##*/}
	ARGS="$2"
	MSG="$3 ${MSG_SEP}"
	REQ="$4"
	CREATE_PID=0

	[ "$5" = "--create-pid-file" ] && CREATE_PID=1

	if [ -x ${PROG} ]
	then
		ulimit -c unlimited >/dev/null 2>&1

		type -a runuser &> /dev/null
		if [ $? != 0 ]
		then
			/bin/su -s /bin/bash - -c "${PROG} ${ARGS} &>/dev/null &"
		else
			/sbin/runuser -s /bin/bash - -c "${PROG} ${ARGS} &>/dev/null &"
		fi

		/bin/sleep 1
		${dl_status} ${PROG} &> /dev/null
			RC=$?
			case $RC in
			'0' )
					${dl_log_success_msg} "${MSG}"
					RC=$PROG_RUNNING
               [ ${CREATE_PID} -eq 1 ] && ${dl_pidofproc} ${PROGNAME} > /var/run/${PROGNAME}.pid 2> /dev/null
					;;

			'1' )
					${dl_log_failure_msg} "${MSG}"
					RC=$PROG_DEADPID
					;;

			'2' )
					${dl_log_failure_msg} "${MSG}"
					RC=$PROG_DEADLOC
					;;

			'3' )
					${dl_log_failure_msg} "${MSG}"
					RC=$PROG_STOPPED
					;;
			esac
	elif [ "${REQ}" = "1" ]
	then
			${dl_log_failure_msg} ${MSG}
			RC=$PROG_UNKNOWN
	else
			RC=$SUCCESS
	fi

	return $RC
}

################################################################
# Generic Function to Stop Server Pass in Program Name and Message 
# Caution: If any change in this function, dlg_stop_server_bw()
# must be changed as well.
################################################################
dlg_server_stop() {

	PROG="$1"
	PROGNAME=${1##*/}
	MSG="$2 ${MSG_SEP}"
	REQ="$3"
	if [ -x ${PROG} ]
	then
	sleep 1
		${dl_status} ${PROG} &> /dev/null
		RC=$?
		
		if [ $RC -eq 0 ]
		then
			#if -9 used, singal handler will not work to delete the pid file. 
			killall -e  ${PROG} &> /dev/null
			rm -f /var/run/${PROGNAME}.pid
		fi

		${dl_status} ${PROG} &> /dev/null
		RC=$?
		case $RC in

			'3' )
					${dl_log_success_msg} "${MSG}"
					RC=$PROG_STOPPED
					;;

			'2' )
					${dl_log_failure_msg} "${MSG}"
					RC=$PROG_DEADLOC
					;;

			'1' )
					${dl_log_failure_msg} "${MSG}"
					RC=$PROG_DEADPID
					;;

			'0' )
					RC=$PROG_RUNNING
					;;
		esac 
	elif [ "${REQ}" = "1" ]
	then
		${dl_log_failure_msg} ${MSG}
		RC=$PROG_UNKNOWN
	else
		RC=$SUCCESS
	fi

	return $RC
}

####################################################################
# generic function to start a program which is a daemon
#
# Arguments:
#
# $1 = full path to the daemon surrounded by double quotes
# $2 = arguments to pass to the daemon surrounded by double quotes
# $3 = message to be displayed surrounded by double quotes
# $4 = --create-pid-file [optional]
# $5 = --wait=<time in seconds> [optional]
#
# Returns: LSB-compliant return codes
###################################################################
# SUCCESS=0
# ERROR_GENERIC=1 # Generic or unspecified error 
# ERROR_ARGUMEN=2 # Invalid or excess arguments
# ERROR_UNIMPLE=3 # Unimplemented feature
# ERROR_NOPRIVI=4 # Insufficient privilege
# ERROR_NOTINST=5 # Program not installed
# ERROR_NOTCONF=6 # Program not configured
# ERROR_NOTRUNN=7 # Program not running

####################################################################
dlg_daemon_start()
{
	# handle calling errors
	if [ $# -lt 3 -o $# -gt 5 ]
	then
		echo "ERROR: dlg_daemon_start():"
		echo "ERROR: Usage: dlg_daemon_start <path_to_daemon> \"<daemon_args>\" \"<message>\" [--create-pid-file] --wait=<time>"
		return ${ERROR_ARGUMEN}
	fi

	# assume a well behaved program until proven otherwise
	CREATE_PID=0

	# assume no wait until proven otherwise
	WAIT=0

	# get arguments passed from caller
	PROG=$1
	PROGNAME=${1##*/}
	ARGS=$2
	MSG="$3 ${MSG_SEP}"
	[ $# -ge 4 -a "$4" = "--create-pid-file" ] && CREATE_PID=1

	if [ $# -eq 5 ]
	then
		# wait time specified so extract it
		echo $5 | grep "\-\-wait=" && WAIT=${5##*=}
	fi

	# Check for the daemon file
	if [ -x ${PROG} ]
	then
		# daemon file was found
			#echo ${PROG}
			# start the daemon using LSB method
			${dl_start_daemon} ${PROG} ${ARGS} &> /dev/null
			RC=$?

			[ -n ${WAIT} ] && sleep ${WAIT}
			# check return value
			if [ ${RC} -eq ${SUCCESS} ]
			then
				# program successfully started
				[ ${CREATE_PID} -eq 1 ] && ${dl_pidofproc} ${PROGNAME} > /var/run/${PROGNAME}.pid 2> /dev/null
				${dl_log_success_msg} "${MSG}"
				RC=${SUCCESS}
			else
				# some failure occurred
				${dl_log_failure_msg} ${MSG}
				RC=${ERROR_GENERIC}
			fi
	else
		${dl_log_warning_msg} "	${PROG} not found${MS_SEP}"
		${dl_log_failure_msg} ${MSG}
		RC=${ERROR_NOTINST}
	fi
	
	return ${RC}
}


####################################################################
# generic function to stop a program which is a daemon
#
# Arguments:
#
# $1 = full path to the daemon
# $2 = message to be displayed surrounded by double quotes
# $3 = signal to pass to killproc
#
# Returns: LSB-compliant return codes
#
# SUCCESS=0
# ERROR_GENERIC=1 # Generic or unspecified error 
# ERROR_ARGUMEN=2 # Invalid or excess arguments
# ERROR_UNIMPLE=3 # Unimplemented feature
# ERROR_NOPRIVI=4 # Insufficient privilege
# ERROR_NOTINST=5 # Program not installed
# ERROR_NOTCONF=6 # Program not configured
# ERROR_NOTRUNN=7 # Program not running
#
####################################################################
dlg_daemon_stop()
{
	# handle calling errors
	if [ $# -lt 2 -o $# -gt 3 ]
	then
		echo "ERROR: dlg_daemon_stop():"
		echo "ERROR: Usage: dlg_daemon_stop <path_to_daemon> \"<message>\" [<signal>]"
		return ${ERROR_ARGUMEN}
	fi

	# get arguments passed from caller
	PROG=$1
	PROGNAME=${1##*/}
	MSG="$2 ${MSG_SEP}"
	
	[ $# -eq 3 ] && SIG=$3

	if [ -x ${PROG} ]
	then
		if [ "${DIST}" = "Debian"  ]
		then
			${dl_stop_daemon} ${PROGNAME} "HUP"
		else
			${dl_stop_daemon} ${PROGNAME}
		fi
		RC=$?
		sleep 2
		if [ ${RC} -eq ${SUCCESS} ]
		then
			# program was successfully stopped
			${dl_log_success_msg} "${MSG}"
			# shouldn't need to do this if program is well behaved
			rm -f /var/run/${PROGNAME}.pid
			return ${SUCCESS}
		else
			# program was not running
			${dl_log_failure_msg} ${MSG}
			# delete any stale pid files
			rm -f /var/run/${PROGNAME}.pid
			RC=${ERROR_NOTRUNNING}
		fi
	else
		${dl_log_warning_msg} "	${PROG} not found${MSG_SEP}"
		${dl_log_failure_msg} ${MSG}
		RC=${ERROR_NOTINST}
	fi
	
	return ${RC}
}

######################################################################
# Get OS release information
######################################################################
get_release_info()
{
        OS=`uname -s`
 
        if [ "${OS}" = "Linux" ] ; then
                if [ -f /etc/redhat-release ] ; then
                        DIST='RedHat'
                        PSUEDONAME=`cat /etc/redhat-release`
                elif [ -f /etc/SuSE-release ] ; then
                        DIST="SuSE"
                        PSUEDONAME=`cat /etc/SuSE-release`
                elif [ -f /etc/debian_version ] ; then
                        DIST="Debian"
                        PSUEDONAME=`cat /etc/debian_version`
		elif [ -f /etc/turbolinux-release  ] ; then
                        DIST="Turbolinux"
                        PSUEDONAME=`cat /etc/turbolinux-release`
                fi
        else
                #Unsupported OS
                DIST="UNKNOWN"
                PSUEDONAME="UNKNOWN"
        fi
}

################################################################
# Start the application
################################################################
start() {
	PROG="${INTEL_DIALOGIC_DIR}/bin/media_server"
	MSG="Starting Media Server"
	ARGS="${INTEL_DIALOGIC_DIR}/cfg/media_server.xml"
	REQ="0"
	
	dlg_server_start "${PROG}" "${ARGS}" "${MSG}" "${REQ}" "--create-pid-file"
	RC=$?
	return ${RC}
}

################################################################
# Stop the application
################################################################
stop() {

	DIR="${INTEL_DIALOGIC_DIR}/bin"
	PROG="media_server"
	MSG="Shutting Down Media Server ${MSG_SEP}"

	if [ -x ${DIR}/${PROG} ]
	then
		PID=`${dl_pidofproc} ${PROG}`
		if [ "${PID}" != "" ]
		then
			kill -SIGUSR1 ${PID} &> /dev/null
			RETVAL=$?
			if [ ${RETVAL} -eq 0 ]
			then
				for(( i=0 ; i <= 10 ; i++ ))
				do
					/bin/ps -Al | /bin/grep ${PROG} &> /dev/null
					RETVAL=$?
					if [ ${RETVAL} -eq 0 ]
					then
						/bin/sleep 2
					else
						/bin/rm -f /var/run/${PROG}.pid
			#			/bin/sleep 5
						${dl_log_success_msg} "${MSG}"
						RETVAL=0
						return ${RETVAL}
					fi
				done
			fi
                                                                                
			# in case kill -SIGUSR1 ${PID} doesn't shut down the media_server
			/bin/ps -Al | /bin/grep ${PROG} &> /dev/null
			RETVAL=$?
			if [ ${RETVAL} -eq 0 ]
			then
#				echo "issuing  kill -9 ${PID}"
				kill -9 ${PID} &> /dev/null
				/bin/rm -f /var/run/${PROG}.pid
				${dl_log_success_msg} "${MSG}"
			fi
		else
#			echo  "${PROG} is not running."
			RETVAL=0
			return ${RETVAL}
		fi
	fi

	return $RETVAL
}

################################################################
# Enable the application
################################################################
enable() {
   FILE="/etc/profile.d/ct_intel.sh"
 
   # check if the application is already enabled
   grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1$' ${FILE} &> /dev/null
   if [ $? -ne 0 ]
   then
      # check if the application is already disabled
      grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0$' ${FILE} &> /dev/null
      if [ $? -eq 0 ]
      then
         # if it is already disabled, change it to enabled
         cat ${FILE} | sed 's/INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0/INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1/g' > ${FILE}.temp
         if [ -s ${FILE}.temp  ]
         then
            cat ${FILE}.temp > ${FILE}
            rm -f ${FILE}.temp
         else
            echo "Failed to disable the application"
            RC=1
            return ${RC}
         fi
      else
         # remove all old content
         grep '^# media server status (disable=0/enable=1)$' ${FILE} &> /dev/null
         if [ $? -eq 0 ]
         then
            cat ${FILE} | sed '/^# media server status (disable=0/enable=1)/d' &> ${FILE}
         fi
 
         grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=$' ${FILE} &> /dev/null
         if [ $? -eq 0 ]
         then
            cat ${FILE} | sed '/^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=/d' &> ${FILE}
         fi
 
         grep '^export INTEL_DIALOGIC_MEDIA_SERVER_STATUS$' ${FILE} &> /dev/null
         if [ $? -eq 0 ]
         then
            cat ${FILE} | sed '/^export INTEL_DIALOGIC_MEDIA_SERVER_STATUS/d' &> ${FILE}
         fi
 
         # set the required values in the profile script
         echo "# media server status (disable=0/enable=1)" >> ${FILE}
         echo "INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1" >> ${FILE}
         echo "export INTEL_DIALOGIC_MEDIA_SERVER_STATUS" >> ${FILE}
      fi
   else
      # the application is already enabled
      RC=0
   fi
 
   return ${RC}
}

################################################################
# Disable the application
################################################################
disable() {
	FILE="/etc/profile.d/ct_intel.sh"

	# check if the application is already disabled
	grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0$' ${FILE} &> /dev/null
	if [ $? -ne 0 ]
	then
		# check if the application is already enabled
		grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1$' ${FILE} &> /dev/null
		if [ $? -eq 0 ]
		then
			# if it is already enabled, change it to disabled
			cat ${FILE} | sed 's/INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1/INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0/g' > ${FILE}.temp
			if [ -s ${FILE}.temp  ]
			then
				cat ${FILE}.temp > ${FILE}
				rm -f ${FILE}.temp
			else
				echo "Failed to disable the application"
				RC=1
				return ${RC}
			fi
		else
			# remove all old content
			grep '^# media server status (disable=0/enable=1)$' ${FILE} &> /dev/null
			if [ $? -eq 0 ]
			then
				cat ${FILE} | sed '/^# media server status (disable=0/enable=1)/d' &> ${FILE}
			fi

			grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=$' ${FILE} &> /dev/null
			if [ $? -eq 0 ]
			then
				cat ${FILE} | sed '/^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=/d' &> ${FILE}
			fi

			grep '^export INTEL_DIALOGIC_MEDIA_SERVER_STATUS$' ${FILE} &> /dev/null
			if [ $? -eq 0 ]
			then
				cat ${FILE} | sed '/^export INTEL_DIALOGIC_MEDIA_SERVER_STATUS/d' &> ${FILE}
			fi

			# set the required values in the profile script
			echo "# media server status (disable=0/enable=1)" >> ${FILE}
			echo "INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0" >> ${FILE}
			echo "export INTEL_DIALOGIC_MEDIA_SERVER_STATUS" >> ${FILE}
		fi
	else
		# the application is already disabled
		RC=0
	fi
	
	return ${RC}
}

#######################################################################
# isEnabled the application
# check INTEL_DIALOGIC_MEDIA_SERVER_STATUS in /etc/profile.d/ct_intel.sh
# return  0 - disabled
#         1 - enabled
#         2 - error
#######################################################################
isEnabled() {
    FILE="/etc/profile.d/ct_intel.sh"
 
    # check if the application is already enabled
    grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=1$' ${FILE} &> /dev/null
    if [ $? -ne 0 ]
    then
        # check if the application is already disabled
        grep '^INTEL_DIALOGIC_MEDIA_SERVER_STATUS=0$' ${FILE} &> /dev/null
        if [ $? -eq 0 ]
        then
            # it is disabled, 
            echo "MSML media_server (auto start) is disabled"
            RC=0
        else
            # neither enabled nor disabled, return 2 as error
            RC=2
        fi
    else
        # the application is enabled
		echo "MSML media_server (auto start) is enabled"
        RC=1
    fi
 
    return ${RC}
}

######################################################################
# Beginning of script execution
######################################################################

USAGE="Usage: `basename $0` {start|stop|status|enable|disable|isEnabled}"
RETVAL=$SUCCESS
SAVEPATH=$PATH

LSB_INIT_FUNCTIONS=/lib/lsb/init-functions
if [ -f ${LSB_INIT_FUNCTIONS} ]
	then
	# Make sure we have the LSB init functions
	. ${LSB_INIT_FUNCTIONS}
else
	echo "$0: FATAL ERROR: Unable to find ${LSB_INIT_FUNCTIONS}."
	echo "$0: FATAL ERROR: This script requires an LSB compliant distribution."
	echo "$0: FATAL ERROR: Please check that your distribution's LSB package is installed."
	echo "$0: FATAL ERROR: Execution aborted due to invalid OS software installation."
	exit $NOTINST_ERROR
fi
get_release_info
set_lsb_interface

PATH=$SAVEPATH:$PATH
export PATH

# 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
	# if INTEL_DIALOGIC_DIR is not set, then get
	# the Intel Dialogic environment variables
	[ -z "${INTEL_DIALOGIC_DIR}" ] && . ${OURVARS}
else
	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

# LSB return codes 
SUCCESS=0
ERROR_GENERIC=1 # Generic or unspecified error 
ERROR_ARGUMEN=2 # Invalid or excess arguments
ERROR_UNIMPLE=3 # Unimplemented feature
ERROR_NOPRIVI=4 # Insufficient privilege
ERROR_NOTINST=5 # Program not installed
ERROR_NOTCONF=6 # Program not configured
ERROR_NOTRUNN=7 # Program not running

# extract name of service to be used for Linux lock file
SUBSYS=`basename $0`

# Red Hat and SuSE use different message formats, Turbilinux is the same as the Red Hat
MSG_SEP=
	[ -f /etc/redhat-release ] && MSG_SEP=":"
	[ -f /etc/turbolinux-release ] && MSG_SEP=":"

case $1 in 
    start)
        start
        RETVAL=$?
        ;;
    stop)
        stop
        RETVAL=$?
        ;;
    status)
        ${dl_status} media_server
        RETVAL=$?
		isEnabled    # print out if media_server is running/stopped as well as if it is enabled
        ;;
    enable)
        enable
        RETVAL=$?
        ;;
    disable)
        disable
        RETVAL=$?
        ;;
    isEnabled)
        isEnabled
        RETVAL=$?    # 0 - disabled; 1 - enabled; 2 - error
        ;;
    *)
        echo ${USAGE}
        echo "ARGS: $*"
        RETVAL=$ARGUMEN_ERROR
        ;;
esac

exit $RETVAL
