#!/bin/sh

#
# This file is part of tej
# https://github.com/VisTrails/tej
#
# Job submission script
# Runs on the server once the job has been transferred
#
# Arguments:
#   1. job ID
#   2. job directory, obtained from new_job
#   3. command or script path (relative to job)
#

set -e

# Include
. "$(dirname "$0")/lib/utils.sh"

# Inputs
job_id="$1"
job_dir="$(absolutepathname "$2")"
script="$3"

cd "$(dirname "$0")/.."

(date; echo "submit $@") >> tej.log

# Sanity check
if ! [ -d "$job_dir" ]; then
    echo "Job directory doesn't exist" >> tej.log
    echo "Job directory doesn't exist" >&2
    exit 1
fi
read status < "$job_dir/../status"
if [ "$status" != created ]; then
    echo "Job is already $status" >> tej.lob
    echo "Invalid job status '$status'" >&2
    exit 1
fi

# Starts process
cd "$job_dir"
nohup "../../../commands/start" "$script" > /dev/null 2>&1 < /dev/null &
