#!/bin/sh
PROGROOT=/opt/McAfee/agent
macmnsvcuser=mfe
user=`id | cut -d'=' -f2 | cut -d\( -f1`
echo_success() {
    TEXT_MSG="[ OK ]"
	if [ "$BOOTUP" = "color" ]
	then
		echo "${MOVE_TO_COL} ${SETCOLOR_SUCCESS} ${TEXT_MSG} ${SETCOLOR_NORMAL}"
	else
		echo "$TEXT_MSG" 
	fi
	return 1
}

echo_failure() {
    TEXT_MSG="[ FAILED ]"
	if [ "$BOOTUP" = "color" ]
	then
		echo "${MOVE_TO_COL} ${SETCOLOR_FAILURE} ${TEXT_MSG} ${SETCOLOR_NORMAL}"
	else
		echo "$TEXT_MSG" 
	fi
	return 1
}

macmnsvc_status() {
	if [ "$macmnsvcuser" = "mfe" ]
	then
	if [ $user = 0 ]
	then	
		isRunning=`ps aux | grep $LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc|grep -v grep|wc -l`
		if [ $isRunning -eq 0 ]
		then
			sudo -u $macmnsvcuser /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc status" > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc status" $macmnsvcuser > /dev/null 2>&1
			fi
		count=`ps -ef | grep ${PROGROOT}/bin/macmnsvc | grep -v grep |wc -l `
		if [ $count -eq 0 ]
		then
			su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc status" > /dev/null 2>&1
		fi
		fi
	else
		su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc status" $macmnsvcuser > /dev/null 2>&1
	fi
   	else
	    LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk ${PROGROOT}/bin/macmnsvc status > /dev/null 2>&1
	fi
	    count=`ps -ef | grep ${PROGROOT}/bin/macmnsvc | grep -v grep |wc -l `
		if [ $count -ge 1 ]
        then
                echo "Trellix common services is already running."
                return 0
        else
                echo "Trellix common services is not running."
                return 1
        fi
}

macmnsvc_stop() {
	macmnsvc_status
	RETVAL=$?
	if [ $RETVAL -eq 0 ]
	then
		echo "Stopping Trellix common services..."
		if [ "$macmnsvcuser" = "mfe" ]
		then
		if [ $user = 0 ]
		then
				sudo -u $macmnsvcuser /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk  ${PROGROOT}/bin/macmnsvc stop" > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc stop" $macmnsvcuser > /dev/null 2>&1
				fi
				count=`ps -ef | grep ${PROGROOT}/bin/macmnsvc | grep -v grep |wc -l `
				if [ $count -eq 1 ]
				then
					su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc stop" > /dev/null 2>&1
				fi
		else
			su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk  ${PROGROOT}/bin/macmnsvc stop" $macmnsvcuser > /dev/null 2>&1
		fi
		else
			LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk  ${PROGROOT}/bin/macmnsvc stop > /dev/null 2>&1
		fi
		count=`ps -ef | grep ${PROGROOT}/macmnsvc | grep -v grep |wc -l `
		if [ $count -eq 0 ]
		then
			echo_success
		else
			echo_failure
			return 1
		fi
	fi
	echo ""
	return 0
}

macmnsvc_start()
{
	echo "Starting Trellix common services... "
	if [ "$macmnsvcuser" = "mfe" ]
	then	
	if [ $user = 0 ]
	then
			sudo -u $macmnsvcuser /bin/sh -c  "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH http_proxy=$http_proxy HTTP_PROXY=$HTTP_PROXY https_proxy=$https_proxy HTTPS_PROXY=$HTTPS_PROXY all_proxy=$all_proxy ALL_PROXY=$ALL_PROXY no_proxy=\"$no_proxy\" NO_PROXY=\"$NO_PROXY\" ${PROGROOT}/bin/macmnsvc start" > /dev/null 2>&1
			if [ $? -ne 0 ]
			then
				su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH http_proxy=$http_proxy HTTP_PROXY=$HTTP_PROXY https_proxy=$https_proxy HTTPS_PROXY=$HTTPS_PROXY all_proxy=$all_proxy ALL_PROXY=$ALL_PROXY no_proxy=\"$no_proxy\" NO_PROXY=\"$NO_PROXY\" ${PROGROOT}/bin/macmnsvc start" $macmnsvcuser > /dev/null 2>&1
			fi
			count=`ps -ef | grep ${PROGROOT}/bin/macmnsvc | grep -v grep |wc -l `
			if [ $count -eq 0 ]
			then
				su -s /bin/sh -c "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH ${PROGROOT}/bin/macmnsvc start" > /dev/null 2>&1
			fi
	else
		su -s /bin/sh -c  "LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk:$LD_LIBRARY_PATH http_proxy=$http_proxy HTTP_PROXY=$HTTP_PROXY https_proxy=$https_proxy HTTPS_PROXY=$HTTPS_PROXY all_proxy=$all_proxy ALL_PROXY=$ALL_PROXY no_proxy=\"$no_proxy\" NO_PROXY=\"$NO_PROXY\" ${PROGROOT}/bin/macmnsvc start" $macmnsvcuser > /dev/null 2>&1
	fi
	else
		LD_LIBRARY_PATH=/opt/McAfee/agent/lib/lib64:/opt/McAfee/agent/lib/lib64/tools:/opt/McAfee/agent/lib/lib64/rsdk:/opt/McAfee/agent/lib:/opt/McAfee/agent/lib/tools:/opt/McAfee/agent/lib/rsdk ${PROGROOT}/bin/macmnsvc start > /dev/null 2>&1
	fi
	count=`ps -ef | grep ${PROGROOT}/bin/macmnsvc | grep -v grep |wc -l `
	if [ $count -ge 1 ]
    	then
   		echo_success
   	else
       		echo_failure
    	fi
	
	sleep 2 # Waiting 2 secs for common services
}

case "$1" in
  start_cmn)
        macmnsvc_start
        ;;
  stop_cmn)
        macmnsvc_stop
        ;;
  *)

        exit 4
esac

exit $?
