#! /bin/sh

# Shift all arguments by one to get command parameters
getArguments () {
    k="first" 
    args=""
    while [ "X$1" !=  "X" ] ; do
	if [ $k = "first" ]; then 
            args="$1"
	    args="${args} start"
	    k="not"
	else
            args="${args} $1"
	fi
        shift
    done
    echo $args
}

usage() { 
/bin/cat >&2 << EOF

Start a job in a specified resource manager:

Options:
	help | usage         	    Display usage
	version          	    Display command version
	jobName [ cluster | factoryUrl type | factoryURL type cluster ] 
				    Start job "Name" in a cluster with/out factoryUrl
				    jobName: name of a job instances returned by job service

        Type should be one of the following four: RMFS, Fork, Pbs and Lsf. RMFS means job
        will be dispatched to Resource Manager Factory Service. For the other three, 
	you need to specify correct type base on the factory type and this is used by GRAM: 
		Factory						Type
		MasterForkManagedJobFactoryService 	        Fork
		MasterLsfManagedJobFactoryService 		Lsf 
		MasterPbsManagedJobFactoryService 		Pbs 
EOF
} 


##### MAIN #####
# Make sure GLOBUS_LOCATION is set
if [ ! -d "$GLOBUS_LOCATION" ] ; then
    echo "Error: GLOBUS_LOCATION invalid or not set: $GLOBUS_LOCATION" 1>&2
    exit 2
fi

full=`which $0`
dir=`dirname $full`
CONFIG="${dir}/csf.sh"
if [ -r "${CONFIG}" ] ; then
   . "${CONFIG}"
else
   echo "Error: Cannot find file ${CONFIG}"
   exit
fi

if [ $# -lt 1 ]; then
    usage 
    exit 1
else 
    if [ $1 = "help" -o $1 = "usage" ]; then 
	usage
	exit 1
    elif [ $1 = "version" ]; then 
       echo $0 version ${CSF_VERSION}
       exit 1
    fi
fi

ARGS=`getArguments "$@"`
eval java ${JOB_CLIENT} $ARGS
return_code=$?

exit $return_code


