#!/bin/sh

show_help() 
{
	echo "Use: condor_submit_workers [options] <servername> <port> <num-workers>"
	echo "options:"
	echo "  -a              Enable auto mode."
	echo "  -s              Run as a shared worker."
	echo "  -N <name>       Preferred master name."
	echo "  -C <catalog>    Set catalog server to <catalog>. <catalog> format: HOSTNAME:PORT."
	echo "  -t <time>       Abort after this amount of idle time. (default=900s)"
	echo "  -r <requirements>   Condor requirements ClassAd."
	echo "  -h              Show this help message."
	exit 1
}

arguments=""
requirements=""
use_auto=0

while getopts aSN:t:r:h opt 
do
	case "$opt" in
		a)  arguments="$arguments -a"; use_auto=1;;
		s)  arguments="$arguments -s";;
		N)  arguments="$arguments -N $OPTARG";;
		C)  arguments="$arguments -C $OPTARG";;
		t)  arguments="$arguments -t $OPTARG";;
		r)  requirements="$requirments $OPTARG";;
		h)  show_help;;
		\?) show_help;;
	esac
done

shift $(expr $OPTIND - 1)

if [ $use_auto = 0 ]; then
    if [ X$3 = X ]
    then
	show_help	
    fi
    host=$1
    port=$2
    count=$3
else
    if [ X$1 = X ]
    then
	show_help	
    fi
    host=
    port=
    count=$1
fi

worker=`which work_queue_worker 2>/dev/null`
if [ $? != 0 ]
then
	echo "$0: please add 'work_queue_worker' to your PATH."
	exit 1
fi

mkdir /tmp/${USER}-workers
cd /tmp/${USER}-workers
cp $worker .

condor_submit << EOF
universe = vanilla
executable = work_queue_worker
arguments = $arguments $host $port
transfer_input_files = work_queue_worker
should_transfer_files = yes
when_to_transfer_output = on_exit
output = worker.\$(PROCESS).output
error = worker.\$(PROCESS).error
log = workers.log
getenv = true
requirements = $requirements
queue $count
EOF

exit $?
