#!/bin/tcsh -f
if ("$1" == '' || "$1" == '-h' || "$1" == '-help') then
	goto USAGE
endif

set noglob

set basearg = 1
set withpath=0

# check for with path option
if ( "$argv[1]" == "-p" ) then
    set withpath = 1
    @ basearg += 1
endif

# note input dataset
set inname = "$argv[$basearg]"

# note any PrefSuf
set PrefSuf = ''
if ( $#argv > $basearg ) then
   @ psarg = $basearg + 1
   set PrefSuf = "$argv[$psarg]"
endif

# separate dir and file (and be careful of spaces)
set dname = "$inname:h"
set fname = "$inname:t"

# if no dir, use .
if ( "$dname" == "$inname" ) then
   set pt = "."
else
   set pt = "$dname"
endif

# remove any sub-brick selector
set Nom = `echo "$fname" | awk -F[ '{print $1}'`

#remove extension .HEAD or .BRIK
set ext = $Nom:e
set fext = ""
if ("$ext" == "gz") then
   #remove .gz first
   set Nom = $Nom:r
   set ext = $Nom:e
   set fext = ".gz"
else if ("$ext" == "bz2") then
   set Nom = $Nom:r
   set ext = $Nom:e
   set fext = ".bz2"
else if ("$ext" == "Z") then
   set Nom = $Nom:r
   set ext = $Nom:e
   set fext = ".Z"
else if ("$ext" == ".BRIK" || "$ext" == ".HEAD") then
   set Nom = $Nom:r
   set fext = ""
endif
if ("$ext" == "nii") then
   set Nom = $Nom:r$PrefSuf".$ext$fext"
   goto ECHO
endif
    
#Remove extension only if it is .HEAD or .BRIK
if ("$ext" == "HEAD" || "$ext" == "BRIK") then
   set Nom = $Nom:r
endif
#remove +orig or whatever it is
set Nom = `echo $Nom | sed 's/+orig.$//'`
set Nom = `echo $Nom | sed 's/+acpc.$//'`
set Nom = `echo $Nom | sed 's/+tlrc.$//'`
set Nom = `echo $Nom | sed 's/+orig$//'`
set Nom = `echo $Nom | sed 's/+acpc$//'`
set Nom = `echo $Nom | sed 's/+tlrc$//'`
set Nom = $Nom$PrefSuf
goto ECHO

ECHO:
if ($withpath) then
   echo $pt/$Nom
else
   echo $Nom
endif
unset noglob

goto ENDALL

USAGE:
	echo "Usage: `basename $0` <Name> [Suffix]"
	echo "example: `basename $0` /Data/stuff/Hello+orig.HEAD"
	echo "returns the afni prefix of name (Hello)" 
	echo "Wildcards are treated as regular characters:"
	echo "example: `basename $0` 'AAzst1r*+orig'"
	echo "returns : AAzst1r*"
	echo ""
   echo "If a Suffix string is specified, then it is"
   echo "appended to the returned prefix."
   echo ""
   echo "Ziad Saad (saadz@mail.nih.gov)"
   echo "  LBC/NIMH/ National Institutes of Health, Bethesda Maryland"
   echo ""
	goto ENDALL
ENDALL:
