#!/bin/bash
# DEVELOPER: Vaibhav K

PARABLU_CONFIG_SCRIPT_PATH=../config/parablu_config.sh
source $PARABLU_CONFIG_SCRIPT_PATH
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"

function start_service ()
{


                if (( $(ps -ef | grep -v -e grep -e tail -e less -e vi -e nano | grep $service_name | wc -l) > 0 ))
                  then
                        echo "$service_name is already running!!!"
                        sleep 2


                elif (( $(ps -ef | grep -v -e grep -e tail -e less -e vi -e nano | grep $service_name | wc -l) == 0 ))
                        then
                                service $service_name start 2>&1 >> /dev/null
                                sleep 10
                                if (( $(ps -ef | grep -v -e grep -e tail -e less -e vi -e nano | grep $service_name | wc -l) > 0 ))
                                then
                                        echo "$service_name Started Successfully!!!"

                                else
                                        echo "$service_name is not running..Retrying to $action the $service_name..!!"
                                        n=0
                                        until [ $n -ge 3 ]
                                        do
                                        service $service_name start 2>&1 >> /dev/null
                                        if (( $(ps -ef | grep -v -e grep -e tail -e less -e vi -e nano | grep $service_name | wc -l) > 0 ))
                                        then
                                                echo "$service_name Started Succesfully"
                                                break
                                        else
                                                echo "$service_name Failed to $action"

                                        fi
                                        n=$[$n+1]
                                        sleep 5s
                                        done
                                        if [ $service_name == apache2 ];then
                                        killall -9 apache2
                                        service apache2 restart
                                        sleep 2s


                                        fi
                                fi
                fi

}

function stop_service ()
{

               	  service $service_name stop 2>&1 >> /dev/null
                  sleep 10
                  if (( $(ps -ef | grep -v -e grep -e tail -e less -e vi -e nano | grep $service_name | wc -l) == 0 ))
                  then
                        echo "$service_name Stopped Succesfully!!!"

                  else
                        echo "$service_name Failed to Stop.....Forcefully killing the processes of $service_name"
                        processInfo=$(ps -aux | grep $service_name | awk '{print $2}')
                        kill -9 $processInfo
                  fi
	
	


}

function process_tomcat_job ()
{
	service_name="tomcat-job1"
	if [[ "$action" == "start" ]]; then
		start_service
	elif [[ "$action" == "stop" ]]; then
		stop_service
	elif [[ "$action" == "restart" ]]; then
		stop_service
		start_service
	else
		echo "Invalid Argument passed"
	fi
		
}

function process_tomcat_pcb ()
{
	service_name="tomcat-pcb1"
	if [[ "$action" == "start" ]]; then
                start_service
        elif [[ "$action" == "stop" ]]; then
                stop_service
        elif [[ "$action" == "restart" ]]; then
                stop_service
                start_service
        else
                echo "Invalid Argument passed"
        fi
}

function process_memcache ()
{
	service_name="memcached"
	if [[ "$action" == "start" ]]; then
                start_service
        elif [[ "$action" == "stop" ]]; then
                stop_service
        elif [[ "$action" == "restart" ]]; then
                stop_service
                start_service
        else
                echo "Invalid Argument passed"
        fi
}

function process_apache ()
{
	service_name="apache2"
	if [[ "$action" == "start" ]]; then
                start_service
        elif [[ "$action" == "stop" ]]; then
                stop_service
        elif [[ "$action" == "restart" ]]; then
                stop_service
                start_service
        else
                echo "Invalid Argument passed"
        fi
}

function process_activemq ()
{
        service_name="activemq"
	if [[ "$action" == "start" ]]; then
                start_service
        elif [[ "$action" == "stop" ]]; then
                stop_service
        elif [[ "$action" == "restart" ]]; then
                stop_service
                start_service
        else
                echo "Invalid Argument passed"
        fi
}

function process_mongo ()
{
	service_name="mongod"
	if [[ "$action" == "start" ]]; then
                start_service
	elif [[ "$action" == "stop" ]]; then
                stop_service
        elif [[ "$action" == "restart" ]]; then
                stop_service
                start_service
        fi
}

function paracloud_component_check ()
{
	process_memcache
	process_apache
	process_tomcat_pcb
	process_tomcat_job


}

function blukrypt_component_check ()
{
	process_memcache
	process_apache
	process_tomcat_pcb
	process_tomcat_job
}


function mongodb_component_check ()
{
	#process_mongo
	echo "disabling mongo service restart"
}

function utility_service ()
{
	if [[ -z $action ]]; then
		echo "Script must be run with an argument like start, stop or reset "
		exit 1
	fi

	if [ "$action" != "start" ] && [ "$action" != "restart" ] && [ "$action" != "stop" ] ; then
		echo "Invalid Argument passed...!!! Arguments can only be 'start' 'stop' or 'restart'"
	exit 1
	fi

	tput clear
 	tput cup 8 20
            
       	echo "Do you really want to $action the services? (y/n)"
	read option
	if [[ $option = y ]]; then
		for a in ${SERVER_TYPE[@]};do
        	        case $a in
				0)	mongodb_component_check
					sleep 50s
					blukrypt_component_check ;;
				1)	mongodb_component_check
					sleep 50s
					paracloud_component_check ;;
				2)	mongodb_component_check
					sleep 50s
				 	blukrypt_component_check ;;
				3)	mongodb_component_check ;;
				4)	mongodb_component_check ;;
			esac
		done
	fi
}


action=$1
utility_service

