#! /bin/csh -f

### sfr (subject/functional/raw), marty sereno, version 0.1b, 3/20/08

# orig, cp, rshuang, flavia, rome, lars, monk
set subjectlist = ( \
  /myrtos/usr0/subjects \
  /myrtos/usr1/mochlos-subjects \
  /mochlos/usr0/subjects \
  /mochlos/usr0/tylissos72G/subjects \
  /mochlos/usr1/fspaceROMA \
  /khania/usr1/ftpscan/lriecke \
  /khania/usr0/subjects \
  \
  /buc05/usr2/subjects-myrtos-usr0 \
  /buc05/usr2/subjects-myrtos-usr1-mochsub \
  /buc05/usr2/subjects-mochlos-usr0 \
  /buc05/usr2/subjects-mochlos-usr0-tylissos72G \
  /buc05/usr2/subjects-khania-usr0 \
  /buc05/usr2/subjects-myrtos-gournia-magcult \
  \
  /buc01/usr0/subjects \
  /buc03/usr1/subjects \
  /buc05/usr0/subjects \
)

set fspacelist = ( \
  /myrtos/usr0/fspace \
  /myrtos/usr0/fspace \
  /myrtos/usr1/fspace \
  /myrtos/usr1/fspaceORIG \
  /myrtos/usr1/gournia-magcult/fspace \
  /mochlos/usr0/fspace \
  /mochlos/usr0/tylissos72G/fspace \
  /mochlos/usr1/fspace \
  /mochlos/usr1/fspacePATIENT/auditory \
  /mochlos/usr1/fspaceROMA \
  /tylissos/usr0/fspace \
  /tylissos/usr1/fspace \
  /tylissos/usr1/zakros/inverse/usr2 \
  /tylissos/usr1/zakros/inverse/usr6 \
  /kommos/usr0/fspace \
  /kommos/usr0/bodyparts/fspace \
  /kommos/usr1/fspace \
  /khania/usr1/ftpscan/lriecke \
  \
  /buc05/usr2/fspace-mochlos-usr1 \
  /buc05/usr2/fspace-myrtos-usr0 \
  /buc05/usr2/fspace-myrtos-usr1ORIG \
  /buc05/usr2/fspace-tylissos-usr0 \
  /buc05/usr2/fspace-tylissos-usr1 \
  /buc05/usr3/fspace-khania-usr1-lriecke \
  /buc05/usr3/fspace-kommos-usr0 \
  /buc05/usr3/fspace-kommos-usr1 \
  /buc05/usr3/fspace-mochlos-usr1-PATIENT \
  /buc05/usr3/fspace-mochlos-usr1-ROMA \
  /buc05/usr3/fspace-myrtos-gournia-magcult \
  /buc05/usr3/fspace-myrtos-usr1 \
  /buc05/usr3/fspace-tylissos-usr1-zakusr26 \
  /buc05/usr3/mochlos-usr0-fspace \
  \
  /buc01/usr0/fspace \
  /buc03/usr1/sessions \
  /buc05/usr0/fspace \
  /buc05/usr0/monk/subjsess \
)

set rawlist = ( \
  /khania/usr0/ftpscan \
  /khania/usr1/ftpscan \
  \
  /buc01/usr0/raw \
  /buc03/usr1/raw \
  /buc05/usr0/raw \
)

set scandirs = 0
if ($#argv == 0) then
  set action = help
endif
# quote first arg else dash operations attempted
if ($#argv == 1) then
  if ("$1" == "-h")    set action = help
  if ("$1" == "-help") set action = help
  if ("$1" == "-dirs") set action = dirs
  if ("$1" == "-ls")   set action = ls
  if ("$1" == "-lsal") set action = lsal
  if ("$1" !~ -*) then
    set action = match
    set pattmatch = both
    set patt = $1
  endif
endif
if ($#argv == 2) then
  set action = match
  if ("$1" == "-both")  set pattmatch = both
  if ("$1" == "-start") set pattmatch = start
  if ("$1" == "-end")   set pattmatch = end
  if ("$1" == "-boths")  set pattmatch = both
  if ("$1" == "-starts") set pattmatch = start
  if ("$1" == "-ends")   set pattmatch = end
  if ("$1" == "-boths" || "$1" == "-starts" || "$1" == "-ends") then
    set scandirs = 1
  endif
  set patt = $2
endif
if ($#argv > 2) then
  echo "sfr: ### too many arguments"
  exit
endif

if ($action == "help") then
  echo ""
  echo "sfr: find subjects/functional/raw directories and scandirs"
  echo ""
  echo "Use: sfr                     [help]"
  echo "     sfr -dirs               [list subj/funct/raw dirs]"
  echo "     sfr -ls                 [ls everything in subj/funct/raw dirs]"
  echo "     sfr -lsal               [ls -al everything in subj/funct/raw dirs]"
  echo "     sfr <pattern>           [ls dirs that contain pattern]"
  echo "     sfr -both <pattern>     [same as above]"
  echo "     sfr -start <pattern>    [match dirs starting with pattern]"
  echo "     sfr -end <pattern>      [match dirs ending with pattern]"
  echo "     sfr -boths/-starts/-ends <pattern>     [ls scandirs, too]"
  echo ""
  echo "  Examples:"
  echo "     sfr rob           => find subjects that match *rob*"
  echo "     sfr -start 0506   => find all scans done in June 2005"
  echo "     sfr -end MS       => find sessions on subject MS"
  echo "     sfr -ends MS      => also list scandirs in those MS sessions"
  echo ""
endif

if ($action == "dirs") then
  echo ""
  echo "Subject Directories:"
  foreach dir ($subjectlist)
    echo "  $dir"
  end
  echo ""
  echo "Functional Directories:"
  foreach dir ($fspacelist)
    echo "  $dir"
  end
  echo ""
  echo "Raw Directories:"
  foreach dir ($rawlist)
    echo "  $dir"
  end
  echo ""
  exit
endif

if ($action == "ls" || $action == "lsal") then
  foreach dir ($subjectlist $fspacelist $rawlist)
    if (! -e $dir) continue
    echo "#####################################################################"
    echo $dir
    echo "#####################################################################"
    if ($action == "ls")   ls $dir
    if ($action == "lsal") ls -al $dir
  end
  exit
endif

if ($action == "match") then
  echo ""
  set foundcnt = 0
  foreach dir ($subjectlist $fspacelist $rawlist)
    if (! -e $dir) continue
    set subdirlist = `ls -1 $dir`
    foreach subdir ($subdirlist)
      set found = 0
      if ($pattmatch == "both"  && $subdir =~ *$patt*) set found = 1
      if ($pattmatch == "start" && $subdir =~  $patt*) set found = 1
      if ($pattmatch == "end"   && $subdir =~ *$patt ) set found = 1
      if ($found) then
        if ($scandirs) then
          echo "-----------------------------------"
          echo $dir/$subdir
          echo "-----------------------------------"
          ls $dir/$subdir/image
        else
          echo $dir/$subdir
        endif
        @ foundcnt++
      endif
    end
  end
  if (! $foundcnt) echo "no subj/funct/raw matches pattern: $patt"
  echo ""
endif

