#!/bin/bash

#       Copyright (c) Marathon Technologies Corporation, 2008.
#       All Rights Reserved. Unpublished rights reserved
#       under the copyright laws of the United States.
# 
#       The software contained on this media is proprietary
#       to and embodies the confidential technology of
#       Marathon Technologies Corporation. Possession, use,
#       duplication or dissemination of the software and
#       media is authorized only pursuant to a valid written
#       license from Marathon Technologies Corporation.
# 
#       RESTRICTED RIGHTS LEGEND Use, duplication, or
#       disclosure by the U.S. Government is subject to
#       restrictions as set forth in Subparagraph (c)(1)(ii)
#       of DFARS 252.227-7013, or in FAR 52.227-19, as
#       applicable.

. /opt/xensource/xha/ha_rc

#
#   ha_set_pool_state    init | active | invalid [-C <config-file-name>]
#

#   Get new state
newstate=`param $*` || exit $?
shift

# get config-file name
cf=`getcf $*` || exit $?

status=$MTC_EXIT_SUCCESS

case $newstate in
    init)
        calldaemon pid > /dev/null
        status=$?
        case $status in
            $MTC_EXIT_DAEMON_IS_NOT_PRESENT)
                writestatefile setinit $cf
                status=$?
                ;;
            *)
                error "Could not set the pool state to INIT because the HA daemon may be present" "" $MTC_EXIT_DAEMON_IS_PRESENT
            ;;
        esac
        ;;
    active)
        calldaemon set_pool_state active
        status=$?

        if [ $status -ne $MTC_EXIT_SUCCESS ]; then
                writestatefile setactive $cf
                status=$?
        fi
        ;;
    invalid)
        calldaemon set_pool_state invalid
        status=$?

        if [ $status -ne $MTC_EXIT_SUCCESS ]; then
                writestatefile setinvalid $cf
                status=$?
        fi
        ;;
    *)
        error "Invalid new state $newstate" "" $MTC_EXIT_INVALID_PARAMETER
        ;;
esac

exit $status
