#!/bin/bash
#
# Wrapper for vcbUtil backup tool
#
# (C) 2005-2007 VMware, Inc. All rights reserved.

hostd_libs=/usr/lib/vmware/hostd
vcb_libs=/usr/lib/vmware/vcb

cfg=${VCB_CONFIG_FILE:-"/etc/vmware/backuptools.conf"}

. "$vcb_libs/plugins/lib"

if [ -e "$cfg" ] ; then
    . "$cfg"
fi

export HOST_THUMBPRINT

#
# Description:
# Runs vcbCommand.
#
# Arguments:
# None. Uses global variables set by argument interception.
#
# Result:
# 0 on success != 0 on failure.
#
function run_command
{
    if [ -n "$VCHOST" ] ; then
	arg=$arg" -h \"\$VCHOST\""
    fi
    if [ -n "$USERNAME" ] ; then
	arg=$arg" -u \"\$USERNAME\""
    fi

    if have_password ; then
	eval LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$hostd_libs" \
 	     VCB_PASSWORD='"$PASSWORD"' \
	    "$vcb_libs/vcbUtil" $arg "$UNPARSED_ARGUMENTS"
	result=$?
    else
	eval LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$hostd_libs" \
	    "$vcb_libs/vcbUtil" $arg "$UNPARSED_ARGUMENTS"
	result=$?
    fi

    return $result
}


intercept_arguments "h:u:p:" "$@"
if [ -n "${INTERCEPT_h}" ] ; then 
    VCHOST="${INTERCEPT_h}"
fi
if [ -n "${INTERCEPT_u}" ] ; then 
    USERNAME="${INTERCEPT_u}"
fi
if [ -z "${INTERCEPT_p}" ] ; then
    if [ "${INTERCEPT_p+EMPTY}" = "EMPTY" ] ; then
        PASSWORD="${INTERCEPT_p}"
    fi
else
    PASSWORD="${INTERCEPT_p}"
fi
run_command
