#!/bin/bash

# Server Websocket
if [[ "$1" != "restartifdown" ]]; then
    echo WEBSOCKET = STOP
    pid="$(pgrep -a -f gos:websocket | grep 5556)"
    if [ -n "${pid}" ]; then
        IFS=' ' read -r -a array <<< "$pid"
        kill -9 ${array[0]};
    fi
fi

# Port LISTEN ?
pid="$(pgrep -a -f gos:websocket | grep 5556)"
restart="yes"
if [ "$1" == "restartifdown" ] && [ -n "${pid}" ] ; then
    restart="no"
fi

if [ "$restart" == "yes" ] && [ "$1" != "stop" ] ; then
    echo WEBSOCKET = START
    cd /var/www/html/ninegate
    systemctl restart wssninegate
fi