#!/bin/bash

# from the xnnppx package (https://github.com/INCF/xnnppx); see COPYING 
# for copyright and license

pipeline_home=/home/ch/xnat/pipeline

# call with command-line arguments and a target flag:
# get_parameter_value -pipeline "$@"
# returns the argument after "-pipeline"
get_parameter_value()
{

    target="$1"
    shift

    echo "$@" | tr ' ' '\n' | grep -A 1 -e "$target" | tail -1

    return

} # end get_parameter_value()

xml=`get_parameter_value -pipeline "$@"`
py=`echo "$xml" | sed 's/xml$/py/'`

if [ -f $py ]
then
    /home/ch/arc-queue/arc-qadd $pipeline_home/bin/XnatPythonLauncher "$@" || exit 1
else
    /home/ch/arc-queue/arc-qadd $pipeline_home/bin/XnatPipelineLauncher.orig `echo "$@" | sed 's/\-notify [^ ]* //g'` || exit 1
    if [ $xml = xnat_tools/AutoRun.xml ]
    then
        # qc pipeline goes here
        project=`get_parameter_value -project "$@"`
        id=`get_parameter_value -id "$@"`
        /home/ch/arc-queue/arc-qadd /home/ch/one_click/run_qc $project $id || exit 1
    fi
fi

exit 0

# eof
