#!/bin/bash
#
#####################################################################
#    Copyright (C) 2001-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.
#
#####################################################################

######################################################################
#
#  Shell script: DM3 Control script
#
######################################################################
SUCCESS=0
PID=0
GCTLOAD_PATH=
SS7_CONFIG=""
DLGCS7D_PID=/var/run/dlgcs7d.pid; export DLGCS7D_PID
GCTLOAD_PID=/var/run/gctload.pid; export GCTLOAD_PID

####################################################################
# set the proper lsb interface
# Add Different Versions of Linux Here
####################################################################
set_lsb_interface()
{
    dl_start_daemon= 
	dl_stop_daemon="killall"
	dl_status="status"
	
	OS=`uname -s`
 
    if [ "${OS}" = "Linux" ] ; then
        if [ -f /etc/redhat-release ] ; then
            dl_start_daemon="/etc/redhat-lsb/lsb_start_daemon"
	        dl_stop_daemon="/etc/redhat-lsb/lsb_killproc"
			DIST='RedHat'
        elif [ -f /etc/SuSE-release ] ; then
		    . /lib/lsb/init-functions
            dl_start_daemon="/sbin/start_daemon"
	        dl_stop_daemon="/sbin/killproc"
            dl_status="/sbin/checkproc"
			DIST="SuSE"
        elif [ -f /etc/debian_version ] ; then
            . /lib/lsb/init-functions
            dl_start_daemon="start_daemon"
            dl_stop_daemon="killproc"
			dl_status=pid_status
            DIST="Debian"
        elif [ -f /etc/turbolinux-release  ] ; then
            dl_start_daemon="/etc/turbo-lsb/lsb_start_daemon"
            dl_stop_daemon="/etc/turbo-lsb/lsb_killproc"
            DIST="Turbolinux"
        fi
    fi
}
######################################################################
# Log entries to syslog
######################################################################
log()
{
	logger -t `basename $0` "$1"
}

######################################################################
# Remove Temporary Cheetah specific files
######################################################################
remove_cheetah_temp_files() {
	# Remove old cheetah files
	if [ -f ${INTEL_DIALOGIC_DIR}/temp/CheetahSharedMemory.dat ]
   then
      rm -f ${INTEL_DIALOGIC_DIR}/temp/CheetahSharedMemory.dat 2>/dev/null
	else
		rm -f /tmp/CheetahSharedMemory.dat	2>/dev/null
   fi
	if [ -f ${INTEL_DIALOGIC_DIR}/temp/CheetahIpcMutex ]
   then
      rm -f ${INTEL_DIALOGIC_DIR}/temp/CheetahIpcMutex 2>/dev/null
	else 
		rm -f /tmp/CheetahIpcMutex 2>/dev/null
   fi
}
######################################################################
# Cheetah specific start part
######################################################################
cheetah_startall() {

	remove_cheetah_temp_files
	# Invoke cheetah_start
	if [ -x ${INTEL_DIALOGIC_BIN}/cheetah_start ] 
	then
		log "Calling ${INTEL_DIALOGIC_BIN}/cheetah_start."
		${INTEL_DIALOGIC_BIN}/cheetah_start
		RC=$?
	fi
   
	if [ $RC -ne 0 ] 
	then
		log "Failed to start cheetah"
		echo "Failed to start cheetah."
		exit 1
	fi
}

cheetah_startboard() {
	ID=$1
	RC=1
	# Invoke cheetah_start
	if [ -x ${INTEL_DIALOGIC_BIN}/cheetah_start ]
	then
		log "Calling ${INTEL_DIALOGIC_BIN}/cheetah_start -conf $ID"
		${INTEL_DIALOGIC_BIN}/cheetah_start -conf $ID
		RC=$?
	fi
   
	if [ $RC -ne 0 ] 
	then
		log "Failed to start cheetah."
		echo "Failed to start cheetah."
		exit 1
	fi
}



######################################################################
#  Remote API Server specific start & stop functions
######################################################################
hrs_run() {
    RC=0
    #check if the hrs_config.xml and remoteapiserver files are present (installed)
    if [ -f ${INTEL_DIALOGIC_CFG}/hrs_config.xml ] && [ -x ${INTEL_DIALOGIC_BIN}/RemoteAPIServer ]; then
        #  check if MSML is enabled
        # if [ ${INTEL_DIALOGIC_MEDIA_SERVER_STATUS} -eq 0 ]; then
        ${INTEL_DIALOGIC_BIN}/mediaserver isEnabled &>/dev/null
        iMSEnable=$?
        if [ $iMSEnable -eq 0 ]; then
            # disabled
            log "Starting Remote API Server"
            log "Calling ${INTEL_DIALOGIC_BIN}/RemoteAPIServer restart"
            PROG="${INTEL_DIALOGIC_BIN}/RemoteAPIServer"
            ARGS="restart"
            ${PROG}  ${ARGS} &> /dev/null
            RC=$?
            if [ $RC = 0 ]; then
                log "Starting Remote API Server success"
            else
                log "Failed to start Remote API Server ($RC)."
            fi
        else
            log "MSML is enabled, not to start Remote API Server"
        fi
    fi
    return $RC
}


##########################################################################
# Stop Remote API Server daemon: ${INTEL_DIALOGIC_BIN}/RemoteAPIServer stop
# RemoteAPIServer requires SIGINT (2) to stop gracefully
# LSB does not accept symbolic signal names
##########################################################################
hrs_stop() {
    RC=0
    if [ -f ${INTEL_DIALOGIC_CFG}/hrs_config.xml ] && [ -x ${INTEL_DIALOGIC_BIN}/RemoteAPIServer ]; then
 #      if [ ${INTEL_DIALOGIC_MEDIA_SERVER_STATUS} == 0 ]; then
        #  check if MSML is enabled
        ${INTEL_DIALOGIC_BIN}/mediaserver isEnabled &>/dev/null
        iMSEnable=$?
        if [ $iMSEnable -eq 0 ]; then
            #check if RemoteAPIServer was configured to operate
            log "Calling ${INTEL_DIALOGIC_BIN}/RemoteAPIServer stop"
            PROG="${INTEL_DIALOGIC_BIN}/RemoteAPIServer"
            ARGS="stop"
            log "Stopping Remote API Server Daemon"
            ${PROG}  ${ARGS} &> /dev/null
            RC=$?
        fi
    fi
    return $RC
}

##########################################################################
# Stop MSML Server: ${INTEL_DIALOGIC_BIN}/media_server stop
##########################################################################
msml_stop() {
    RC=0
   # check if MSML, media_server is running
    log "checking if MSML is running"
    P=`ps -ef | grep "media_server" | grep -v grep`
	# If it is running and mediaserver script is found, then stop it
	if [ ! -z "${P}" ] && [ -x "${INTEL_DIALOGIC_BIN}/mediaserver" ]
  	then
		log "Calling ${INTEL_DIALOGIC_BIN}/mediaserver stop"
		"${INTEL_DIALOGIC_BIN}/mediaserver" stop
		RC=$?
		if [ $RC -ne 0 ] 
		then
			log "Failed to stop MSML."
        fi
    fi
    return $RC
}

##########################################################################
# Start MSML Server: ${INTEL_DIALOGIC_BIN}/media_server start
##########################################################################
msml_start() {
    RC=0
	#  check if MSML is enabled
	log "In msml_start(), check if MSML is enabled"
    # if [ ${INTEL_DIALOGIC_MEDIA_SERVER_STATUS} -eq 0 ]; then
    ${INTEL_DIALOGIC_BIN}/mediaserver isEnabled &>/dev/null
    iMSEnable=$?
    if [ $iMSEnable -eq 1 ]; then
        log "In msml_start(), MSML is enabled"
	    # check if MSML, media_server is running
		log "checking if MSML is running"
		P=`ps -ef | grep "media_server" | grep -v grep`
		# If it is not running and mediaserver script is found, then start it
		if [ -z "${P}" ] && [ -x "${INTEL_DIALOGIC_BIN}/mediaserver" ]
		then
			log "Calling ${INTEL_DIALOGIC_BIN}/mediaserver start"
			"${INTEL_DIALOGIC_BIN}/mediaserver" start
			RC=$?
			if [ $RC -ne 0 ] 
			then
				log "Failed to start MSML."
			fi
		fi
    fi
    return $RC
}

######################################################################
# DM3 specific startup
######################################################################
dm3_startall() {

	HMP_BOOT_KERNEL=ssp_x86Linux_boot

	# if HMP boot kernel file exists
	if [ -x ${INTEL_DIALOGIC_BIN}/${HMP_BOOT_KERNEL} ]
	then
		# look for any running HMP boot kernel processes
		P=`ps -ef | grep ${HMP_BOOT_KERNEL} | grep -v grep`

		# If the HMP boot kernel is not running and hmpd is found, then start it
		if [ -z "${P}" ] && [ -x "${INTEL_DIALOGIC_BIN}/hmpd" ]
  		then
  			log "Calling ${INTEL_DIALOGIC_BIN}/hmpd start"
			"${INTEL_DIALOGIC_BIN}/hmpd" start
			RC=$?
			if [ $RC -ne 0 ] 
			then
				log "Failed to start ssp_x86Linux_boot."
				echo "Failed to start ssp_x86Linux_boot."
				exit 1
			fi
		fi
	fi

	# not to remove board AUID entry. otherwise, a new set of AUIDs are created on each media start
    # dm3_remove_from_devmap

	hrs_run
	
    # Check if any HMP boards in system
    log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep -i dm3 > /dev/null"
    ${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep -i dm3 > /dev/null
    RC=$?
    if [ $RC -ne 0 ]
    then
	# No DM3 boards
	log "No Dialogic(r) DM-family board(s) found."
	echo "No Dialogic(r) DM-family board(s) found."
        # Still need to initialize cheetah
	cheetah_startall
	exit 1
    fi

    # tbparms only needed if pyramid.scd exists
    if [ -f ${INTEL_DIALOGIC_CFG}/pyramid.scd ]
    then
    	if [ -x ${TBPROG} ]
    	then
    	   log "Calling ${INTEL_DIALOGIC_BIN}/tbparms"
    	   ${INTEL_DIALOGIC_BIN}/tbparms &> /dev/null	
           RC=$?
       	   if [ ${RC} -ne 0 ]
    	   then
    	   	log "Execution of tbparms failed."
    	   	echo "Execution of tbparms failed."
    		exit 1
    	   fi
    	fi
     fi

   # Run the downloader to download firmware on board(s)
   if [ -x ${INTEL_DIALOGIC_BIN}/downloader ]
   then
        echo "Starting Dialogic(r) DM-family board(s) download phase."
        log "Calling ${INTEL_DIALOGIC_BIN}/downloader -c -i"
        ${INTEL_DIALOGIC_BIN}/downloader -c -i
	RC=$?
	if [ $RC -ne 0 ]
    	then
    		log "Dialogic(r) DM-family board(s) downloader failed."
		echo " Dialogic(r) DM-family board(s) downloader failed."
        	exit 1
    	fi

   fi

   # Run tbassign to make board know what TDM bus type it is using
   if [ -x ${INTEL_DIALOGIC_BIN}/tbassign ]
   then
      log "Calling ${INTEL_DIALOGIC_BIN}/tbassign -a"
      ${INTEL_DIALOGIC_BIN}/tbassign -a 2>/dev/null
      RC=$?	 
      if [ $RC -ne 0 ] 
      then
      		log "Dialogic(r) DM-family board(s) tbassign failed."
       		echo " Dialogic(r) DM-family board(s) tbassign fail."
                exit 1
      fi
	
   fi
}

######################################################################
# DM3 specific startup for single board
######################################################################
dm3_startboard() {

    BOARD=$1
    RC=0
    # Run the downloader to download firmware on board(s)
    if [ -x ${INTEL_DIALOGIC_BIN}/downloader ]
    then
        echo " "
        echo "Starting Dialogic(r) DM-family board(s) download phase."
        log "Calling ${INTEL_DIALOGIC_BIN}/downloader -c -i -b $BOARD"
	${INTEL_DIALOGIC_BIN}/downloader -c -i -b $BOARD
        RC=$?
        if [ $RC -ne 0 ] ; then
           log "Failed to download firmware on board(s)."
	   return 1
        fi
    fi
    # Run tbassign to make board know what TDM bus type it is using
    if [ -x ${INTEL_DIALOGIC_BIN}/tbassign ]
    then
        log "Calling ${INTEL_DIALOGIC_BIN}/tbassign -b $BOARD"
        ${INTEL_DIALOGIC_BIN}/tbassign -b $BOARD
        RC=$?
       
        if [ $RC -ne 0 ] ; then
           log "Failed to run tbassign."
	   return 1
        fi
    fi
    return $RC

}

######################################################################
# DMR4  specific startup all boards part
######################################################################
dmr4_startall() {

    # Invoke Clusterpkg 
    if [ -x ${INTEL_DIALOGIC_BIN}/clusterpkg ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/clusterpkg -L${INTEL_DIALOGIC_DIR}/log/clusterpkg.log"
	${INTEL_DIALOGIC_BIN}/clusterpkg -L${INTEL_DIALOGIC_DIR}/log/clusterpkg.log 
        RC=$?
        if [ $RC -ne 0 ] 
        then 
        	log "Dialogic(r) DM-family board(s) clusterpkg fail."
                echo " Dialogic(r) DM-family board(s) clusterpkg fail."
                exit 1
        fi 

    fi

	# Run PDKManager if pdk.cfg is present
	if [ -f ${INTEL_DIALOGIC_CFG}/pdk.cfg ]
	then
		log "Calling ${INTEL_DIALOGIC_BIN}/PDKManager -autodownload -fastset -L${INTEL_DIALOGIC_DIR}/log/pdk.log -console"
		${INTEL_DIALOGIC_BIN}/PDKManager -autodownload -fastset -L${INTEL_DIALOGIC_DIR}/log/pdk.log -console
		RC=$?
		if [ $RC -ne 0 ] 
		then 
			log "Failed to start PDK Manager."
			echo "Failed to start PDK Manager."
			exit 1
		fi
	fi        

    # Removing the .CleoStateMutex file
    if [ -f ${INTEL_DIALOGIC_DIR}/temp/.CleoStateMutex ]
    then
		rm -f "${INTEL_DIALOGIC_DIR}/temp/.CleoStateMutex" 2>/dev/null
	 else
		rm -f /tmp/.CleoStateMutex
    fi

    # Invoke regvox
    if [ -x ${INTEL_DIALOGIC_BIN}/regvox ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/regvox -m"
	${INTEL_DIALOGIC_BIN}/regvox -m
	RC=$?
        if [ $RC -ne 0 ]
        then
        	log "Dialogic(r) DM-family board(s) regvox fail."
                echo " Dialogic(r) DM-family board(s) regvox fail." 
                exit 1
        fi

    fi

    # Invoke iptconf
    if [ -x ${INTEL_DIALOGIC_BIN}/iptconf ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/iptconf > ${INTEL_DIALOGIC_DIR}/log/iptconf.log"
	${INTEL_DIALOGIC_BIN}/iptconf > ${INTEL_DIALOGIC_DIR}/log/iptconf.log 2>&1
	RC=$?
        if [ $RC -ne 0 ]
        then
        	log "Dialogic(r) DM-family board(s) iptconf fail."
                echo " Dialogic(r) DM-family board(s) iptconf fail." 
                exit 1
        fi

    fi

    # Invoke confslot
    if [ -x ${INTEL_DIALOGIC_BIN}/confslot ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/confslot > ${INTEL_DIALOGIC_DIR}/log/confslot.log"
	${INTEL_DIALOGIC_BIN}/confslot > ${INTEL_DIALOGIC_DIR}/log/confslot.log 2>&1
	RC=$?
        if [ $RC -ne 0 ]
        then
        	log "Dialogic(r) DM-family board(s) confslot fail."
                echo " Dialogic(r) DM-family board(s) confslot fail." 
                exit 1
        fi

    fi
 
    if [ "${DIALOGIC_PLATFORM}" = "HMP" ] && [ "${DIALOGIC_RELNUM}" = "3.1" ] ||
      	[ "${DIALOGIC_RELNUM}" = "4.1" ] && [ "${DIALOGIC_PLATFORM}" = "HMP" ] ||
    	[ "${DIALOGIC_PLATFORM}" = "ZVL" ]
    then
	    # Start fault detection
	    if [ -x ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline ]
	    then
	    	log "Calling ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --start"
		${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --start
		RC=$?
		
		if [ $RC -ne 0 ]
		then
			log "Failed to start fault detection."
			exit 1
		fi
	    fi
    fi
}

######################################################################
# DMR4  specific startup part
######################################################################
dmr4_startboard() {

    BOARD=$1
    AUID=$2

    RC=0
    # Invoke Clusterpkg 
    if [ -x ${INTEL_DIALOGIC_BIN}/clusterpkg ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/clusterpkg  -b$BOARD"
	${INTEL_DIALOGIC_BIN}/clusterpkg  -b$BOARD
        RC=$?
        
    	if [ $RC -ne 0 ] ; then
    		log "Failed to start clusterpkg."
    		return 1
    	fi
    fi

    # Run PDKManager if pdk.cfg is present
    if [ -f ${INTEL_DIALOGIC_CFG}/pdk.cfg ]
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/PDKManager -board $BOARD -autodownload -fastset -L${INTEL_DIALOGIC_DIR}/log/pdk.log -console"
        ${INTEL_DIALOGIC_BIN}/PDKManager -board $BOARD -autodownload -fastset -L${INTEL_DIALOGIC_DIR}/log/pdk.log -console
        RC=$?
        if [ $RC -ne 0 ] 
        then 
            log "Failed to start PDK Manager."
            echo "Failed to start PDK Manager."
            exit 1
        fi
    fi

    # Invoke regvox
    if [ -x ${INTEL_DIALOGIC_BIN}/regvox ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/regvox -m -b $BOARD"
	${INTEL_DIALOGIC_BIN}/regvox -m -b $BOARD
        RC=$?
        if [ $RC -ne 0 ] ; then
        	log "Failed to start regvox."
		return 1
        fi
    fi

    # Invoke iptconf
    if [ -x ${INTEL_DIALOGIC_BIN}/iptconf ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/iptconf -b$BOARD > ${INTEL_DIALOGIC_DIR}/log/iptconf.log"
	${INTEL_DIALOGIC_BIN}/iptconf -b$BOARD > ${INTEL_DIALOGIC_DIR}/log/iptconf.log 2>&1
	RC=$?
	if [ $RC -ne 0 ] 
	then 
		log "Failed to start iptconf."
		echo "Failed to start iptconf."
		exit 1
	fi
    fi

    # Invoke confslot
    if [ -x ${INTEL_DIALOGIC_BIN}/confslot ] 
    then
    	log "${INTEL_DIALOGIC_BIN}/confslot -b$BOARD > ${INTEL_DIALOGIC_DIR}/log/confslot.log"
	${INTEL_DIALOGIC_BIN}/confslot -b$BOARD > ${INTEL_DIALOGIC_DIR}/log/confslot.log 2>&1
	RC=$?
	if [ $RC -ne 0 ] 
	then 
		log "Failed to start confslot."
		echo "Failed to start confslot."
		exit 1
	fi
    fi
    
    # Invoke dlgsysmonitorcmdline
   # if [ -x ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline ] 
   # then
	#${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --auid=$AUID --start
   # fi

    # Return good, confslot, et.al. don't have valid returns
    return 0

}

######################################################################
# DMR4 specific stop all part
######################################################################
dmr4stop_all() {

    hrs_stop
    if [ "${DIALOGIC_PLATFORM}" = "HMP" ] && [ "${DIALOGIC_RELNUM}" = "3.1" ] ||
	[ "${DIALOGIC_RELNUM}" = "4.1" ] && [ "${DIALOGIC_PLATFORM}" = "HMP" ] || 
    	[ "${DIALOGIC_PLATFORM}" = "ZVL" ]
    then
	    # Stop fault detection
	    if [ -x ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline ]
	    then
	       log "Calling ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --stop"
	       ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --stop
	       RC=$?
	       if [ $RC -ne 0 ] 
	       then 
	       		log "Failed to stop fault detection."
	       fi
	    fi
    fi

    if [ -x ${INTEL_DIALOGIC_BIN}/regvox ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/regvox -r"
	${INTEL_DIALOGIC_BIN}/regvox -r
	RC=$?
	
	if [ $RC -ne 0 ] 
	then 
		log "Failed to stop regvox."
	fi	
    fi
   
    # get all logical Ids
    alllogidlist=`${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DM3' | awk '{ print $6; }'`

    if [ -x ${INTEL_DIALOGIC_BIN}/shutboard ]
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DOWNLOADED|STARTED|CONFIGED' | awk '{ print $6; }'"
        logidlist=`${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DOWNLOADED|STARTED|CONFIGED' | awk '{ print $6; }'`
        for logid in $logidlist
        do
	if [ $logid -eq 1 ]
	then
        #    board=`${INTEL_DIALOGIC_BIN}/devmapget -a $auid -k "LOGICAL ID" -S`
        #    reason=`${INTEL_DIALOGIC_BIN}/devmapget -a $auid -k "BOARD_CONDITION" -L`
	    PID=`pidof ssp_x86Linux_boot`
	   
            log "Calling ${INTEL_DIALOGIC_BIN}/shutboard -f -b $logid -r 0"
            ${INTEL_DIALOGIC_BIN}/shutboard -f -b $logid -r 0
            RC=$?
            if [ $RC -ne 0 ] 
            then 
            	log "Failed to shutboard."
            fi
	fi
        done 
    fi
	
    sleep 10

    COREFILE="/usr/dialogic/data/core.${PID}"
     if [ -f $COREFILE ]
        then
#               echo "core file ${COREFILE} deleted"
                rm -f ${COREFILE} 2>/dev/null
     fi
 


    if [ ${DIALOGIC_PLATFORM} = HMP ] && [ ${DIALOGIC_RELNUM} = 3.1 ] ||
	[ ${DIALOGIC_RELNUM} = 4.1 ] && [ ${DIALOGIC_PLATFORM} = HMP ] ||
    	[ ${DIALOGIC_PLATFORM} = ZVL ]
    then
	if [ -x "${INTEL_DIALOGIC_BIN}/hmpd" ]
	then
	    log "Calling ${INTEL_DIALOGIC_BIN}/hmpd reset"
	    ${INTEL_DIALOGIC_BIN}/hmpd reset

            RC=$?
            if [ $RC -ne 0 ] 
            then 
            	log "Failed to reset hmpd."
            fi
	fi
    fi
	    
    if [ -x ${INTEL_DIALOGIC_BIN}/shutboard ]
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DOWNLOADED|STARTED|CONFIGED' | awk '{ print $6; }'"
        logidlist=`${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DOWNLOADED|STARTED|CONFIGED' | awk '{ print $6; }'`
        for logid in $logidlist
        do
	if [ $logid -ne 1 ]
	then
        #    board=`${INTEL_DIALOGIC_BIN}/devmapget -a $auid -k "LOGICAL ID" -S`
        #    reason=`${INTEL_DIALOGIC_BIN}/devmapget -a $auid -k "BOARD_CONDITION" -L`
	    sleep 10
            log "Calling ${INTEL_DIALOGIC_BIN}/shutboard -f -b $logid -r 0"
            ${INTEL_DIALOGIC_BIN}/shutboard -f -b $logid -r 0
            RC=$?
            if [ $RC -ne 0 ] 
            then 
            	log "Failed to shutboard."
            fi
	fi
        done 
    fi
    
    if [ -x ${INTEL_DIALOGIC_BIN}/dm3kload ]
    then
        log "Calling ${INTEL_DIALOGIC_BIN}/dm3kload -f ${INTEL_DIALOGIC_FWL}/hmp2_pdk.bin -g 1"
            ${INTEL_DIALOGIC_BIN}/dm3kload -f ${INTEL_DIALOGIC_FWL}/hmp2_pdk.bin -g 1 &>/dev/null
        RC=$?
        if [ $RC -ne 0 ]
        then
            log "Failed to call dm3kload to reload boot kernel."
        fi
    fi 

	# Release reserved timeslots
 	if [ -x ${INTEL_DIALOGIC_BIN}/clusterpkg ]
 	then
 		for logid in $alllogidlist
 		do
			if [ $logid = "<un>" ]
			then
				continue;
			fi
			
			if [ $logid -gt 0 ]
			then
				log "Calling ${INTEL_DIALOGIC_BIN}/clusterpkg -u -b$logid"
 				${INTEL_DIALOGIC_BIN}/clusterpkg -u -b$logid
				RC=$?
				if [ $RC -ne 0 ]
				then
					log "Failed to release reserved timeslots."
				fi
			fi
		done
	fi 
}

######################################################################
# DMR4 specific stop single part
######################################################################
dmr4stop_board() {

    BOARD=$1
    AUID=$2

    # Stop fault detection
    if [ -x ${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline ] 
    then
    	log "${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --auid=$AUID --stop"
	${INTEL_DIALOGIC_BIN}/dlgsysmonitorcmdline --fault --auid=$AUID --stop
	RC=$?
        if [ $RC -ne 0 ] 
        then 
        	log "Failed to reset stop fault detection."
        fi
    fi

    # Shut it down
    if [ -x ${INTEL_DIALOGIC_BIN}/shutboard ] 
    then
    	log "${INTEL_DIALOGIC_BIN}/shutboard -f -b $BOARD"
	${INTEL_DIALOGIC_BIN}/shutboard -f -b $BOARD
	RC=$?
        if [ $RC -ne 0 ] 
        then 
        	log "Failed to shutboard."
        fi
    fi

    # Release reserved timeslots
    if [ -x ${INTEL_DIALOGIC_BIN}/clusterpkg ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/clusterpkg -u -b$BOARD"
	${INTEL_DIALOGIC_BIN}/clusterpkg -u -b$BOARD
	RC=$?
        if [ $RC -ne 0 ] 
        then 
        	log "Failed to release reserved timeslots."
        fi
    fi

    # Remove device mapper settings
    if [ -x ${INTEL_DIALOGIC_BIN}/regvox ] 
    then
    	log "Calling ${INTEL_DIALOGIC_BIN}/regvox -r -b $BOARD"
	${INTEL_DIALOGIC_BIN}/regvox -r -b $BOARD
	RC=$?
        if [ $RC -ne 0 ] 
        then 
        	log "Failed to remove device mapper settings."
        fi
 fi

}

######################################################################
# POST routine
######################################################################
dm3_post() {
   
    ARG=$1
    DRESET=$2

    # Get from listboards since CHASSIS_SLOT isn't populated yet
    log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 --iAUID=$ARG --oPHYS"
    PHYS=`${INTEL_DIALOGIC_BIN}/listboards -l2 --iAUID=$ARG --oPHYS 2>/dev/null`
    RC=$?
    if [ $RC -ne 0 ] ; then
    	log "AUID not recognized."
	echo  "AUID not recognized."
	EXIT_CODE=1;return
    fi

    log "Calling ${INTEL_DIALOGIC_BIN}/devmapget -a $ARG -k "PCI_BUS_NUMBER" -L"
    BUS=`${INTEL_DIALOGIC_BIN}/devmapget -a $ARG -k "PCI_BUS_NUMBER" -L`
    RC=$?
    if [ $RC -ne 0 ] ; then
    	log "AUID not recognized."
        echo  "AUID not recognized."
	EXIT_CODE=1;return
    fi

    # See if device mapper is running
    if [ -x ${INTEL_DIALOGIC_BIN}/dm3post ]
    then
       if [ $DRESET -eq 1 ]
       then
          log "Calling ${INTEL_DIALOGIC_BIN}/dm3post -b$BUS -s$PHYS -r -l"
          ${INTEL_DIALOGIC_BIN}/dm3post -b$BUS -s$PHYS -r -l <<EOF
y
EOF
       else
          log "Calling ${INTEL_DIALOGIC_BIN}/dm3post -b$BUS -s$PHYS -l"
          ${INTEL_DIALOGIC_BIN}/dm3post -b$BUS -s$PHYS -l <<EOF
y
EOF
       fi

       EXIT_CODE=$?

    else
 	    log "Dialogic(r) DM-family board(s) POST utility not found. Verify installation."
 	    echo  "Dialogic(r) DM-family board(s) POST utility not found. Verify installation."
	    EXIT_CODE=1
    fi

    return $EXIT_CODE
}

######################################################################
# Special code to detect non-downloaded DM3 board and stick info in 
# device mapper
######################################################################
dm3_detect_not_downloaded() {

    log "${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | awk '{ printf "AUID%d=\"%s\";BUS%d=%d;SLOT%d=%d;PSLOT%d=%d;SERIAL%d=\"%s\";",NR,$1,NR,$3,NR,$4,NR,$5,NR,$7; }'"
    INFO=`${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | awk '{ printf "AUID%d=\"%s\";BUS%d=%d;SLOT%d=%d;PSLOT%d=%d;SERIAL%d=\"%s\";",NR,$1,NR,$3,NR,$4,NR,$5,NR,$7; }'`

    # Evaluate shell statement we created
    eval $INFO

    # Now BUS1 through BUS$COUNT is set

    # Get number of DM3 boards in system
    log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | wc -l"
    COUNT=`${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | wc -l`

    # Loop through boards and add boards not found in device mapper
    I=0
    while [ $I -lt $COUNT ] ; do
	I=$(( I+1 ))
	Y="\$AUID$I";  AUID=`eval echo $Y`
	if [ $AUID != "<un>" ]
	then
	    continue;
	fi
    
	# If AUID is <un> Create an entry in devmap then we create a node
	# and add info
	#
	Y="\$BUS$I";  BUS=`eval echo $Y`
	Y="\$SLOT$I"; SLOT=`eval echo $Y`    
       	Y="\$PSLOT$I"; PSLOT=`eval echo $Y`    
	Y="\$SERIAL$I"; SERIAL=`eval echo $Y`    

	# echo "AUID=$AUID BUS=$BUS SLOT=$SLOT PSLOT=$PSLOT SERIAL=$SERIAL"

   # With the board in a disabled state (not downloaded) still place 
   # the required information into device mapper's data store via
	# the devmapset binary executable. 
	AUID=`${INTEL_DIALOGIC_BIN}/devmapset -c -t200 -n DM3 | cut -f2 -d:`
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "PCI_BUS_NUMBER" -L $BUS
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "PCI_SLOT_NUMBER" -L $SLOT
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "CHASSIS_SLOT" -L $PSLOT
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "SERIAL_NUMBER" -s $SERIAL
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "BOARD_PRESENT" -L 1
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "BOARD_CONDITION" -L 1
	${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "CTPLATFORM" -s DM3
    done
}

######################################################################
# Special code to detect non-downloaded DM3 board and stick info in 
# device mapper
######################################################################
dm3_remove_from_devmap()
{
    INFO=`${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | awk '{ printf "AUID%d=\"%s\";BUS%d=%d;SLOT%d=%d;PSLOT%d=%d;SERIAL%d=\"%s\";",NR,$1,NR,$3,NR,$4,NR,$5,NR,$7; }'`
 
    # Evaluate shell statement we created
    eval $INFO
 
    # Now BUS1 through BUS$COUNT is set
 
    # Get number of DM3 boards in system
    log "Calling ${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | wc -l"
    COUNT=`${INTEL_DIALOGIC_BIN}/listboards -l2 -q 2>/dev/null | grep DM3  | wc -l`
                 
    # Loop through boards and add boards not found in device mapper
    I=0          
    while [ $I -lt $COUNT ] ; do
        I=$(( I+1 ))
	Y="\$AUID$I";  AUID=`eval echo $Y`
        if [ $AUID == "<un>" ]
        then
            continue;
        fi

 
        Y="\$BUS$I";  BUS=`eval echo $Y`
        Y="\$SLOT$I"; SLOT=`eval echo $Y`
        Y="\$PSLOT$I"; PSLOT=`eval echo $Y`
        Y="\$SERIAL$I"; SERIAL=`eval echo $Y`
 
        echo "BUS=$BUS SLOT=$SLOT PSLOT=$PSLOT SERIAL=$SERIAL"
	# Remove device from devmap
	${INTEL_DIALOGIC_BIN}/devmapset -r -a$AUID
    done
}
###################################################################
# 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
        log "$MSG"
        log "Calling ${dl_start_daemon} ${PROG} ${ARGS}"
        ${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 ] && pidof ${PROGNAME} > /var/run/${PROGNAME}.pid 2> /dev/null
            log "${MSG} success"
            RC=${SUCCESS}
        else
            # May not have failed... so, let's make sure
            if [ ${DIST} = "RedHat" ] || [ ${DIST} = "Turbolinux" ]
            then
                PROG_PID=`pidof ${PROGNAME}`
                if [ -n "$PROG_PID" ]; then
                    [ ${CREATE_PID} -eq 1 ] && pidof ${PROGNAME} > /var/run/${PROGNAME}.pid 2> /dev/null
                    log "${MSG} success"
                    RC=${SUCCESS}
                    return ${RC}
                fi
            fi
            # some failure occurred
            log "${3} failed ($RC)."
            log "${MSG} failed"
            RC=${ERROR_GENERIC}
        fi
    else
        log "${PROG} not found:"
        log "${MSG} failed"
        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 "
	
    SIG=
    [ $# -eq 3 ] && SIG=$3

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

######################################################################
#  get DSI path
######################################################################
getGCSS7Conf() {
    if [ -f ${INTEL_DIALOGIC_CFG}/gcss7.cfg ] && [ -x ${INTEL_DIALOGIC_BIN}/dlgcs7d ]
    then
        #check if GCSS7 was configured to operate: System.Configuration = "Card"
        SS7_CONFIG=`awk -F '=' '/^System\.Configuration/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | tr -d '\r' | expand - | sed -n -e 's/ *"\([a-zA-Z]*\)" */\1/p'`
        #SS7_CONFIG=`awk -F '=' '/^System\.Configuration/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | sed -n -e 's/ *"\([a-zA-Z]*\)" *$/\1/p'`
		if [ ! "${SS7_CONFIG}" = "" ] && [ ! "${SS7_CONFIG}" = "None" ]
        then
            log "SS7_CONFIG: $SS7_CONFIG"
            # get DSI path
            # parse line: Service.GCTLOAD_Path = "/opt/DSI"
            # GCTLOAD_PATH=`awk -F '=' '/^Service.GCTLOAD_Path/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | sed -e 's/ *"\(.*\)"/\1/' | sed 's/\r$//'`
            GCTLOAD_PATH=`awk -F '=' '/^Service\.GCTLOAD_Path/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | tr -d '\r' | expand - | sed -n -e 's/ *"\(.*\)"/\1/p'`
            if [ ! "${GCTLOAD_PATH}" = "" ]; then 
                export GCTLOAD_PATH
            else
                log "GCTLoad path is not defined in the gcss7.cfg file"
            fi
        fi
    fi	
    return 0	
}

######################################################################
#  check if SS7/Gemini combined board exists
######################################################################
checkSS7GeminiCombinedBoard() {
    hasSS7GeminiCombined=0
    if [ -f ${INTEL_DIALOGIC_CFG}/gcss7.cfg ] && [ -x ${INTEL_DIALOGIC_BIN}/dlgcs7d ]
    then
        #check if GCSS7 was configured to operate for physical board: System.Configuration = "Card"
        if [ "${SS7_CONFIG}" = "Card" ]
        then
            # get "SeptelCard.ConfigDir" from gcss7.cfg
            # ex, SeptelCard.ConfigDir = "/opt/DSI"
            ConfigDir=`awk -F '=' '/^SeptelCard\.ConfigDir/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | tr -d '\r' | expand - | sed -n -e 's/ *"\(.*\)"/\1/p'`
            if [ ! "${ConfigDir}" = "" ]
            then 
                # check if config.txt file defines DNI board as 
                # ex, SS7_BOARD 0 DNI2410 0x0000 /usr/dialogic/data/hmp2_mixed.bin MTP2
                boardList=`awk  '/^SS7_BOARD/ {print $2":"$3}' ${ConfigDir}/config.txt`
                for boardType in $boardList
                do
	                if [[ `echo "${boardType}" | tr -d '\r' | expand - | sed -n -e 's/[0-9]*:\(DNI\)\([0-9a-zA-Z]*\) */\1/p'` = "DNI" ]]; then
                        hasSS7GeminiCombined=1
                        log "Found ${boardType} board in SS7 protocol config file"
                    fi
                done
            else
                log "SeptelCard.ConfigDir is not defined in the config.txt file"
            fi
        fi
    fi 
    return 0
}

######################################################################
#  GC/SS7 specific start & stop functions
#  Start GCSS7 daemon (phase 1 - run): the real start of the daemon
######################################################################
ss7_run() {
    RC=0
    GCSS7_STARTED="NO"
    #check if the gcss7.cfg and GCSS7 daemon files are present (installed)
    if [ -f ${INTEL_DIALOGIC_CFG}/gcss7.cfg ] && [ -x ${INTEL_DIALOGIC_BIN}/dlgcs7d ]
    then
        #check if GCSS7 was configured to operate
        if [ ! "${SS7_CONFIG}" = "" ] && [ ! "${SS7_CONFIG}" = "None" ]
        then
            # if we are here it means one of "Card", "SIU", "UserPart", etc
            # are configured, whether with SS7 Combined or not
            GCT_CONFIG=`awk -F '=' '/^Service\.GCTLOAD_Control/ {print $2}' ${INTEL_DIALOGIC_CFG}/gcss7.cfg | tr -d '\r' | expand - | sed -n -e 's/ *"\([a-zA-Z]*\)" */\1/p'`
            if [ "${GCT_CONFIG}" = "Yes" ] || [ "${GCT_CONFIG}" = "yes" ]
            then
                # we "overload" the GCTLOAD_Control as signal to start the daemon
                # otherwise we assume manual control
                # if SS7 combined we do special processing
                startSS7Srv=1 
                if [ ${hasSS7GeminiCombined} -eq 1 ]
                then
                        # don't start ss7 unless at least one SS7 combined board starts
                        startSS7Srv=0 
                        # check if SS7 enabled combined board started
                        modelList=`${INTEL_DIALOGIC_BIN}/listboards -l2 --extrainfo 2>/dev/null | egrep 'DOWNLOADED' | awk '{ print $8; }'`
                        for modelId in $modelList
                        do
                        if [ $modelId = 0x1D2C ] || [ $modelId = 0x1D2D ] ||
                                [ $modelId = 0x1D2E ] || [ $modelId = 0x1D2F ]; then
                                startSS7Srv=1
                        fi
                        done
                fi
            fi  
            if  [ ${startSS7Srv} -eq 1 ]; then
                PROG="${INTEL_DIALOGIC_BIN}/dlgcs7d"
                MSG="Starting GCSS7 Server"
                ARGS="run silent"
                log "$MSG"
                dlg_daemon_start "${PROG}" "${ARGS}" "${MSG}"
                RC=$?
                if [ $RC -eq 0 ]; then
                    PID=`cat $DLGCS7D_PID 2>/dev/null`
                    /bin/ps -p $PID >/dev/null 2>&1
                    if [ $? -eq 0 ] ; then
                        GCSS7_STARTED="YES"
                    fi
                fi
            fi
        fi
    fi
    return $RC
}

##########################################################################
# Start GCSS7 daemon (phase 2 - continue): sending a signal to the daemon
##########################################################################
ss7_continue() {
    RC=0
    if [ ${GCSS7_STARTED} = "YES" ]; then
        ${INTEL_DIALOGIC_BIN}/dlgcs7d continue
        RC=$?
    fi
    return $RC
}

##########################################################################
# Stop GCSS7 daemon: ${INTEL_DIALOGIC_BIN}/dlgcs7d stop
# GCSS7 requires SIGINT (2) to stop gracefully
# LSB does not accept symbolic signal names
##########################################################################
ss7_stop() {
    RC=0
        #check if GCSS7 was configured to operate
        if [ ! "${SS7_CONFIG}" = "" ] && [ ! "${SS7_CONFIG}" = "None" ]; then
            PROG="${INTEL_DIALOGIC_BIN}/dlgcs7d"
            PID=`ps -eaf | grep "dlgcs7srv" | grep -v grep | cut -c 10-15`
            if [ ! "$PID" = "" ]; then
                log "dlgcs7d is running, need to stop it"
                MSG="Stopping GCSS7 Daemon :"
                SGN="-2"
                dlg_daemon_stop "${PROG}" "${MSG}" "${SGN}"
                RC=$?
            else
                log "dlgcs7d is not running"
            fi
        fi
    return $RC
}

######################################################################
# Beginning of the script
######################################################################

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

if [ -z "${INTEL_DIALOGIC_DIR}" ]
then
    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

    # Get our environment variables
    . ${OURVARS}
fi

 
FUNCTIONS=/lib/lsb/init-functions
if [ ! -f ${FUNCTIONS} ]
then
    echo "$0: FATAL ERROR: Unable to find ${FUNCTIONS}."
    echo "$0: FATAL ERROR: Execution aborted due to invalid OS software installation."
    exit 5		# NOTINST_ERROR: Program not installed
fi

# Make sure we have functions
. ${FUNCTIONS}
set_lsb_interface
# ipy91959 - no longer add any dialogic log location entry in /etc/syslog.conf file
# get syslog facility
#FACILITY=`grep ${INTEL_DIALOGIC_DIR}/log /etc/syslog.conf 2>/dev/null | cut -f1 -d"."`

# set initlog args for use by /etc/functions this ensures that
# all our output will be logged to our file (see /etc/syslog.conf)
# instead of the system default
# export INITLOG_ARGS="${INITLOG_ARGS} --facility=${FACILITY}"

USAGE="Usage: `basename $0` {startall | stopall | stopauid | removeauid | post | startauid } - This script should not be called directly."
RETVAL=$SUCCESS

AUID=$2
RARG=$3
hasSS7GeminiCombined=0

# assume not installed until proven otherwise
HMP_DAEMON=0

# check for installed components
# get SS7 config
getGCSS7Conf

case $1 in
    'startall')
        echo "Starting Dialogic(r) DM-family board(s)."
        checkSS7GeminiCombinedBoard
        # for Opal board, start ss7 1st.
        if [ ${DIALOGIC_PLATFORM} = HMP ] && [ ${DIALOGIC_RELNUM} = 4.1 ] 
        then
            if [ ${hasSS7GeminiCombined} -eq 0 ]; then
                log "No SS7 Combined board"
                ss7_run
                ss7_continue
            fi
        fi
        # cheetah_startall must be executed before
        # executing any ODIlite dependant executable..
        #$log testing syslog function
        cheetah_startall
        dm3_startall
        dmr4_startall
        msml_start
        # call start ss7
        if [ ${DIALOGIC_PLATFORM} = HMP ] && [ ${DIALOGIC_RELNUM} = 4.1 ] 
        then
            if [ ${hasSS7GeminiCombined} -eq 1 ]; then
                # for SS7/Gemini combined board, start ss7 after board is downloaded
                ss7_run
                ss7_continue
            fi
        fi
        # dm3_detect_not_downloaded  cr25240 - Causing confslot failure, no longer needed.
        ;;

    'stopall')
        echo "Stopping Dialogic(r) DM-family board(s)."
        msml_stop
        dmr4stop_all

        # Stop the HMP script
        #if [ -x "${INTEL_DIALOGIC_BIN}/hmpd" ]
        #then
        #	"${INTEL_DIALOGIC_BIN}/hmpd" stop
        #fi

        remove_cheetah_temp_files

        if [ -f "${INTEL_DIALOGIC_DIR}/temp/DM3Download" ]
        then
            rm -f "${INTEL_DIALOGIC_DIR}/temp/DM3Download" 2>/dev/null
        else
            rm -f /tmp/DM3Download 2>/dev/null
        fi
		
        if [ ${DIALOGIC_PLATFORM} = HMP ] && [ ${DIALOGIC_RELNUM} = 4.1 ] 
        then
            ss7_stop
        fi
        ;;

    'stopauid')
        BRD=`${INTEL_DIALOGIC_BIN}/devmapget -a $AUID -k "LOGICAL ID" -S`
        RC=$?
        if [ $RC -ne 0 ] ; then
            echo  "AUID not recognized."
            exit $GENERIC_ERROR 
        fi

        dmr4stop_board $BRD $AUID

        # Stop the HMP script
        if [ -x "${INTEL_DIALOGIC_BIN}/hmpd" ]
        then
            "${INTEL_DIALOGIC_BIN}/hmpd" stop
        fi

        # Set condition to "not good" BOARD_PRESENT is ignored by some binaries
        ${INTEL_DIALOGIC_BIN}/devmapset -m -a $AUID -k "BOARD_CONDITION" -L 1
        ;;

    'removeauid')
        # Get physical slot from PBL
        PHYS=`${INTEL_DIALOGIC_BIN}/listboards -l2 --iAUID=$AUID --oPHYS 2>/dev/null`
        RC=$?
        if [ $RC -ne 0 ] ; then
            echo  "AUID not recognized."
            exit $GENERIC_ERROR 
        fi

        if [ -x /sbin/hsctl ] ; then
            /sbin/hsctl extract $PHYS
        fi
        ;;
    'post')
        dm3_post $AUID $RARG
        exit $EXIT_CODE
        ;;
    'startauid')
        # Get physical slot from Listboards
        PHYS=`${INTEL_DIALOGIC_BIN}/listboards -l2 --iAUID=$AUID --oPHYS 2>/dev/null`
        RC=$?

        if [ $RC -ne 0 ] ; then
            echo  "AUID not recognized."
            exit $GENERIC_ERROR 
        fi

        # From Physical number we can get the logical board which is what
        # the DM3 device need
        BRD=`${INTEL_DIALOGIC_BIN}/scdget -tDM3 -p$PHYS -klogical`
        RC=$?

        if [ $RC -ne 0 ] ; then
            echo  "Board at slot $PHYS not configured yet. Check SCD file"
            exit $NOTCONF_ERROR 
        fi

        # DM3 phase
        dm3_startboard $BRD
        RC=$?

        if [ $RC -ne 0 ] ; then
            echo "Dialogic(r) DM-family board(s) download failure."
            exit $NOTCONF_ERROR
        fi

        # R4 layer
        dmr4_startboard $BRD $AUID
        RC=$?

        if [ $RC -ne 0 ] ; then
            echo 
            echo "Dialogic(r) DM-family board(s) R4 layer init failure."
            exit $NOTCONF_ERROR 
        fi

        # Cheetah layer
        cheetah_startboard $AUID
        RC=$?

        if [ $RC -ne 0 ] ; then
            echo "Cheetah layer init failure"
            exit $NOTCONF_ERROR 
        fi
        ;;

    *)
        echo ${USAGE}
        echo "ARGS: $*"
        RETVAL=$ARGUMEN_ERROR 
		;;
esac

exit $RETVAL 
