#!/bin/sh
#
# Startup script for iNodeAuthService
#
# chkconfig: - 80 01
# description: AuthenMngService
# processname: AuthenMngService
# config: /etc/iNode/iNode.conf

# Get fake config
INODE_CFG="/etc/iNode/inodesys.conf"

#[ -r "$INODE_CFG" ] && . "${INODE_CFG}"
if [ -r "$INODE_CFG" ];then
    LINE=`cat $INODE_CFG`
    INSTALL_DIR=${LINE##*INSTALL_DIR=}
fi

# if INSTALL_DIR is not set, exit
if [ -z "$INSTALL_DIR" ]; then
    echo INSTALL_DIR not set yet
    exit 1
fi

RETVAL=0

ubuntu_terminate_process()
{
	ubuntu_os=`grep -e "^.*Ubuntu.*$" /etc/issue`
	case "$ubuntu_os" in 
		"Ubuntu 19"* )
			sudo pkill -SIGKILL -f $1 >  /dev/null 2>&1
		;;
		*)
		    sudo killall -9 $1 > /dev/null 2>&1
		;;
	esac
	return 0
}

# See how we were called.
start() {
    echo -n "Starting AuthenMngService: "
        
    SELINUX_FLAG=`getenforce 2>/dev/null | grep -x -i enforcing`
    if [ "$SELINUX_FLAG" != "" ]
    then
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeUtility.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodePortalPt.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeX1Pt.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeSecurityAuth.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libCoreUtils.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libImplAv.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libOesisCore.so
: << DONTNEEDEX
        chcon -t textrel_shlib_t /usr/lib/libInodeUtility.so
        chcon -t textrel_shlib_t /usr/lib/libInodePortalPt.so
        chcon -t textrel_shlib_t /usr/lib/libInodeX1Pt.so
        chcon -t textrel_shlib_t /usr/lib/libInodeSecurityAuth.so
        chcon -t textrel_shlib_t /usr/lib/libCoreUtils.so
        chcon -t textrel_shlib_t /usr/lib/libImplAv.so
        chcon -t textrel_shlib_t /usr/lib/libOesisCore.so
DONTNEEDEX
   fi
    
    IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x AuthenMngService`
    if [ "$IfExist" != "" ]
    then
        echo "AuthenMngService already running"
    else
        sudo "$INSTALL_DIR/AuthenMngService" &
        echo OK
    fi
    
    IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x iNodeMon`
    if [ "$IfExist" = "" ]
    then
        sleep 5
        sudo "$INSTALL_DIR/iNodeMon" &
    fi
}

stop() {
    echo -n "Stopping AuthenMngService: "
    
    SELINUX_FLAG=`getenforce 2>/dev/null | grep -x -i enforcing`
    if [ "$SELINUX_FLAG" != "" ]
    then
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeUtility.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodePortalPt.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeX1Pt.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libInodeSecurityAuth.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libCoreUtils.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libImplAv.so
        chcon -t textrel_shlib_t ${INSTALL_DIR}/libs/libOesisCore.so
: << DONTNEEDEX
        chcon -t textrel_shlib_t /usr/lib/libInodeUtility.so
        chcon -t textrel_shlib_t /usr/lib/libInodePortalPt.so
        chcon -t textrel_shlib_t /usr/lib/libInodeX1Pt.so
        chcon -t textrel_shlib_t /usr/lib/libInodeSecurityAuth.so
        chcon -t textrel_shlib_t /usr/lib/libCoreUtils.so
        chcon -t textrel_shlib_t /usr/lib/libImplAv.so
        chcon -t textrel_shlib_t /usr/lib/libOesisCore.so
DONTNEEDEX
    fi
    IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x iNodeMon`
    if [ "$IfExist" != "" ]
    then
	sudo "$INSTALL_DIR/iNodeMon" -k
    
    	Sec=0
    	while [ 1 ]
    	do
            IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x iNodeMon`
	    if [ "$IfExist" != "" ]
	    then
		sleep 1
		Sec=`expr $Sec + 1`

		if [ "$Sec" -lt 10 ]
		then
		    #sudo killall -9 iNodeMon > /dev/null 2>&1
		    ubuntu_terminate_process iNodeMon 
		else
			echo "Stop iNodeMon failed"
		    break
		fi
	    else
		break
	    fi
    	done
    fi
	
    IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x AuthenMngService`
    if [ "$IfExist" != "" ]
    then
	sudo "$INSTALL_DIR/AuthenMngService" -k
    
    	Sec=0
    	while [ 1 ]
    	do
            IfExist=`ps awx -o command|awk -F/ '{print $NF}'|grep -x AuthenMngService`
	    if [ "$IfExist" != "" ]
	    then
		sleep 1
		Sec=`expr $Sec + 1`

		if [ "$Sec" -lt 10 ]
		then
		    #sudo killall -9 AuthenMngService > /dev/null 2>&1
			ubuntu_terminate_process AuthenMngService
		else
			echo "Stop AuthenMngService failed"
		    break
		fi
	    else
		break
	    fi
    	done
    	
    	echo OK
    else
        echo "AuthenMngService not running"	
    fi
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 5
        start
        ;;
  *)
        echo "Usage: iNodeAuthService {start|stop|restart}"
        exit 1
esac

exit $RETVAL
