#!/bin/bash

#Handle websocket service
#Version 1
#author Luiz Rogerio Santos ezvoice Telecom. 2022
#

# Documentation.
# Save file with name in /usr/local/bin/ezsocket

# execute permission on the file.
# chmod a+x /usr/local/bin/ezsocket
# execute cmd bash
# execute cmd ezsocket status or ezsocket restart
# params status | restart

# Warning...
# check file way call-server.sh then is in way different!!!!
# if this situation occurs, uncomment the line and comment the other.
# restart=$(source /var/www/html/resources/websocket/src/call-server.sh)

if [ -z "$1" ];
then
    echo "I hope it is informing one of the values:
	 > status
         > restart
	 example: ezsocket restart"
elif [ $1 == "debug" ]
then
     #tail -f /var/www/html/resources/websocket/src/websocket.log
     tail -f /var/www/ezagent/resources/websocket/src/websocket.log
elif [ $1 == "status" ]
then
	status=$(ps aux |grep ezwebsocket |wc -l)
        if [ $status -gt 1 ]; then
            echo "
	    >>>>
	    status service websocket is running... ok
	    >>>>"
	else
	    echo "
	    >>>>
	    status service websocket is not running... =( !!!!
	    >>>> use cmd >>>> ezsocket restart >>> param!!!"
        fi
elif [ $1 == "restart" ]
then
	kill=$(pkill -f "ezwebsocket")
	if [ $? -eq 0 ]; then
	   echo 'Websocket droped pid' $pid
	fi
	#Start websocket
	restart=$(source /var/www/ezagent/resources/websocket/src/call-server.sh)
	#restart=$(source /var/www/html/resources/websocket/src/call-server.sh)
	echo 'Starting module websocket ...'
	sleep 1
	echo '...'
	sleep 1
	echo 'check if service is running...
	...'
	if [ $? -eq 0 ];
	then
	    status=$(ps aux |grep ezwebsocket |wc -l)
	    if [ $status -ge 1 ];
	    then
		echo 'Service websocket started successfuly!!! =)'
	    fi
        fi
else

         echo "I hope it is informing one of the values:
	 > status
         > restart
	 example: ezsocket restart"

fi

echo ">>>end<<<"
