#!/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_stop_daemon
#

daemon_pid=`calldaemon pid`
status=$?

if [ $status -eq $MTC_EXIT_SUCCESS ]; then
    # send SIGTERM to the daemon
    kill -15 $daemon_pid
    while true; do
        daemon_pid=`calldaemon pid`
        status=$?
        if [ $status -ne $MTC_EXIT_SUCCESS ]; then

            # one more attempt to mask any
            # transient error codes

            sleep 1
            daemon_pid=`calldaemon pid`
            status=$?
            
            break
        fi
        sleep 1
    done
fi

if [ $status -eq $MTC_EXIT_DAEMON_IS_NOT_PRESENT ]; then
    status=$MTC_EXIT_SUCCESS
fi

exit $status
