# Scripting library for Service Console VCB Tools wrappers.
# Provides functionality for handling transport plugins and
# intercepting command line arguments.
#
# (C) 2005 VMware, Inc. All rights reserved.



#
# Description:
# Look at a specified source/destination directory specifier.
# and figure out the transport protocol to use. 
# The directory specifier is supposed to match the following
# pattern:
#
# [<transport_specifier>"://"]<path>
#
# By default, "file" is assumed if no transport specifier is found.
#
# Also, checks if the corresponding transport plugin is present.
#
# Arguments:
# $1: Directory specifier
#
# Result:
# 0 on success, != 0 on failure. Writes the transport specifier
# to stdout when successful.
#
function check_transport_plugin
{
    local path="$1"
    local prefix
    local transport
    local rv

    # If we don't find a prefix, we default to "file://"
    echo $path | grep -q '://'
    rv=$?
    if [ "$rv" != "0" ] ; then
	path="file://$path"
    fi

    prefix=`echo $path | sed s@://.*@@`

    transport="$vcb_libs/plugins/transport-$prefix"
    if [ ! -r "$transport" ] ; then
	echo "Unknown transport protocol: $prefix" >&2
	return 11
    fi
    echo $prefix
    return 0
}



#
# Description:
# Load a transport plugin.
#
# Arguments:
# $1: Transport plugin prefix as returned by check_transport_plugin
#
# Result:
# None.
#
function load_transport_plugin
{
    local prefix="$1"
    . "$vcb_libs/plugins/transport-$prefix"
}


#
# Description:
# Takes a path name conforming to
# [<transport>"://"]<path> 
# and returns the path component.
#
# Arguments:
# $1: Path
#
# Result:
# None. Prints path without prefix on stdout.
#
function remove_transport_prefix
{
    echo "${1#*://}"
}



#
# Description:
# Allow a VCB wrapper script to intercept command line arguments passed
# to a VCB binary.
#
# Arguments:
# $1: getopts conformant argument string (example: h:u:p:)
# $2 -> $n: Command line arguments to evaluage
#
# Result:
# None. Sets a bunch of global variables:
# For each intercepted command line option, a variable named
# INTERCEPT_<option> is created and set to the value assigned
# to this option.
# 
# Example: -h backuphost --> INTERCEPT_h="backuphost"
#
# Options that are not intercepted or command line arguments that
# are not options will be placed in a variable called UNPARSED_ARGUMENTS
#
function intercept_arguments
{
    local intercept="$1"
    local arg
    local match
    local varname
    local i
    declare -i i

    shift
    UNPARSED_ARGUMENTS=""
    while getopts :$intercept arg ; do
	# see if we got an argument we should intercept
	echo $intercept | grep -q "$arg"
	match=$?
	if [ "$match" = "0" ] ; then
	    # argument we want to intercept!
	    varname="INTERCEPT_$arg"
	    eval ${varname}=\$OPTARG
	else
	    # argument we don't want to intercept, append
	    # to unparsed args
	    UNPARSED_ARGUMENTS=$UNPARSED_ARGUMENTS"-$OPTARG "
	    # Peek at the next argument. If it does not start with
	    # an "-", we assume it is the option for the current argument
	    if [ $OPTIND -le $# -a "${!OPTIND:0:1}" != "-" ] ; then
		UNPARSED_ARGUMENTS=$UNPARSED_ARGUMENTS"\"${!OPTIND}\" "
		shift
	    fi
	fi
    done

    # Now all intercepted arguments are stored in variables named
    # INTERCEPT_<optionchar>. All non-intercepted arguments are stored
    # in UNPARSED_ARGUMENTS

    # We still need to deal with non-option args the command might have:
    UNPARSED_ARGUMENTS=$UNPARSED_ARGUMENTS" -- "
    i=${OPTIND}-1
    shift $i
    while [ $# -gt 0 ] ; do
        UNPARSED_ARGUMENTS=$UNPARSED_ARGUMENTS" \"$1\" "
        shift
    done
}



#
# Description:
# Determine whether the PASSWORD environment variable is set.
#
# Arguments:
# None
#
# Result:
# Returns 0 if PASSWORD is set, non-zero otherwise.
#
function have_password
{
    # Use the shell's builtin test and return result
    [ "${PASSWORD+EMPTY}" = "EMPTY" ]
}
