#!/bin/bash -l

# Create SGE qsubs for all supplied image runs, with appropriate dependencies.
allIDs=''; delim='';
for imageRun
do
    trackID=$(qsub -terse -N Celegans${imageRun}Track -o ./logs/${imageRun}_track.log ./track.sh ${imageRun})
    spinID=$(qsub -terse -N Celegans${imageRun}Spin -o ./logs/${imageRun}_spin.log ./spin.sh ${imageRun})
    animateID=$(qsub -terse -hold_jid ${trackID},${spinID} -N Celegans${imageRun}Animate -o ./logs/${imageRun}_animate.log ./animate.sh ${imageRun})
    printf 'For %s: using jobs %s, %s and %s\n' "${imageRun}" "${trackID}" "${spinID}" "${animateID}"
    allIDs=${allIDs}${delim}${trackID}; delim=','
    allIDs=${allIDs}${delim}${spinID}
    allIDs=${allIDs}${delim}${animateID}
done

# If you want to receive an email when the work is complete, use these lines below, putting your email address in the qsub -M field as shown.
# Create a job that just waits until all the others are complete in order to send an email that it's done.
# qsub -hold_jid ${allIDs} -M you@you.com ./completion.sh >/dev/null
# printf 'Will email when these are complete: %s\n' "${allIDs}"
