#!/bin/sh
# Submit a PBS script already staged on Aspire (see aspire-ship) and print the
# job ID. A thin wrapper — the only value it adds over typing ssh+qsub by hand
# is the absolute qsub path and connect-first, both easy to forget.
#
#   ./bin/aspire-submit <remote-path-to-.pbs> [qsub-args...]
#
# Examples:
#   ./bin/aspire-submit /scratch/users/nus/<username>/myrun/submit.pbs
#   ./bin/aspire-submit -J 0-3 /scratch/users/nus/<username>/myrun/array.pbs

set -eu

HOST="${ASPIRE_HOST:-aspire}"
BIN=$(cd -P "$(dirname "$0")" && pwd)

if [ $# -eq 0 ]; then
    echo "usage: aspire-submit [qsub-args...] <remote-path-to-.pbs>" >&2
    exit 2
fi

"$BIN/aspire-connect"

echo "aspire-submit: qsub $* on $HOST" >&2
JOB_ID=$(ssh -o BatchMode=yes "$HOST" /opt/pbs/bin/qsub "$@")
echo "aspire-submit: submitted $JOB_ID" >&2
echo "$JOB_ID"
