#!/bin/bash

#set -x


# Get dataset path to work on
if [ "$1" ]
then
    # A path has been given -> Test if path is a PROCNO path
	ParSpaceId=`pvcmd -a ParxServer -r OpenSharedPs -dset $1`
	echo ParSpaceId =$ParSpaceId
	if [ `pvcmd -a ParxServer -r OpenSharedPs -dset $1` == -1 ]
    then
        echo -e "Path $1 is not a EXPNO path." 1>&2
        exit 1
    fi
	DataPath=`pvcmd -a ParxServer -r DsetGetPath -psid $ParSpaceId -path EXPNO`
	echo DataPath = $DataPath
else
    echo -e "No EXPNO path given." 1>&2
    exit 1
fi


# Set dataset path from examination path.
ProcnoPath=$DataPath/pdata/1
echo -e ProcnoPath = $ProcnoPath

# Get list of all open parameter spaces.
# Find parameter space for the current path
# Get the parameter space key for the GUI.
GuiKey=$(pvcmd -a ParxServer -r ListPs -csv | grep $ProcnoPath | cut -d ";" -f 2)
echo GuiKey = $GuiKey

# Change it to the parameter space key for acquisition
AcqKey=$(echo $GuiKey | sed -e "s/_87$/_65/")

echo Acqkey = $AcqKey
pvcmd -a JMacroManager JMMExecuteMacro -category $USER -macro Feed2AFNI_rt3DEPI -id 10 -args $AcqKey $ParSpaceId $ProcnoPath
