#!/bin/bash

#       Copyright (c) Stratus Technologies Bermuda Ltd., 2008.
#       All Rights Reserved. Unpublished rights reserved
#       under the copyright laws of the United States.
# 
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU Lesser General Public License as published
#       by the Free Software Foundation; version 2.1 only. with the special
#       exception on linking described in file LICENSE.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU Lesser General Public License for more details.

. /usr/libexec/xapi/cluster-stack/xhad/ha_rc

#
#   ha_start_daemon [-C <config-file-name>] [initial-fist-points]
#

#   get config-file name

cf=$configfile
if [ $# -gt 0 ]; then
    c=`param $*`
    if [ $c = "-C" -o $c = "-c" ]; then
        shift
        cf=`param $*` || error "No config-file name following -C" "" $MTC_EXIT_INVALID_PARAMETER
        shift
    fi
fi

#   change current directory to a known place
#   (mainly to avoid a core file left in an arbitrary location.

cd $daemon_wd || exit $?

# see if the daemon is already present

calldaemon gethoststate > /dev/null 2>&1
retval=$?

if [ $retval -ne $MTC_EXIT_DAEMON_IS_NOT_PRESENT ]; then
    exit $MTC_EXIT_DAEMON_IS_PRESENT
fi

#   flush watchdog slots
cleanupwatchdog --force

#   start xha daemon

portrelease xhad 2&>1 > /dev/null || true
xhad $cf $* < /dev/null > /dev/null 2>&1 &
daemon_pid=$!

#   wait for boot completion

nattempts=0
while true
do
    sleep 1

    # get host state

    host_state=`calldaemon gethoststate`
    retval=$?
    
    #
    # We handle the following cases.
    #
    # $retval == $MTC_EXIT_SUCCESS && $host_state==Online
    #       The local host has beome a member of the liveset. We return success.
    # $retval == $MTC_EXIT_SUCCESS && $host_state==Starting
    #       The local host is still attempt to become a member of the liveset.
    #       We wait for a while and see the host state again.
    # $retval == $MTC_EXIT_DAEMON_IS_NOT_PRESENT && $host_state==any && $daemon_pid is valid
    #       The daemon is still not ready to accept script service for "calldaemon gethoststate".
    #       We leave the decision to the next iteration of the main loop.
    # $retval == $MTC_EXIT_DAEMON_IS_NOT_PRESENT && $host_state==any && $daemon_pid is invalid
    #       The daemon is gone. We return the exit code of the daemon.
    # $retval == other errors && $host_state==any
    #       The calldaemon encountered an error. We tolerate 3 occurences of this.
    #       After 3 times, we return the exit code of the calldaemon.

    if [ $retval -eq $MTC_EXIT_SUCCESS ]; then
        case $host_state in
        Online)
            exit $MTC_EXIT_SUCCESS  # The local host has become a member of the liveset
            ;;
        Starting)
            nattempts=0
            ;;
        *)
            # This should not happen
            error "ha_start_daemon: calldaemon returned success, but with an illegal host-state " $host_state $MTC_EXIT_INTERNAL_BUG
            ;;
        esac
    elif [ $retval -eq $MTC_EXIT_DAEMON_IS_NOT_PRESENT ]; then
        # if the daemon process is still there, it may still be booting.
        stat /proc/$daemon_pid > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            wait $daemon_pid
            retval=$?
            cleanupwatchdog
            if [ $retval -eq $MTC_EXIT_IMPROPER_LICENSE ]; then
                error "ha_start_daemon: the HA daemon is not licensed on this host" "" $retval
            fi
            error "ha_start_daemon: the HA daemon stopped without forming a liveset" "" $retval
        fi
    else
        nattempts=`expr $nattempts + 1`
        if [ $nattempts -ge 3 ]; then
            error "ha_start_daemon: internal error in communication with the HA daemon" "" $retval
        fi
    fi
done
