#! /bin/csh -f

#
# ppcheck-sess
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:18 $
#    $Revision: 1.2 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA). 
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#


set VERSION = '$Id: ppcheck-sess,v 1.2 2007/01/09 22:41:18 nicks Exp $';
set PrintHelp = 0;
set cmdargs = ($argv);

set funcstem = ();
set fsd = bold
set runlistfile = ();
set checklevel = 3;

if($#argv == 0)  goto usage_exit;
set n = `echo $argv | grep -e -help | wc -l` 
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
  exit 1;
endif

set SessList = `getsesspath $argv`;
if($status || $#SessList == 0) then
  getsesspath $argv 
  exit 1;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

#------------------------------------------------------------------#
foreach sess ($SessList)
  set sessid = `basename $sess`;

  ### Check that the target directory is there ###
  if(! -d $sess/$fsd) then
    echo "ERROR: $sess/$fsd does not exist"
    exit 1;
  endif

  ### Check that the target directory is writable ###
  if(! -w $sess/$fsd) then
    echo "ERROR: $sess/$fsd is not writable"
    exit 1;
  endif

  set RunList = `getrunlist $sess/$fsd $runlistfile`;
  #echo "RunList $RunList"

  ## Go through each run ##
  foreach run ($RunList)

    set checkstem = $sess/$fsd/$run/$funcstem

    set f0hdr = $checkstem"_000.hdr"
    if(! -e $f0hdr) then
      #echo $bhdr
      echo $sessid $run hdr
      continue;
    endif

    if($checklevel < 2) continue;

    set bhdr = $checkstem.bhdr
    if(! -e $bhdr) then
      #echo $bhdr
      echo $sessid $run bhdr
      continue;
    endif

    if($checklevel < 3) continue;

    set mvfile = $checkstem.meanval
    if(! -e $mvfile) then
      #echo $bhdr
      echo $sessid $run meanval
      continue;
    endif

    if($checklevel < 4) continue;

  end # Loop over runs

end # Loop over sessions #


exit 0;
###############################################


############--------------##################
parse_args:
set cmdline = "$argv";
while( $#argv != 0 )

  set flag = $argv[1]; shift;
  
  switch($flag)

    case "-f":
      if ( $#argv == 0) goto arg1err;
      set funcstem = $argv[1]; shift;
      breaksw

    case "-level":
      if ( $#argv == 0) goto arg1err;
      set checklevel = $argv[1]; shift;
      breaksw

    case "-fsd":
      if ( $#argv == 0) goto arg1err;
      set fsd = $argv[1]; shift;
      breaksw

    case "-rlf":
      if ( $#argv == 0) goto arg1err;
      set runlistfile = $argv[1]; shift;
      breaksw

    case "-g":
    case "-s":
    case "-sf":
    case "-d":
    case "-df":
      shift;
      # ignore getsesspath arguments 
      breaksw

    case "-cwd":
      # ignore getsesspath arguments 
      breaksw

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      setenv MRI_UMASK $1; shift;
      breaksw

    case "-debug":
      set verbose = 1;
      set echo = 1; # turns on terminal echoing
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized. 
      echo $cmdline
      exit 1
      breaksw
  endsw

end

goto parse_args_return;
############--------------##################

############--------------##################
check_params:
  if($#funcstem == 0) then
    echo "ERROR: must specify a functional stem"
    exit 1;
  endif

goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: ppcheck-sess"
  echo ""
  echo "  -f funcstem "
  echo ""
  echo "Session Arguments (Required)"
  echo "   -sf sessidfile  "
  echo "   -df srchdirfile "
  echo "   -s  sessid      "
  echo "   -d  srchdir     "
  echo ""
  echo "Optional Arguments"
  echo "" 
  echo "   -level level : check level 1, 2, or 3 <$checklevel>" 
  echo "   -fsd fsd : default is bold"
  echo "   -rlf runlistfile : default is all runs"
  echo ""
  echo "Other Arguments"
  echo "   -umask umask    : set unix file permission mask"
  echo "   -version        : print version and exit"
  echo "   -help           : print help and exit"
  echo ""

  if(! $PrintHelp) exit 1;

  echo $VERSION

  echo "------------------------------------------------------------"
  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'

  echo "------------------------------------------------------------"

exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP

FS-FAST utility to check whether raw or preprocessed data exists
in a run directory.

ARGUMENTS

-level levelno

Check using the given level number. Each level checks a different part 
the raw data plus whatever the the lower levels checked:

  1   checks for stem_000.hdr
  2   checks for stem.bhdr (and level 1)
  3   checks for stem.meanval (and level 2)

The .meanval file is produced by inorm. Default is level 3.

-f stem

Check for the given stem in each run

-rlf runlistfile

Only check the runs in the runlistfile. Default is to check all runs.

-fsd fsd

Use fsd instead of bold.

OUTPUT

Prints out any session and run that fails a check. If everything 
passes, then nothing is printed out.

EXAMPLE:

ppcheck-sess -level 2 -f f -rlf noncog.rlf \
  -df sessdir.notsg2 -sf all.notsg2.sid 


