####################################################################
# (C) Copyright 2015-2016 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 utility functions.
############################
. /etc/cmcluster.conf
SG_UTILS=${SGCONF}/scripts/mscripts/utils.sh
if [[ -f ${SG_UTILS} ]]; then
    . ${SG_UTILS}
    if (( $? != 0 ))
    then
        echo "ERROR: Unable to source package utility functions file: ${SG_UTILS}"
        exit 1
    fi
else
    echo "ERROR: Unable to find package utility functions file: ${SG_UTILS}"
    exit 1
fi

####################################################################
## Source the VMFS utility functions:
####################################################################
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

if (( type_n_option > 0 )) && (( type_f_option > 0 )); then
    echo "The command options -f and -n are mutually exclusive"
    usage
fi

usage()
{
  cat >&2 <<EOM
  Usage:
    cmvmdiskop -h
    cmvmdiskop -A -F <input_file> -c <cmviewcl_output_file> -r
    cmvmdiskop -A -F <input_file> -c <cmviewcl_output_file> -x
    cmvmdiskop -D -F <input_file> -c <cmviewcl_output_file> -r
    |------------------------------------------------------------------------------------------------------|
    | -A                         | attach                                                                  |
    |------------------------------------------------------------------------------------------------------|
    | -D                         | detach                                                                  |
    |------------------------------------------------------------------------------------------------------|
    | -F <input_file>            | -f and -n option are mutually exclusive                                 |
    |                            | Input file can have the following                                       |
    |                            | UUID: UUID of the cluster node, if already with -n option skip it here  |
    |                            | VMDK: The VMFS vmdk file(with dir/file.vmdk)                            |
    |                            | DS:   The name of the datastore in which the VMFS vdmk file exists      |
    |                            | SLOT: The slot on which the VMFS vmdk file will be attached             |
    |------------------------------------------------------------------------------------------------------|
    | -c <cmviewcl_output_file>  | Pass the "cmviewcl_output" variable from the vmfs_module                |
    |------------------------------------------------------------------------------------------------------|
    | -r                         | This option will be used with metrocluster during attach of a disk      |
    |                            | With this option the utility will rescan the datastore on the host.     |
    |------------------------------------------------------------------------------------------------------|
    | -x                         | This option will be used with XDC during attach of a disk.              |
    |                            | With this option the utility will look for valid datastore before       |
    |                            | issuing the attach of the disks.                                        |
    |------------------------------------------------------------------------------------------------------|
    Example Input file 
    UUID:564DCA9A-EECF-F398-C588-D55B153DBC0F
    VMDK:cluster/kf4.vmdk
    DS:kf1_kf3shared10GB2
    SLOT:1:1
    VMDK:cluster/kf4_1.vmdk
    DS:kf1_kf3shared10GB2
    SLOT:1:2
    VMDK:cluster/kf4_2.vmdk
    DS:kf1_kf3shared10GB2
    SLOT:1:3
    VMDK:cluster/nissan4.vmdk
    DS:kf1_kf3shared10GB3
    SLOT:2:1
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 OP=""
typeset input_file=""
typeset -i type_o_option=0
typeset -i type_f_option=0
typeset -i type_c_option=0
typeset -i type_r_option=0
typeset -i type_x_option=0

while [[ $# -ge 1 ]]
do
    key="$1"
    case $key in
    -F)
        type_f_option=1
        input_file="$2"
        if [[ "X$input_file" == "X" ]]; then
            echo "The Input file is missing"
            usage
        fi
        if [ ! -f $input_file ]; then
            echo "The Input file $input_file does not exist"
            usage
        fi
        shift
    ;;
    -A)
        type_o_option=1
        OP="attach"
        if [[ "X$OP" == "X" ]]; then
            echo "Operation is missing"
            usage
        fi
    ;;
    -D)
        type_o_option=1
        OP="detach"
        if [[ "X$OP" == "X" ]]; then
            echo "Operation is missing"
            usage
        fi
    ;;
    -c)
        type_c_option=1
        cmviewcl_output="$2"
        if [[ "X$cmviewcl_output" == "X" ]]; then
            echo "The cmviewcl output file is missing"
            usage
        fi
        if [ ! -f $cmviewcl_output ]; then
            echo "The cmviewcl output file $cmviewcl_output does not exist"
            usage
        fi
        shift
    ;;
    -r)
        type_r_option=1
    ;;
    -x)
        type_x_option=1
    ;;
    *)
        echo "Unknown Option :: $key"
        usage
        exit 1
    ;;
    esac
    shift
done

if (( $type_c_option == 0 )) ||  (( $type_o_option == 0 )) || (( $type_f_option == 0 )) ; then
    usage
    exit 1
fi

function read_uuid_from_file
{
    UUID=`cat $input_file | grep "UUID" | awk -F: '/UUID:/ {print $2}'`
    echo "$UUID"
}

#MAIN
typeset -i type_esx=1
typeset -i type_vcenter=2
typeset script_name="vmfs.sh-cmvmdiskop"

typeset output=""
typeset vmfs_tmp_dir="/var/tmp"
typeset error_output_file=$vmfs_tmp_dir/$log_prefix"_error_output_file.$$"

check_if_cluster_is_configured_with_esx_or_vcenter
cluster_type=$?
_uuid=`read_uuid_from_file`

if (( $cluster_type == $type_esx )); then
    _uuid_host=`get_esx_host_name_from_uuid $_uuid`
    if [[ "X$_uuid_host" == "X" ]]; then
        echo "msgtype:ERROR|message=Missing UUID"
        exit 1
    fi
else
    _uuid_host=`get_vcenter_name`
fi

. /etc/cmcluster.conf
JAVA=`command -v java`
JAVA_TOOL=$SGSBIN/vmwutil
cmd_opts="-o"
if (( $type_r_option == 1 )); then
    cmd_opts="-r -o"
elif (( $type_x_option == 1 )); then
    cmd_opts="-x -o"
fi

output=`$JAVA -jar $JAVA_TOOL $cmd_opts $OP -H $_uuid_host -F $input_file 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

if (( $ret != 0 )); then
    echo "$output"
fi
exit $ret
