####################################################################
# (C) Copyright 2015 Hewlett Packard Enterprise Development LP.
# @(#) Serviceguard UPCC Package Configuration Script.
# @(#) 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 HP Serviceguard.
#
# Changing this file may lead to unrecoverable package startup
# and/or shutdown problems.
#
###################################################################

####################################################################
## Source the VMFS utility functions:
####################################################################
. /etc/cmcluster.conf
VMFS_COMMON_UTIL=${SGCONF}/scripts/sg/vmfs_common_util.sh
if [[ -f ${VMFS_COMMON_UTIL} ]]; then
    . ${VMFS_COMMON_UTIL}
    if (( $? != 0 ))
    then
        echo "ERROR: Unable to source package vmfs utility functions file: ${VMFS_COMMON_UTIL}"
        exit 1
    fi
else
    echo "ERROR: Unable to find package vmfs utility functions file: ${VMFS_COMMON_UTIL}"
    exit 1
fi

usage()
{
  cat >&2 <<EOM
  Usage:
    cmvmvalidate -h
    cmvmvalidate -H <host> -n uuid>
    |------------------------------------------------------------------------------------|
    | -H <host>  | The IP/DNS of vCenter or ESX host. Only one host at a time            |
    |------------------------------------------------------------------------------------|
    | -n <uuid>  | The UUID of the node from which it is called. Only one UUID at a time |
    |------------------------------------------------------------------------------------|
EOM
    exit 1
}

print_usage_if_help_or_no_arg()
{
  if [ $# -eq 0 ]; then
    usage
  fi
  if [ $# -eq 1 ] && \
     [ "$1" = "-h" ]; then
    usage
  fi
  return 0
}

print_usage_if_help_or_no_arg $*

typeset -i type_h_option=0
typeset -i type_n_option=0

typeset host=""
typeset uuid=""

while [[ $# -ge 1 ]]
do
    key="$1"
    case $key in
    -H)
        host="$2"
        if [[ "X$host" == "X" ]]; then
            echo "Host is missing"
            usage
        fi
        shift
        (( type_h_option = type_h_option + 1 ))
    ;;
    -n)
        uuid="$2"
        if [[ "X$uuid" == "X" ]]; then
            echo "UUID is missing"
            usage
        fi
        shift
        (( type_n_option = type_n_option + 1 ))
    ;;
    *)
        echo "Unknown Option :: $key"
        usage
        exit 1
    ;;
    esac
    shift
done

if (( $type_h_option == 0 )) ||  (( $type_n_option == 0 )) || \
    [ $type_h_option -gt 1 ] || [ $type_n_option -gt 1 ]; then
    usage
    exit 1
fi

# MAIN

typeset -i ret=0
typeset -i ver=0
prefix=$(date +"%H_%M_%S_%N")
str_prefix="Log_"$$_
log_prefix=$str_prefix$prefix
typeset vmfs_tmp_dir="/var/tmp"
typeset error_output_file=$vmfs_tmp_dir/$log_prefix"_error_output_file.$$"

. /etc/cmcluster.conf
JAVA=`command -v java`
JAVA_TOOL=$SGSBIN/vmwutil
JAVA_VERSION="$JAVA -d64 -version"
OP="validate"

ver=`$JAVA_VERSION 2>&1 | head -1 | awk '{print $NF}'| cut -d '"' -f 2 | cut -d '_' -f 1  | sed 's/\.//g'`
if [ $? == 0 ]; then
    if [ $ver -ge 170 ]; then
        output=`$JAVA -jar $JAVA_TOOL -o $OP -H $host -n $uuid 2>$error_output_file`
        ret=$?
        if [ -s $error_output_file ]; then
            $LOGGER -t $script_name[$$] -f $error_output_file
        fi
        # The file will be created whether the file size is zero or not
        # Let us delete it.
        rm -f $error_output_file
    else
        ret=10
    fi
else
    ret=10
fi    

###########################################################
# The return value will be one of the below               #
# 0: The login was successful and UUID is part of the host#
# 3: UUID is not part of the host                         #
# 6: The credentials are wrong                            #
#10: Unsupported java version                             #
###########################################################
exit $ret
