#!/bin/csh -f
# spmregister-sess
#
set VERSION = '$Id: spmregister-sess,v 1.6 2003/11/14 23:43:27 greve Exp $';
set inputargs = ($argv);
set DateString = "`date '+%y%m%d%H%M'`"

set fsd          = bold;
set asd          = 3danat;
set samesessanat = 0;
set nolog        = 0;
set Run          = ();
set fsvol        = brain;
set regfilename  = register.dat
set funcstem     = f;

## If there are no options, just print the usage ##
if($#argv == 0) goto usage_exit;

## Look for version option ##
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) then
  set nolog = 1;
endif

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

echo "--------------------------------------------------------------"
echo "spmregister-sess logfile is $LF"
echo "--------------------------------------------------------------"

echo "spmregister-sess log file" >> $LF
echo $VERSION   >> $LF
which spmregister >> $LF

goto parse_args;
parse_args_return:

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

goto check_params;
check_params_return:

## Loop through each session ##
foreach sess ($SessList)

  echo " " |& tee -a $LF
  echo "Session: $sess ----------------" |& tee -a $LF
  date  |& tee -a $LF
  pushd $sess > /dev/null

  ## Anatomical directory is in the database ##
  if(! -e subjectname) then
    echo "ERROR: file 'subjectname' does not exist in $sess" |& tee -a $LF
    exit 1;
  endif
  set subject = `cat subjectname`;
  if(! -e $SUBJECTS_DIR/$subject) then
    echo "ERROR: $subject is not in SUBJECTS_DIR" |& tee -a $LF
    echo "  SUBJECTS_DIR is $SUBJECTS_DIR" |& tee -a $LF
    exit 1;
  endif

  ## Make sure the functional subdirectory exists ##
  if(! -e $fsd) then
    echo "ERROR: $fsd does not exist" |& tee -a $LF
    exit 1;
  endif

  if($#Run == 0) then
    set funcdir = `getfirstrundir-sess $fsd`;
    if($status) then
      echo "$funcdir" |& tee -a $LF
      exit 1;
    endif
  else
    set funcdir = $fsd/$Run
    if(! -e $funcdir) then
      echo "ERROR: $Run does not exist in $fsd" |& tee -a $LF
      exit 1;
    endif
  endif

  set movfile = $funcdir/$funcstem"_000.bshort"
  if(! -e $movfile) then
    set movfile = $funcdir/$funcstem"_000.bfloat"
    if(! -e $movfile) then
       echo "ERROR: cannot find f volume in $funcdir" |& tee -a $LF
       exit 1;
    endif
  endif

  set regfile = $fsd/$regfilename
  set cmd = (spmregister --s $subject --mov $movfile \
    --reg $regfile --fsvol $fsvol)
  $cmd |& tee -a $LF
  if($status) then 
    echo "ERROR: spmregister failed" |& tee -a $LF
    exit 1;
  endif

  popd > /dev/null

end  # foreach sess ($SessList)

date |& tee -a $LF
echo "spmregister-sess completed " |& tee -a $LF

exit 0;

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

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

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

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

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

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

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

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

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

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

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

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

    case "-cwd":
      breaksw

    case "-s":
    case "-sf":
    case "-df":
    case "-d":
    case "-g":
      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

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

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

############--------------##################
usage_exit:
  echo "USAGE: spmregister-sess"
  echo ""
  echo "   -sf sessidfile  ..."
  echo "   -df srchdirfile ..."
  echo "   -s  sessid      ..."
  echo "   -d  srchdir     ..."
  echo "   -fsd dir       : functional subdirectory ($fsd)"
  echo "   -run RRR       : register to run RRR"
  echo "   -funcstem stem : default is f"
  echo "   -fsvol volid   : FreeSurfer vol id (default is $fsvol)"
  echo "   -regfile file  : Save reg as file instead of register.dat"
  echo "   -umask umask   : set unix file permission mask"
  echo "   -version       : print version and exit"
  echo ""
exit 1;
