#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2024, 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=/run/one/fireedge.pid
    FIREEDGE_SERVER=/usr/lib/one/fireedge/dist/index.js
    FIREEDGE_LOCK_FILE=/run/lock/one/.fireedge.lock
    FIREEDGE_LOG=/var/log/one/fireedge.log
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
fi

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
}


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