####################################################################
# (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

usage()
{
  cat >&2 <<EOM
  Usage:
    cmvmotion -h
    cmvmotion -n <UUID> -L
    cmvmotion -n <UUID> -T <target host> 
    |------------------------------------------------------------------------------------------------------|
    | -L                         | List the available hosts for vmotion                                    |
    |------------------------------------------------------------------------------------------------------|
    | -n <UUID>                  | UUID of the VM to be moved.                                             |
    |------------------------------------------------------------------------------------------------------|
    | -T <target host>           | target host where vm will be migrated to.                               |
    |------------------------------------------------------------------------------------------------------|
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="vmotion"
typeset input_file=""
typeset -i type_n_option=0
typeset -i type_l_option=0
typeset -i type_t_option=0

while [[ $# -ge 1 ]]
do
    key="$1"
    case $key in
    -n)
        type_n_option=1
        src_vm_uuid="$2"
        if [[ "X$src_vm_uuid" == "X" ]]; then
            echo "UUID is missing for source VM."
            usage
        fi
        shift
    ;;

    -L)
        type_l_option=1
    ;;

    -T)
        type_t_option=1
        tgthost="$2"
        if [[ "X$tgthost" == "X" ]]; then
            echo "Target host is missing."
            usage
        fi
        shift
    ;;

    *)
        echo "Unknown Option :: $key"
        usage
    ;;
    esac
    shift
done

if (( $type_n_option == 0 )) ; then
    usage
fi

if (( $type_l_option == 1 )) && (( $type_t_option == 1 )); then
    echo "-L and -T options are mutually exclusive."
    usage
fi


#MAIN
typeset script_name="cmvmotion"

prefix=$(date +"%H_%M_%S_%N")
str_prefix="Log_"$$_
log_prefix=$str_prefix$prefix
typeset error_str=""
typeset tmp_dir="/var/tmp"
typeset error_output_file=$tmp_dir/$log_prefix"_error_output_file.$$"
typeset output_file=$tmp_dir/$log_prefix"_output_file.$$"

# Proceed only if vcenter is configured, else exit
typeset cmviewcl_output=$vmotion_tmp_dir/$log_prefix"_cmviewcl.$$"
cache_cmviewcl_output_lx

check_if_cluster_is_configured_with_esx_or_vcenter
cluster_type=$?
if (( $cluster_type != $type_vcenter )); then
    echo "msgtype:ERROR|message=vCenter is not configured in the cluster. Configure vCenter to migrate to another host."
    cleanup_before_exit $NO_VCENTER_CONFIGURED
else
    vcenter_name=`get_vcenter_name`
fi

JAVA=`command -v java`
JAVA_TOOL=$SGSBIN/vmwutil

if (( $type_l_option == 1 )); then
     `$JAVA -jar $JAVA_TOOL -o $OP -H $vcenter_name -n $src_vm_uuid -L 1>$output_file 2>$error_output_file`
else
     `$JAVA -jar $JAVA_TOOL -o $OP -H $vcenter_name -n $src_vm_uuid -T $tgthost 1>$output_file 2>$error_output_file`
fi
ret=$?

if ((( $ret == 0 )) && (( $type_l_option == 1 ))); then
    cat $output_file
fi

if (( $ret != 0 )); then
    if (( $ret == $TGT_HOST_NOT_ELIGIBLE )); then
        error_str=`cat $output_file | grep "error=" | awk -F= '/^|error=/  {print $2}'`
        echo "$error_str" | grep -q "<>"
        if (( $? == 0 )); then
            old_IFS=$IFS
            IFS=$'\n'
            for line in $(echo "$error_str" | tr "<>" "\n" | sed 's/^_*//g' | sed 's/_*$//g')
            do
                echo "msgtype:ERROR|message=$line"
            done
            IFS=$old_IFS
        else
            echo "msgtype:ERROR|message=$error_str"
        fi
    else
        cat $output_file
    fi
fi

if [ -s $error_output_file ]; then
    $LOGGER -t $script_name[$$] -f $error_output_file
fi

cleanup_before_exit $ret
