#!/bin/csh -f
# funcvits-sess
#
set VERSION = '$Id: funcvits-sess,v 1.3 2000/08/08 21:11:43 greve Exp greve $';
set FASTVER = `cat $FMRI_ANALYSIS_DIR/docs/version`;
set inputargs = "$argv";

set analysis    = ();
set ScriptOnly = 0;
set umaskarg = ();
set paintsurf = ();
set nolog = 0;

if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

set n = `echo $argv | grep version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

## Look for nolog option ##
set n = `echo $argv | grep nolog | wc -l` 
if($n != 0) set nolog = 1;

##### Create a log file ######
if(! $nolog) then
  set logdir = `pwd`/log;
  mkdir -p $logdir
  if(! -e $logdir) then
    echo "ERROR: could not create $logdir"
    exit 1;
  endif
  set LF = $logdir/funcvits-sess.log
  if(-e $LF) mv $LF $LF.old
else
  echo "No log file"
  set LF = /dev/null
endif

echo "----------------------------------------------------------"
echo "funcvits-sess Logfile is $LF"
echo "----------------------------------------------------------"

echo "fucnvits-sess log file" >> $LF
echo $VERSION >> $LF
echo "FAST Version $FASTVER"  >> $LF
pwd           >> $LF
echo "$0"     >> $LF
echo "$inputargs"  >> $LF
uname -a      >> $LF
date          >> $LF
which mri-funcvits >> $LF

set SessList = `getsesspath $argv`;
if($status || $#SessList == 0) then
  echo "ERROR: cannot find any sessions" | tee -a $LF
  exit 1;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

## Get functional subdirectory from the info file ##
set infofile = $analysis/analysis.info
if(! -e $infofile) then
  echo "ERROR: cannot find $infofile"
  exit 1;
endif
set fsd = `cat $infofile | awk '{if($1 == "fsd") print $2}'`;

foreach sess ($SessList)

  echo "-------------------------------------------" |& tee -a $LF
  echo "-------------------------------------------" |& tee -a $LF
  echo "Session: $sess " |& tee -a $LF
  date |& tee -a $LF

  # Make sure the Session Dir exists (redundant) ##
  if(! -d $sess) then
    echo "ERROR: $sess does not exist"   |& tee -a $LF
    exit 1;
  endif

  ## Go to the functional subdirectory for the session ##
  pushd $sess/$fsd > /dev/null
  df . |& tee -a $LF

  ## Make sure there is a register.dat ##
  set regfile = register.dat
  if(! -e $regfile ) then
    echo "ERROR ($sess): no $regfile file found" |& tee -a $LF
    exit 1;
  endif

  set funcstem = $analysis/h
  set fvitdir  = fvit

  ## Prepare the script, if need be ##
  if($ScriptOnly) then
    mkdir -p scripts
    if(! -d scripts) then
      echo "ERROR ($sess): cannot create `pwd`/scripts"
      exit 1;
    endif
    set scriptfile = scripts/run-funcvits-$analysis
    rm -f $scriptfile
    touch $scriptfile
    echo "#\!/bin/csh -f" >> $scriptfile
    echo "cd .." >> $scriptfile
    chmod a+x $scriptfile
  endif
    
  ## Create the command line ###
  set cmd = "mri-funcvits $umaskarg -stem $funcstem"
  set cmd = "$cmd -reg $regfile -outdir $fvitdir -paintsurf $paintsurf"
  pwd |& tee -a $LF
  echo "$cmd" |& tee -a $LF
  if(! $ScriptOnly) then
    $cmd |& tee -a $LF
    if($status) then
      echo "ERROR ($sess): mri-funcvits failed" |& tee -a $LF
      exit 1;
    endif
  else
    echo "$cmd" >> $scriptfile
    echo "\n\n" >> $scriptfile
  endif

  popd > /dev/null
  echo " " |& tee -a $LF
  echo " "

end

date | tee -a $LF
echo "funcvits-sess completed SUCCESSFULLY" | tee -a $LF
echo " "
echo " "

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

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

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

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

    case "--version":
    case "-version":
      echo $VERSION
      exit 0;
      breaksw

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

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

    case "-sphdir":
    case "-hemi":
      ## This is actually just ignored ##
      if ( $#argv == 0) goto arg1err;
      shift;
      breaksw

    case "-verbose":
      set verbose = 1;
      breaksw

    case "-echo":
      set echo = 1;
      breaksw

    case "-debug":
      set verbose = 1;
      set echo = 1;
      breaksw

    case "-scriptonly":
      set ScriptOnly = 1;
      breaksw

    case "-cwd":
    case "-nolog":
    case "-old":
    case "-vss":
      breaksw

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

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

end

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

############--------------##################
check_params:

  if ($#SessList == 0) then
     echo "ERROR: no sessions specified"|& tee -a $LF 
     exit 1
  endif

  if ($#analysis == 0) then
     echo "ERROR: no analaysis name specified"|& tee -a $LF 
     exit 1
  endif

  if($#paintsurf == 0) set paintsurf = white;

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

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

############--------------##################
usage_exit:
  echo "USAGE: funcvits-sess"
  echo "Options:";
  echo "   -analysis analysisname : session-level functional analysis name"
  echo "   -sf sessidfile  ..."
  echo "   -df srchdirfile ..."
  echo "   -s  sessid      ..."
  echo "   -d  srchdir     ..."
#  echo "   -fsd dir : session functional subdirectory ($fsd)"
  echo "   -umask umask   : set unix file permission mask"
  echo "   -scriptonly    : don't run, just generate a script"
  echo "   -version       : print version and exit"
exit 1;
