#!/bin/bash

# -----------------------------------------------------------------------------
# init
# -----------------------------------------------------------------------------

pipeline_date=$(date +%F-%H-%M-%S)
script_dir=$(dirname "$0")

# -----------------------------------------------------------------------------
# command line arguments
# -----------------------------------------------------------------------------

function usage { cat << EOF
usage: bash $(basename "$0") chunk_dir [output]
EOF
}

[[ $# -eq 1 ]] || {
  usage >&2
  exit 1
}

chunk_dir=$1

[[ -d $chunk_dir ]] || {
  echo "chunk directory does not exist or is not a directory" >&2
  exit 1
}

csv_dir=/work/$USER/zamba/csvs-$pipeline_date
csv_chunk_dir=$csv_dir/chunks

if [[ -n $2 ]] ; then
  output=$2
else
  output="$csv_dir/output-$pipeline_date.csv"
fi

# -----------------------------------------------------------------------------
# submission
# -----------------------------------------------------------------------------

# create output directories
mkdir -p "/work/$USER/zamba/logs"

n_chunks=$(find "$chunk_dir" -mindepth 1 -maxdepth 1 -type d | wc -l)

# submit prediction
array_job_id=$(
  qsub \
    -terse \
    -v OUTPUT_DIR="$csv_chunk_dir" \
    -t 1-"$n_chunks" \
    "$script_dir"/submit-zamba-array-job.sh \
    "$chunk_dir"
)

# submit csv combiner
qsub \
  -hold_jid "${array_job_id%%.*}" \
  "$script_dir"/submit-csv-combiner.sh \
  "$csv_chunk_dir" \
  "$output"
