#!/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
    ONE_PID=/var/run/one/oned.pid
    ONE_CONF=/etc/one/oned.conf
    ONE_DB=/var/lib/one/one.db
    ONE_LOG=/var/log/one/oned.log
    ONE_SCHED_LOG=/var/log/one/sched.log
    ONE_HEM_LOG=/var/log/one/onehem.log
    ONE_XMLRPC_LOG=/var/log/one/one_xmlrpc.log
    ONE_MONITOR_LOG=/var/log/one/monitor.log

    ONED=/usr/bin/oned
    ONE_HEM=/usr/bin/onehem-server

    LOCK_FILE=/var/lock/one/one
else
    ONE_PID=$ONE_LOCATION/var/oned.pid
    ONE_CONF=$ONE_LOCATION/etc/oned.conf
    ONE_DB=$ONE_LOCATION/var/one.db
    ONE_LOG=$ONE_LOCATION/var/oned.log
    ONE_SCHED_LOG=$ONE_LOCATION/var/sched.log
    ONE_HEM_LOG=$ONE_LOCATION/var/onehem.log
    ONE_XMLRPC_LOG=$ONE_LOCATION/var/one_xmlrpc.log
    ONE_MONITOR_LOG=$ONE_LOCATION/var/monitor.log

    ONED=$ONE_LOCATION/bin/oned
    ONE_HEM=$ONE_LOCATION/bin/onehem-server

    LOCK_FILE=$ONE_LOCATION/var/lock/.lock
fi

KILL_9_SECONDS=10
BACKUP="true"

#------------------------------------------------------------------------------
# Function that checks for running daemons
#------------------------------------------------------------------------------
setup()
{
    ONE_PID_DIR=`dirname $ONE_PID`

    mkdir -p $ONE_PID_DIR

    if [ ! -w $ONE_PID_DIR ]; then
        echo "$ONE_PID_DIR is not writable, cannot start oned."
        exit 1
    fi

    if [ -f $LOCK_FILE ]; then
        if [ -f  $ONE_PID ]; then
            ONEPID=`cat $ONE_PID`
            ps $ONEPID > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                echo "ONE is still running (PID:$ONEPID). Please try 'one stop' first."
                exit 1
            fi
        fi

        echo "Stale .lock detected. Erasing it."
        rm $LOCK_FILE
    fi
}

#------------------------------------------------------------------------------
# Function that stops the daemons
#------------------------------------------------------------------------------
stop()
{
    stop_oned

    stop_hem
}

stop_oned()
{
    if [ -f $ONE_PID ]; then
        PID=$(cat $ONE_PID)
        kill $PID > /dev/null 2>&1

        counter=0
        while ps $PID > /dev/null 2>&1; do
            let counter=counter+1
            if [ $counter -gt $KILL_9_SECONDS ]; then
                kill -9 $PID > /dev/null 2>&1
                break
            fi
            sleep 1
        done

        rm -f $ONE_PID > /dev/null 2>&1
    fi
}

stop_hem()
{
    onehem-server stop > /dev/null 2>&1
}

#------------------------------------------------------------------------------
# Function that starts the daemons
#------------------------------------------------------------------------------
start()
{
    if [ ! -x "$ONED" ]; then
        echo "Can not find $ONED."
        exit 1
    fi

    if [ ! -x "$ONE_HEM" ]; then
        echo "Can not find $ONE_HEM."
        exit 1
    fi

    if [ ! -f "$ONE_DB" ]; then
        if [ ! -f "$HOME/.one/one_auth" ]; then
            if [ -z "$ONE_AUTH" ]; then
                echo "You should have ONE_AUTH set the first time you start"
                echo "OpenNebula as it is used to set the credentials for"
                echo "the administrator user."
                exit 1
            fi
        fi
    fi

    # Start the one daemon
    start_oned

    # Start hook execution manager server
    start_hem

    # Wait for the daemons to warm up
    sleep 3

    STARTED="true"

    ps `cat $ONE_PID` > /dev/null 2>&1

    if [ $? -ne 0 ]; then
        echo "oned failed to start"
        STARTED="false"
    fi

    if [ "$STARTED" == "false" ]; then
        stop
        exit -1
    fi
}

start_oned()
{
    if [ "$BACKUP" = "true" ];then
        [ -f "$ONE_LOG" ] && mv $ONE_LOG{,.$(date '+%Y%m%d%H%M%S')}
        [ -f "$ONE_XMLRPC_LOG" ] && mv $ONE_XMLRPC_LOG{,.$(date '+%Y%m%d%H%M%S')}
        [ -f "$ONE_MONITOR_LOG" ] && mv $ONE_MONITOR_LOG{,.$(date '+%Y%m%d%H%M%S')}
    fi
    $ONED -f 2>&1 &

    LASTRC=$?
    LASTPID=$!

    if [ $LASTRC -ne 0 ]; then
        echo "Error executing $ONED"
        exit 1
    else
        echo $LASTPID > $ONE_PID
    fi
}

start_hem()
{
    if [ "$BACKUP" = "true" ];then
        [ -f "$ONE_HEM_LOG" ] && mv $ONE_HEM_LOG{,.$(date '+%Y%m%d%H%M%S')}
    fi

    HEM_ERROR=$(mktemp /tmp/hem-error.XXXXXX)

    onehem-server start > $HEM_ERROR 2>&1

    LASTRC=$?

    if [ $LASTRC -ne 0 ]; then
        echo "Error starting onehem-server: $(cat $HEM_ERROR)"
        rm -f $HEM_ERROR
        exit 1
    fi

    rm -f $HEM_ERROR
}

#------------------------------------------------------------------------------
#------------------------------------------------------------------------------

if [ "$1" = "-f" ]; then
    BACKUP="false"
    shift
fi

case "$1" in
    start)
        setup
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        setup
        start
        ;;
    *)
        echo "Usage: one [-f] {start|stop|restart}" >&2
        echo "Options:" >&2
        echo "  -f  Do not backup log files." >&2
        exit 3
        ;;
esac
