####################################################################
# (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
# @(#) Product Name                :  HP Serviceguard
# @(#) Product Version             :  A.12.10.00
# @(#) Patch Name                  :  
#
# *** Note: This file MUST NOT be edited. *****
#
# Any changes made to it will be overwritten when you upgrade to the
# next release of Serviceguard.
#
# Changing this file may lead to unrecoverable package startup
# and/or shutdown problems.
#
###################################################################
#
# Source in Serviceguard environment variables
#
. ${SGCONFFILE:=/etc/cmcluster.conf}

#
# PATH for HPUX and LINUX
#
PATH=${SGSBIN}:/usr/bin:/usr/sbin:/etc:/bin:/sbin
export PATH

#########################################
# Source Site Controller utility functions.
##########################################

SC_UTILS=$SGCONF/scripts/sg/sc_util.sh

if [[ -f ${SC_UTILS} ]]; then
    . ${SC_UTILS}
    if (( $? != 0 ))
    then
        echo "ERROR: Unable to source Site Controller utility functions file: ${SC_UTILS}"
        exit 1
    fi
else
    echo "ERROR: Unable to find Site Controller utility functions file: ${SC_UTILS}"
    exit 1
fi

# Global Variables 
postfix=$(date +"%H.%M.%S")
SG_env_file=$sgsadta_tmp_dir/envtemp_${postfix}_$$.txt
typeset tempfile=$sgsadta_tmp_dir/temp_${postfix}_$$.txt
typeset local_node_name=`hostname | cut -d "." -f 1`


#############################################################################		
# This function logs a message to stdout if log level is 0, ignore otherwise.		
#		
# Arguments:  $1 : log level		
#             $* : message		
#		
#############################################################################
function sg_log		
{		
    typeset log_level=$1		
    if [ $log_level = 0 ];then		
        echo "$2" >> $tempfile		
    fi		
}

#############################################################################
# This function checks if the user is root user to run the command
#
# Arguments: None
#
#############################################################################
function check_user
{
 typeset user
 user=`whoami`
 if [[ "$user" != "root" ]]; then
    echo "ERROR: Non-root users are not authorized to run this command"
    exit 1
 fi
}

###########################################################################
# This function reset the local site for the specified Site Controller package
#
# Arguments:  $1 : Site Controller package name
#
###########################################################################

function resetsite
{
    typeset sc=$1
    typeset status
    
    #check if site controler package is down or not
    status=`echo "$VCLVIEW" | awk -F "[:|=]" '(($1 == "package") && \
            ($2 == "'$sc'") && ($3 == "status")) {print $4}'`
    if [[ "$status" != "down" ]]
    then
        echo "$sc package should be halted before running this command"
        rm -f $SG_env_file 2>&1 > /dev/null
        rm -f $tempfile 2>&1 > /dev/null
        exit 1
    fi

    if [[ -z $local_site ]]
    then
        echo "Failed to determine the local site"
        rm -f $SG_env_file 2>&1 > /dev/null
        rm -f $tempfile 2>&1 > /dev/null
        exit 1
    fi

    echo "Resetting local site $local_site ..."

    sc_deactivate_site $sc $local_site ${PASSIVE_STATE} 2>&1 > /dev/null

    if [[ $? -eq 1 ]]
    then
        echo "Error: Failed to reset site $local_site"
        rm -f $SG_env_file 2>&1 > /dev/null
        rm -f $tempfile 2>&1 > /dev/null
        exit 1
    fi

    echo "Resetting $local_site is successful"
    rm -f $SG_env_file 2>&1 > /dev/null
    rm -f $tempfile 2>&1 > /dev/null
    exit 0       
}

################
# main routine
################

#
# This script must be specified with site controller package name as a parameter
#


typeset sc_pkg

check_user

if (($# == 1))
then
    sc_pkg=$1
    $cmviewcl -v -f line -s config -p $sc_pkg | grep -i -q '^module_name:sg/sc|module_name=sg/sc$'
    
    if [[ $? -ne 0 ]]
    then
        echo "Error: $sc_pkg is not a site controller package"
        echo "Usage: cmresetsc <site_controller_package_name>"
        echo "Description: This script is for resetting a site"
        echo "             controller package's site safety latch"
        echo "             on nodes in the current site."
        exit 1
    fi
    sys_log "Executing 'cmresetsc $sc_pkg'"

    echo "Site Controller Contributed Script for resetting a Site"    

    ${SGSBIN}/cmgetpkgenv $sc_pkg > $SG_env_file
    if (( $? != 0 ))
    then
        cat $SG_env_file | grep -e "package $sc is not configured" \
                                -e "$sc has no environment variables defined" >/dev/null
        if (( $? != 0 ))
        then
            echo "ERROR: Function resetsite"
            echo "       Unable to retrieve package attributes."
            rm -f $SG_env_file 2>&1 > /dev/null
            exit 1
        fi
    fi
    export SG_env_file
    . ${SG_env_file}

    # Initialize cache
    init_cache $sc_pkg
    setup_local_and_remote_site
    resetsite $sc_pkg

else
    echo "Usage: cmresetsc <site_controller_package_name>"
    echo "Description: This script is for resetting a site"
    echo "             controller package's site safety latch"
    echo "             on nodes in the current site." 
    exit 1
fi
