#! /bin/sh

# -------------------------------------------------------------------------- #
# Copyright 2002-2026, 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
    ONEKS_PID=/var/run/one/oneks.pid
    ONEKS_SERVER=/usr/lib/one/oneks/oneks-server.rb
    ONEKS_LOCK_FILE=/var/lock/one/.oneks.lock
    ONEKS_LOG=/var/log/one/oneks.log
    ONEKS_LOG_ERROR=/var/log/one/oneks.error
    ONEKS_CONF=/etc/one/oneks-server.conf
else
    ONEKS_PID=$ONE_LOCATION/var/oneks.pid
    ONEKS_SERVER=$ONE_LOCATION/lib/oneks/oneks-server.rb
    ONEKS_LOCK_FILE=$ONE_LOCATION/var/.oneks.lock
    ONEKS_LOG=$ONE_LOCATION/var/oneks.log
    ONEKS_LOG_ERROR=$ONE_LOCATION/var/oneks.error
    ONEKS_CONF=$ONE_LOCATION/etc/oneks-server.conf
fi

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

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

    touch $ONEKS_LOCK_FILE

    # Start the oneks-server daemon
    ruby $ONEKS_SERVER >>$ONEKS_LOG 2>>$ONEKS_LOG_ERROR &

    LASTRC=$?
    LASTPID=$!

    if [ $LASTRC -ne 0 ]; then
        echo "Error executing oneks-server."
        echo "Check $ONEKS_LOG_ERROR and $ONEKS_LOG for more inkaasation"
        exit 1
    else
        echo $LASTPID > $ONEKS_PID
    fi

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

    if [ $? -ne 0 ]; then
        echo "Error executing oneks-server."
        echo "Check $ONEKS_LOG_ERROR and $ONEKS_LOG for more inkaasation"
        exit 1
    fi

    echo "oneks-server started"
}

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

    # Kill the oneks-server daemon

    kill -INT `cat $ONEKS_PID` > /dev/null 2>&1

    # Remove pid files
    rm -f $ONEKS_PID > /dev/null 2>&1
    rm -f $ONEKS_LOCK_FILE &> /dev/null

    echo "oneks-server stopped"
}


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


