#!/bin/csh -f

#
# Change log:

if ( $#argv != 1 || "$argv[1]" == "-help") then
    echo "Usage: `basename $0` <dsetname>"
    echo ""
echo "Search AFNI_GLOBAL_SESSION, AFNI_PLUGINPATH, and afni bin directory"
echo " (in that order) for named dataset.  If found, echo the first valid path"
echo " discovered and return zero status.  If not found, return non-zero status."
echo ""
echo "-help to get this message"
echo ""
echo " Jason W. Bacon"
echo " Medical College of Wisconsin"
echo " Sep 27, 2006"
   echo ""
    if ( $#argv != 1 ) exit 1
    exit 0
endif

set ref_in = $1
set dataset_path = './'

if ( $?AFNI_GLOBAL_SESSION ) then
    set dataset_path = (${dataset_path} `echo $AFNI_GLOBAL_SESSION | tr ':' ' '`)
endif

if ( $?AFNI_ATLAS_PATH ) then
    set dataset_path = (${dataset_path} `echo $AFNI_ATLAS_PATH | tr ':' ' '`)
endif

if ( -d $HOME/.afni/atlases ) then
    set dataset_path = (${dataset_path} $HOME/.afni/atlases)
endif

if ( $?AFNI_PLUGINPATH ) then
    set dataset_path = (${dataset_path} `echo $AFNI_PLUGINPATH | tr ':' ' '`)
endif

foreach dir (${dataset_path})
    if ( `@CheckForAfniDset ${dir}/${ref_in}` == 2 ) then
	echo $dir
	exit 0
    endif
end

# Check afni bin directory, for compatibility with older installations
# that installed atlas datasets there.
set wa = `which afni`
if ( $status != 0) then
   exit 1
endif
set ref_path = "$wa:h"
if ( "$ref_path" == "$wa" ) then
   exit 1
endif
if ( `@CheckForAfniDset ${ref_path}/${ref_in}` ) then
    echo ${ref_path}
    exit 0
else
    exit 1
endif

