#! /bin/sh

. ${GLOBUS_LOCATION}/libexec/globus-script-initializer
globus_source ${libexecdir}/globus-sh-tools.sh


#  CVS Information:
#     $Source: /home/globdev/CVS/globus-packages/gram_client_tools/source/globus-job-get-output.in,v $
#     $Date: 2003/03/20 20:43:11 $
#     $Revision: 1.4 $
#     $Author: bester $

PROGRAM_NAME=`echo $0 | ${GLOBUS_SH_SED-sed} -e 's|.*/||g'`
PROGRAM_VERSION='3.6'
PACKAGE='globus_gram_client_tools'
VERSION='3.6'
DIRT_TIMESTAMP='1059418471'
DIRT_BRANCH_ID='42'

short_usage="$PROGRAM_NAME [-help] [-version] [-f N] [-tail N] [-r <resource manager>] -out|-err <jobID>"

long_usage()
{
    ${GLOBUS_SH_CAT-cat} >&2 <<EOF

Usage: ${short_usage}

    Retrieves the stdout (-out) or stderr (-err) from a job started with
    globus-job-submit and identified through the identifier <jobID>.

    If neither -out or -err is given, -out is assumed.

    A valid proxy is required for this operation to succeed.

    Options:
	-help, -usage           Display usage
	-version                Display version
	-resource         (-r)  Fetch output using the specified resource
                                manager
	-out, -err              Show stdout OR stderr of remote job
	-tail N           (-t)  Show last N lines of output
	-follow N         (-f)  Show last N lines of output and any new line

EOF
}

globus_source ${libexecdir}/globus-args-parser-header $*

if [ $# -lt 1 ]; then
    globus_args_short_usage
    exit 1
fi

outopt=
erropt=
jobid=
resource=
tailopt=no
followopt=no

while [ -n "$1" ]; do
    case "$1" in
	-out)
	    outopt=true
	    ;;
	-err)
	    erropt=true
	    ;;
	-r|-resource)
	    if [ -z "$2" ]; then
		globus_args_option_error "$1" "need a resource contact string"
	    fi
	    resource="$2"
	    shift
	    ;;
	-t|-tail|-f|-follow)
	    if [ -z "$2" ]; then
		globus_args_option_error "$1" "need an integer argument"
	    fi
	    if ! ${GLOBUS_SH_EXPR-expr} "$2" + 1 - 1 \> 0 \
		1>/dev/null 2>/dev/null
	    then
		globus_args_option_error "$1" "need an integer argument"
	    fi
	    case "$1" in
		-t|-tail)
		    tailopt="$2"
		    ;;
		-f|-follow)
		    followopt="$2"
		    ;;
	    esac
	    shift
	    ;;
	--)
	    break
	    ;;
	-*)
	    globus_args_unrecognized_option "$1"
	    ;;
	*)
	    if [ $# -gt 1 ]; then
		globus_args_option_error "$1" "jobID has to be last argument"
	    fi
	    jobid="$1"
	    ;;
    esac
    shift
done

stream=stdout
if [ -n "${erropt}" ] ; then
    stream=stderr
fi

if [ -z "${jobid}" ]; then
    echo "" >&2
    echo "ERROR: missing job ID" >&2
    globus_args_short_usage
    exit 1
fi

if [ -n "${outopt}" -a -n "${erropt}" ]; then
    echo "" >&2
    echo "ERROR: -out and -err are exclusive!" >&2
    globus_args_short_usage
    exit 1
fi

if [ "${followopt}" != no -a "${tailopt}" != no ]; then
    echo "" >&2
    echo "ERROR: -follow and -tail are exclusive!" >&2
    globus_args_short_usage
    exit 1
fi

if [ -z "${resource}" ]; then
    resource=`echo ${jobid} | ${GLOBUS_SH_AWK} -F: '{print $2}' | \
	${GLOBUS_SH_SED-sed} 's|/||g'`
fi

${bindir}/grid-proxy-info -exists
if [ $? -ne 0 ]; then
    echo "" >&2
    echo "ERROR: unable to locate a valid proxy" >&2
    globus_args_short_usage
    exit 1
fi

case ${tailopt}:${followopt} in
    no:no)
	# cat the file, exit
	command="\${GLOBUS_SH_CAT-cat}"
	;;
    *:no)
	# tail the file
	command="\${GLOBUS_SH_TAIL-tail} -${tailopt}"
	;;
    no:*)
	# follow the file
	command="\${GLOBUS_SH_TAIL-tail} -${followopt}f"
	;;
    *)
	echo "INTERNAL ERROR: tailopt:followopt = ${tailopt}:${followopt}" >&2
	;;
esac

tmpfile=${local_tmpdir}/globus_job_get_output.`${GLOBUS_SH_WHOAMI-whoami}`.tmp.$$

# redirect stderr to file so we can see if error occurs after the fact
${bindir}/globusrun -o -r "$resource" 2> $tmpfile \
    "&(executable=\$(GLOBUS_LOCATION)/bin/globus-sh-exec)(arguments=-exec \"file=\`\${bindir}/globus-gass-cache -query -t anExtraTag x-gass-cache://${jobid}${stream}\`; if test -r \"\"\$file\"\" ; then ${command} \$file ; else echo Invalid job id. 1>&2; fi\")"

status=$?

if test "$status" != 0; then
    cat $tmpfile 1>&2
    rm $tmpfile
    exit $status
fi
if test -s $tmpfile; then
    cat $tmpfile 1>&2
    rm $tmpfile
    exit 1
fi
rm $tmpfile
exit 0
