#!/bin/ksh
#########################################################################
# The interface script determines the ip address of the interface used to
# talk to the given remote host. If remote host cannot be contacted, it
# will exit with an exit value of 1.
#########################################################################

COMMAND="$0"
TARGET="$1"

usage() {
    echo "usage: ${COMMAND} host" >&2
    exit 1;
}

if [ -z "${TARGET}" ] ; then
    usage
fi

ROUTE=`/usr/sbin/ping -s -R -v "${TARGET}" 64 1 2>/tmp/interface.$$.err`

if [ "$?" != "0" ] ; then
    echo ${ROUTE} >&2
    cat /tmp/interface.$$.err >&2
    echo "Cannot contact ${TARGET}" >&2
    exit 1
fi

if [ -e "/tmp/interface.$$.err" ] ; then
    rm /tmp/interface.$$.err
fi

LASTLINE=""
FOUND=""
for ia in ${ROUTE}
do
    if [ "${ia}" = "(End" ] ; then
        FOUND="true"
        break
    else
        LASTLINE="${ia}"
    fi
done

if [ -z "${FOUND}" ] ; then
    echo "Host ${TARGET} is too far, cannot determine route" >&2
    exit 1
else
    LASTLINE="${LASTLINE%\,}" # Strip off the trailing comma
    LASTLINE="${LASTLINE%\)}" # Strip off the trailing ')'
    echo "${LASTLINE#\(}"     # Strip off the leading '('
fi
