#!/bin/sh

show_help() 
{

	echo "Use: sge_submit_workers [options] <servername> <port> <num-workers>"
	echo "         when auto mode is not enabled, or"  
	echo "     sge_submit_workers [options] <num-workers>"
	echo "         when auto mode is enabled."  
	echo "Options:"
	echo "  -M,--master-name <name>  Name of the preferred master for worker. (auto mode enabled)"
	echo "  -N,--name <name>         Same as -M (backwards compatibility). (auto mode enabled)"
	echo "  -C,--catalog <catalog>   Set catalog server to <catalog>. <catalog> format: HOSTNAME:PORT."
	echo "  -t,--timeout <time>      Abort after this amount of idle time. (default=900s)."
	echo "  -d,--debug <subsystem>   Enable debugging on worker for this subsystem (try -d all to start)."
	echo "  -w,--tcp-window-size <size>  Set TCP window size."
	echo "  -i,--min-backoff <time>  Set initial value for backoff interval when worker fails to connect to a master. (default=1s)"
	echo "  -b,--max-backoff <time>  Set maxmimum value for backoff interval when worker fails to connect to a master. (default=60s)"
	echo "  -z,--disk-threshold <size>   Set available disk space threshold (in MB). When exceeded worker will clean up and reconnect. (default=100MB)"
	echo "  -A,--arch <arch>         Set architecture string for the worker to report to master instead of the value in uname."
	echo "  -O,--os <os>             Set operating system string for the worker to report to master instead of the value in uname."
	echo "  -s,--workdir <path>      Set the location for creating the working directory of the worker."
	echo "  -P,--password <pwfile>   Password file to authenticate workers to master."
	echo "  --cores <num>            Set the number of cores each worker should use (0=auto). (default=1)"
	echo "  --memory <size>          Manually set the amonut of memory (in MB) reported by this worker."
	echo "  --disk <size>            Manually set the amount of disk (in MB) reported by this worker."
	echo "  -j                       Use job array to submit workers."	
	echo "  -p <parameters>          SGE qsub parameters."
	echo "  -h,--help                Show this help message."
	exit 1
}

arguments=""
use_auto=0
use_jobarray=0
parameters=""
cores=1

# Used options (as in the getopts format):  aM:N:C:t:d:w:i:b:z:A:O:s:P:jp:h  

while [ $# -gt 0 ]
do
	case $1 in
		-a | --advertise)
            #left here for backwards compatibility
			arguments="$arguments -a"
			use_auto=1
			;;
		-M | --master-name)
		    shift
			arguments="$arguments -M $1"
			use_auto=1
			;;
		-N | --name)
		    shift
			arguments="$arguments -M $1"
		   	use_auto=1
			;;
		-C | --catalog)  
		    shift
			arguments="$arguments -C $1"
			;;
		-t | --timeout)  
		    shift
			arguments="$arguments -t $1"
			;;
		-d | --debug)  
			shift
			arguments="$arguments -d $1"
			;;
		-w | --tcp-window-size)
			shift
	  		arguments="$arguments -w $1"
			;;
		-i | --min-backoff)
			shift
			arguments="$arguments -i $1"
			;;
		-b | --max-backoff)
			shift
		    arguments="$arguments -b $1"
			;;
		-z | --disk-threshold)
			shift
			arguments="$arguments -z $1"
			;;
		-A | --arch)
			shift
			arguments="$arguments -A $1"
			;;
		-O | --os)  
			shift
			arguments="$arguments -O $1"
			;;
		-s | --workdir)  
			shift
			arguments="$arguments -s $1"
			;;
		-P | --password)  
			shift
			pwfile=$1 
			arguments="$arguments -P $pwfile"
			;;
		--cores)  
			shift
			cores=$1
			arguments="$arguments --cores $cores"
			;; 
		--memory)  
			shift
			arguments="$arguments --memory $1"
			;;
		--disk)  
			shift
			arguments="$arguments --disk $1"
			;;
		-j)  
			use_jobarray=1
			;;
		-p)  
			shift
			parameters="$parameters $1"
			;;
		-h | --help)
		    show_help
			;;
		*)
			break
			;;
	esac
	shift
done

if [ $use_auto = 0 ]; then
    if [ $# -ne 3 ] ; then
		echo "3 arguments (<servername> <port> <num-workers>) are expected while $# is present: \"$@\"."
		echo "To view the help message, type: sge_submit_workers -h"
		exit 1
    fi
    host=$1
    port=$2
    count=$3
else
    if [ $# -ne 1 ]
    then
		echo "1 argument (<num-workers>) is expected while $# is present: \"$@\"."
		echo "To view the help message, type: sge_submit_workers -h"
		exit 1
    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

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

mkdir -p ${USER}-workers
cd ${USER}-workers
cp $worker .

cat >worker.sh <<EOF
#!/bin/sh
#$ -pe smp $cores
./work_queue_worker $arguments $host $port
EOF

chmod 755 worker.sh

if [ $use_jobarray = 1 ]
then
	qsub -t 1-$count:1 -cwd $parameters worker.sh	
else 
	for n in `seq 1 $count`
	do
		qsub -cwd $parameters worker.sh
	done
fi
return_status=$?

exit $return_status
