#!/bin/bash
#
# Wrapper for vcb backup tools
#

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

    # 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/vcbSnapshot" $arg "$UNPARSED_ARGUMENTS"
	result=$?
    else
	eval LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$hostd_libs" \
	    "$vcb_libs/vcbSnapshot" $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 [ "${INTERCEPT_p+EXISTS}" = "EXISTS" ] ; then
    PASSWORD="${INTERCEPT_p}"
fi
run_command
