#!/bin/bash
#
# Wrapper for vcbVmName 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
{
    local result

    if [ -n "$VCHOST" ] ; then
	arg=$arg" -h \"\$VCHOST\""
    fi
    if [ -n "$USERNAME" ] ; then
	arg=$arg" -u \"\$USERNAME\""
    fi
    if [ -n "$VMNAMECACHE" ] ; then
	arg=$arg" -c \"\$VMNAMECACHE\""
    fi

    # We don't want the password to show up in the process listing, so pass
    # it in through the environment. If none was specified, make sure
    # we still prompt for one.
    if have_password ; then
	eval LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$hostd_libs" \
	    VCB_PASSWORD='"$PASSWORD"' \
	    "$vcb_libs/vcbVmName" $arg "$UNPARSED_ARGUMENTS"
	result=$?
    else
	eval LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$hostd_libs" \
	    "$vcb_libs/vcbVmName" $arg "$UNPARSED_ARGUMENTS"
	result=$?
    fi

    return $result
}


intercept_arguments "h:u:p:c:" "$@"
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
if [ -n "${INTERCEPT_c}" ] ; then 
    VMNAMECACHE="${INTERCEPT_c}"
fi
run_command
