#! /bin/csh -f

#
# func2roi-sess-mrun - just a front end to run func2roi-sess for
# multiple runs (ie, on the output of a jackknife or perrun selxavg)
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:17 $
#    $Revision: 1.4 $
#
# 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: func2roi-sess-mrun,v 1.4 2007/01/09 22:41:17 nicks Exp $';

set inputargs = ($argv);
set func2roiargs = ();

set analysis  = ();
set mruntype   = ();
set nolog = 0;

set PWDCMD = `getpwdcmd`;

if($#argv == 0) goto usage_exit;

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

## Look for nolog option ##
set n = `echo $argv | grep -e -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/func2roi-sess-mrun.log
  if(-e $LF) mv $LF $LF.old
else
  echo "No log file"
  set LF = /dev/null
endif

echo "----------------------------------------------------------"
echo "func2roi-sess-mrun logfile is $LF"
echo "----------------------------------------------------------"

echo "fucn2roi-sess-mrun log file" >> $LF
echo $VERSION >> $LF
pwd           >> $LF
echo $0     >> $LF
echo $inputargs  >> $LF
uname -a      >> $LF
date          >> $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}'`;
set runlistfile = `cat $infofile | awk '{if($1 == "runlistfile") print $2}'`;

####### Go through each session ######
set StartDate = `date`;
set nth = 1;
foreach sess ($SessList)
  pushd $sess > /dev/null
  set sessid     = `basename $sess`;
  set sessparent = `dirname $sess`;

  echo "-----------------------------------------------------" |& tee -a $LF
  echo "Session: $sessid $nth/$#SessList" |& tee -a $LF
  echo $StartDate |& tee -a $LF
  date |& tee -a $LF
  @ nth = $nth + 1;

  if($#runlistfile == 0) then
    set runlist = `getrunlist $fsd`;
    if($#runlist == 0) then
      echo "ERROR: could not find any runs" |& tee -a $LF
      pwd |& tee -a $LF
      exit 1;
    endif
    else
    if( ! -e $fsd/$runlistfile ) then
      echo "ERROR: cannot find $fsd/$runlistfile" |& tee -a $LF
      pwd |& tee -a $LF
    endif
    set runlist = `cat $fsd/$runlistfile`;
    if($#runlist == 0) then
      echo "ERROR: $fsd/$runlistfile is empty" |& tee -a $LF
      pwd |& tee -a $LF
      exit 1;
    endif
  endif
  echo "RunList: $runlist" |& tee -a $LF

  popd > /dev/null;
  foreach run ($runlist)
    if($mruntype == "jackknife") then
      set anapend = -jk$run
    else
      set anapend = -$run
    endif
    set cmd = (func2roi-sess -anapend $anapend $func2roiargs)
    set cmd = ($cmd -s $sessid -d $sessparent)
    echo "--------------------------------------------" |& tee -a $LF
    echo $PWD |& tee -a $LF
    echo $cmd |& tee -a $LF
    echo "--------------------------------------------" |& tee -a $LF
    $cmd |& tee -a $LF
    if($status) then
      echo "ERROR: func2roi-sess failed"|& tee -a $LF
      exit 1;
    endif
  end

end

echo $StartDate | tee -a $LF
date | tee -a $LF
echo "func2roi-sess completed " | tee -a $LF
echo " "
echo " "

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

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

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

    case "-mruntype"
    case "-type"
      if ( $#argv == 0) goto arg1err;
      set mruntype = $argv[1]; shift;
      if($mruntype != jackknife && $mruntype != perrun) then
        echo "ERROR: mruntype must be either jackkife or perrun"
        exit 1;
      endif
      breaksw

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

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

    case "-verbose":
      set verbose = 1;
      set func2roiargs = ($func2roiargs -verbose);
      breaksw

    case "-echo":
      set echo = 1;
      set func2roiargs = ($func2roiargs -echo);
      breaksw

    case "-debug":
      set verbose = 1;
      set echo = 1;
      set func2roiargs = ($func2roiargs -debug);
      breaksw

    case "-cwd":
    case "-nolog":
      breaksw

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

    default:
      set func2roiargs = ($func2roiargs $flag);
      breaksw
  endsw

end

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

############--------------##################
check_params:
  if($#mruntype == 0) then
    echo "ERROR: must specify an mruntype"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo "USAGE: func2roi-sess-mrun"
  echo "Options:";
  echo "   -mruntype type : jackknife or perrun"
  echo " All other options are the same as func2roi-sess"
exit 1;
