#! /bin/csh -f

#
# func2sph-sess-vss - this corresponds to func2sph-sess version 1.6 (01/1/19). 
# It uses the old vss (vertex-sampled space) libraries and functions which
# have now been replaced. This version can be accessed from func2sph-sess
# by including a "-old" flag.
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:17 $
#    $Revision: 1.3 $
#
# 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: func2sph-sess-vss,v 1.3 2007/01/09 22:41:17 nicks Exp $'

set inputargs = "$argv";
set analysis    = ();
set ScriptOnly = 0;
set umaskarg = ();
set hemilist = ();
set build_funcvit = 1;
set paintsurf = ();
set nolog = 0;
set sphdir = sph

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/func2sph-sess-vss.log
  if(-e $LF) mv $LF $LF.old
else
  echo "No log file"
  set LF = /dev/null
endif

echo "----------------------------------------------------------"
echo "func2sph-sess-vss logfile is $LF"
echo "----------------------------------------------------------"

echo "fucn2sph-sess log file" >> $LF
echo $VERSION >> $LF
pwd           >> $LF
echo "$0"     >> $LF
echo "$inputargs"  >> $LF
uname -a      >> $LF
date          >> $LF
which mri-func2sph >> $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:

### Build the functional vertex interpolation tables ####
if($build_funcvit) then
  echo "Building functional VITs" | tee -a  $LF
  set cmd = "funcvits-sess $inputargs"
  pwd | tee -a  $LF
  echo $cmd | tee -a  $LF
  $cmd  | tee -a  $LF
  if($status) then
    echo "ERROR: funcvits-sess"  | tee -a  $LF
    exit 1;
  endif
else
  echo "NOT Building functional VITs" | tee -a  $LF  
endif

## 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}'`;

####### Go through each session ######
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

  ## Get the vertex interpolation table directory ##
  set fvitdir  = fvit
  if(! -e $fvitdir ) then
    echo "ERROR ($sess): fvit dir $fvitdir does not exist" |& tee -a $LF
    exit 1;
  endif

  ## Prepare the script, if need be ##
  if($ScriptOnly) then
    if(! -d scripts) mkdir scripts
    set scriptfile = scripts/run-func2sph-$analysis
    rm -f $scriptfile
    touch $scriptfile
    echo "#\!/bin/csh -f" >> $scriptfile
    echo "cd .." >> $scriptfile
    chmod a+x $scriptfile
  endif

  set funcstem = $analysis/h

  foreach hemi ($hemilist)
    ## Create the command line ###
    set sphstem  = $analysis/$sphdir/h-$hemi
    set cmd = "mri-func2sph -hemi $hemi $umaskarg -i $funcstem -o $sphstem -fvitdir $fvitdir"
    pwd |& tee -a $LF
    echo "$cmd" |& tee -a $LF
    if(! $ScriptOnly) then
      $cmd |& tee -a $LF
      if($status) then
        echo "ERROR ($sess): mri_func2psh failed" |& tee -a $LF
        exit 1;
      endif
    else
      echo "$cmd" >> $scriptfile
      echo "\n\n" >> $scriptfile
    endif
  end

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

end

echo "-----------------------------------------------------" |& tee -a $LF

date | tee -a $LF
echo "func2sph-sess-vss 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 "-hemi":
      if ( $#argv == 0) goto arg1err;
      set hemi = $argv[1]; shift;
      if($hemi != rh && $hemi != lh) then
        echo "ERROR: hemi must be either lh or rh ($hemi)"
        exit 1;
      endif
      set hemilist = ($hemilist $hemi)
      breaksw

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

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

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

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

    case "-nofuncvit":
    case "-nofuncvits":
      set build_funcvit = 0;
      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

    # these are ignored #
    case "-old":
    case "-vss":
    case "-cwd":
    case "-nolog":
      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 sxa name specified"|& tee -a $LF 
     exit 1
  endif

  if ($#hemilist == 0) set hemilist = (lh rh);

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

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

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

############--------------##################
usage_exit:
  echo "USAGE: func2sph-sess-vss"
  echo "Options:";
  echo "   -analysis analysisname : session-level functional analysis name"
  echo "   -hemi hemilist : lh, rh, or lh rh"
  echo "   -sf sessidfile  ..."
  echo "   -df srchdirfile ..."
  echo "   -s  sessid      ..."
  echo "   -d  srchdir     ..."
  echo "   -sphdir dir    : sphere subdirectory (sph)"
  echo "   -nofuncvit     : do not rebuild the functional vits"
  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;
