#! /bin/sh

# Shift all arguments by one to get command parameters
getArguments () {
    args=""
    while [ "X$1" !=  "X" ] ; do
        args="${args} $1"
        shift
    done
    echo $args
}

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

Create a job instance through job factory service.

Options:
	help | usage         	    Display usage
	version          	    Display command version
	rsl    rslfile [jobname]    Submit a job using RSL file with/out a job name
				    rslfile is path to job rsl file.
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 ${CONFIG} configuration file"
   exit
fi

##### MAIN #####
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 
    elif [ $1 != "rsl" ]; then
        usage
        exit 1
    fi
fi

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

exit $return_code


