#!/bin/sh

if [ $# == 0 ]; then
    echo "Usage: ./tune-main-cluster <BINDIR> <EXECDIR> additional_args_to_irace"
    exit 1
fi

BINDIR=$1
EXECDIR=$2
shift 2

#MACHINE=opteron2216
#MACHINE=xeon5410
#MACHINE=opteron6128
MACHINE=opteron6272

QUEUE="long"
#QUEUE="short"

exec qsub -v PATH <<EOF
#!/bin/bash
#$ -N irace-${EXECDIR#execdir/}-$$
#$ -l $MACHINE
#$ -l $QUEUE
#$ -S /bin/bash
#$ -m as
#      b     Mail is sent at the beginning of the job.
#      e     Mail is sent at the end of the job.
#      a     Mail is sent when the job is aborted or rescheduled.
#      s     Mail is sent when the job is suspended.
#$ -o /dev/null
#$ -e $EXECDIR/irace-$$.stderr
#$ -cwd

# Working on /tmp should be faster
TMP=\$(mktemp -d -t \${HOSTNAME}-XXXXXXXXXX)
if [ ! -d "\$TMP" ]; then
  echo "cannot create temporary directory" >&2
  exit 1
fi
echo \$TMP/irace-$$.stdout >&2
exec 1> \$TMP/irace-$$.stdout
echo \$TMP/irace-$$.stdout >&2
$BINDIR/irace --exec-dir=\$TMP $*
RET=\$?
# Use command to avoid aliases
command cp --force -R \$TMP/./ $EXECDIR/
rm -rf \$TMP
exit \$RET
EOF
