#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

if [ -z "$ONE_LOCATION" ]; then
    FIREEDGE_PID=/var/run/one/fireedge.pid
    FIREEDGE_SERVER=/usr/lib/one/fireedge/dist/index.js
    FIREEDGE_LOCK_FILE=/var/lock/one/.fireedge.lock
    FIREEDGE_LOG=/var/log/one/fireedge.log
    GUACD_PID=/var/run/one/guacd.pid
else
    FIREEDGE_PID=$ONE_LOCATION/var/fireedge.pid
    FIREEDGE_SERVER=$ONE_LOCATION/lib/fireedge/dist/index.js
    FIREEDGE_LOCK_FILE=$ONE_LOCATION/var/.fireedge.lock
    FIREEDGE_LOG=$ONE_LOCATION/var/fireedge.log
    GUACD_PID=$ONE_LOCATION/var/guacd.pid
fi

PATH=$PATH:/usr/share/one/guacd/sbin
GUACD_BIN=guacd

get_pid()
{
	if [ -f "$1" ]; then
		read PID < "$1"

		# if pidfile contains PID and PID is valid
		if [ -n "$PID" ] && ps "$PID" > /dev/null 2>&1; then
			echo "$PID"
			return 0
		fi
	fi

	# pidfile/pid not found, or process is dead
	return 1
}

setup()
{
  if [ -f $FIREEDGE_LOCK_FILE ]; then
    if [ -f  $FIREEDGE_PID ]; then
      FIREEDGEPID=`cat $FIREEDGE_PID`
      ps $FIREEDGEPID > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "FireEdge Server is still running (PID:$FIREEDGEPID)."
        echo "Please try 'fireedge-server stop' first."
        exit 1
      fi
    fi
    echo "Stale .lock detected. Erasing it."
    rm $FIREEDGE_LOCK_FILE
  fi
}

start()
{
  if [ ! -f "$FIREEDGE_SERVER" ]; then
    echo "Cannot find $FIREEDGE_SERVER."
    exit 1
  fi

  touch $FIREEDGE_LOCK_FILE

  # Start the fireedge-server daemon
  node $FIREEDGE_SERVER >>$FIREEDGE_LOG &

  LASTRC=$?
  LASTPID=$!

  if [ $LASTRC -ne 0 ]; then
    echo "Error executing fireedge-server."
    echo "Check $FIREEDGE_LOG for more information"
    exit 1
  else
    echo $LASTPID > $FIREEDGE_PID
  fi

  sleep 2
  ps $LASTPID > /dev/null 2>&1

  if [ $? -ne 0 ]; then
    echo "Error executing fireedge-server."
    echo "Check $FIREEDGE_LOG for more information"
    exit 1
  fi
}

stop()
{
  if [ ! -f $FIREEDGE_PID ]; then
    echo "Couldn't find fireedge-server process pid."
    exit 1
  fi

  # Kill the fireedge-server daemon

  kill `cat $FIREEDGE_PID` > /dev/null 2>&1

  # Remove pid files
  rm -f $FIREEDGE_PID > /dev/null 2>&1
  rm -f $FIREEDGE_LOCK_FILE &> /dev/null
}

start_guacd()
{

  if ! hash $GUACD_BIN &>/dev/null; then
		echo "Cannot find $GUACD_BIN."
		return 1
	fi

	PID=`get_pid $GUACD_PID`
	if [ $? -eq 0 ]; then
		echo "Guacamole proxy daemon already running"
		return 1
	fi

	echo -n "Starting guacd: "
	$GUACD_BIN -p "$GUACD_PID" > /dev/null 2>&1

	case "$?" in
		0)
		echo "success"
		;;
		*)
		echo "fail"
		;;
	esac
}

stop_guacd()
{
	echo -n "Stopping guacd: "

	PID=`get_pid $GUACD_PID`
	case "$?" in
		0)
		if kill -9 $PID > /dev/null 2>&1
		then
			echo "success"
			return 0
		fi

		echo "fail"
		return 0
		;;
		*)
		echo "success (not running)"
		return 0
		;;
	esac
}

case "$1" in
  start)
    start_guacd
    setup
    start
    echo "fireedge-server started"
    ;;
  stop)
    stop_guacd
    stop
    echo "fireedge-server stopped"
    ;;
  restart)
    stop_guacd 2> /dev/null
    stop 2> /dev/null
    start_guacd
    setup
    start
    echo "fireedge-server restarted"
    ;;
  *)
  echo "Usage: fireedge-server {start|stop|restart}" >&2
  exit 3
  ;;
esac
