#!/usr/bin/ksh
#
# usage:
#	wfu_CONVERT_DICOM study_directory dicom_image_directory
#
# run wfu_DICOM.m and wfu_postprocess.m on study_directory
#
# if no dicom_image_directory is given, the locs directory
# will be used.
#
# dicom_image_directory may be absolute or relative to the study_directory
# 

. $WFU_PATH/global.site.ksh

if (test $# -lt 1) then
	echo usage:
	echo wfu_CONVERT_DICOM study_directory dicom_image_directory
	exit
fi

curDir=`pwd`
studyDir=$1

if [[ ! -d $studyDir ]]; then
	echo "no such directory: $studyDir"
	exit
fi

if [[ $# -lt 2 ]]; then
	imageDir="locs"
else
	imageDir=$2
fi

if [[ ! -d $imageDir ]]; then
	if [[ ! -d $studyDir/$imageDir ]]; then
		echo "no such dicom dirctory: $imageDir"
		exit
	fi
fi

echo "running DICOM conversion on processing: $imageDir"
cd $studyDir
cmd="wfu_startup([],5);wfu_DICOM('$imageDir');exit;"
echo $cmd
${batch_matlab} -r "$cmd"

echo "running POSTPROCESS on: $studyDir"
cmd="wfu_startup([],5);wfu_postprocess();exit;"
echo $cmd
${batch_matlab} -r "$cmd"

echo "if no paradigm numbers are in the dicom files (dicom "
echo "field 0019,10e0), then please create a .paradigms file"
echo "in the $studyDir directory containing"
echo "the following 4D files along with their respective "
echo "paradigm number.  Then run:"
echo "  ${WFU_PIPELINE}/bin/wfu_AUTOSPM5 $studyDir"
echo "to continue autoprocessing, if so desired."

find . -maxdepth 1 -name "*_4D.img" -o -name "*_4D.nii" | xargs -I {} basename {}

cd $curDir


