#!/bin/bash

function usage()
{
    (( $# == 0 )) || echo "error:" "$@"
    echo "USAGE:  ./jelly-stage.sh <dataset> <stage>"
    exit 1
} >&2

(( $# >= 1 )) || usage 'missing <dataset>'
(( $# >= 2 )) || usage 'missing <stage>; allowed values: setup mapping support extraction assembly output'
(( $# == 2 )) || usage 'too many arguments'

SPECIES="$1"
STAGE="$2"
PROTOCOL="$PBGAPS/source/pbjelly/$SPECIES/protocol.xml"
WORKDIR="$PBGAPS/workdir/$SPECIES/pbjelly"
LOG="$WORKDIR/logs/$STAGE.log"

[[ -e "$PROTOCOL" ]] || exit 42

mkdir -p "$WORKDIR" "$(dirname "$LOG")"

Jelly.py "$STAGE" "$PROTOCOL" --debug |& tee "$LOG"
echo '-- job IDs'
sed -nE 's/Submitted batch job ([0-9]+)/'"$STAGE"' \1\n/gp' "$LOG" | grep -oE '[0-9]+$' | tr '\n' ' '
echo

