#! /bin/sh

# -------------------------------------------------------------------------- #
# 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
    HEM_PID=/var/run/one/onehem.pid
    HEM_SERVER=/usr/lib/one/onehem/onehem-server.rb
    HEM_LOCK_FILE=/var/lock/one/.onehem.lock
    HEM_LOG=/var/log/one/onehem.log
    HEM_LOG_ERROR=/var/log/one/onehem.error
    HEM_CONF=/etc/one/onehem-server.conf
else
    HEM_PID=$ONE_LOCATION/var/onehem.pid
    HEM_SERVER=$ONE_LOCATION/lib/onehem/onehem-server.rb
    HEM_LOCK_FILE=$ONE_LOCATION/var/.onehem.lock
    HEM_LOG=$ONE_LOCATION/var/onehem.log
    HEM_LOG_ERROR=$ONE_LOCATION/var/onehem.error
    HEM_CONF=$ONE_LOCATION/etc/onehem-server.conf
fi

setup()
{
  if [ -f $HEM_LOCK_FILE ]; then
    if [ -f  $HEM_PID ]; then
      HEMPID=`cat $HEM_PID`
      ps $HEMPID > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        echo "Hook execution manger (onehem) is still running (PID:$HEMPID)."
        echo "Please try 'onehem-server stop' first."
        exit 1
      fi
    fi
    echo "Stale .lock detected. Erasing it."
    rm $HEM_LOCK_FILE
  fi
}

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

  touch $HEM_LOCK_FILE

  # Start the oneflow-server daemon
  ruby $HEM_SERVER >$HEM_LOG 2>$HEM_LOG_ERROR &

  LASTRC=$?
  LASTPID=$!

  if [ $LASTRC -ne 0 ]; then
        echo "Error executing onehem-server."
        echo "Check $HEM_LOG_ERROR and $HEM_LOG for more information"
    exit 1
  else
    echo $LASTPID > $HEM_PID
  fi

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

  if [ $? -ne 0 ]; then
        echo "Error executing onehem-server."
        echo "Check $HEM_LOG_ERROR and $HEM_LOG for more information"
    exit 1
  fi

  echo "onehem-server started"
}

#
# Function that stops the daemon/service
#
stop()
{
  if [ ! -f $HEM_PID ]; then
    echo "Couldn't find onehem-server process pid."
    exit 1
  fi

  # Kill the onehem-server daemon
  kill -INT `cat $HEM_PID` > /dev/null 2>&1

  # Remove pid files
  rm -f $HEM_PID > /dev/null 2>&1
  rm -f $HEM_LOCK_FILE &> /dev/null

  echo "onehem-server stopped"
}


case "$1" in
  start)
  setup
  start
  ;;
  stop)
  stop
  ;;
  *)
  echo "Usage: onehem-server {start|stop}" >&2
  exit 3
  ;;
esac

