#! /bin/tcsh -f

#
# xsanatreg 
#
# cross-session anatomical registration.  This is a front end
# for minctracc.  The inputs are directories pointing to COR volumes.
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:15 $
#    $Revision: 1.6 $
#
# 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: xsanatreg,v 1.6 2007/01/06 00:01:15 nicks Exp $';

set src      = ();
set srcminc  = ();
set targ     = ();
set targminc = ();
set cleanup  = 1;
set xfm  = ();
set tmpdir = ();
set MTArgs = (); # minctracc args

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

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

goto parse_args;
parse_args_return:

## Set up the tmp directory ##
if($#tmpdir == 0) then
  set tmpdir = /tmp
else
  mkdir -p $tmpdir
endif

goto check_params;
check_params_return:

## Convert the source volume to minc ##
if($#srcminc == 0)   set srcminc = $tmpdir/src-$$.mnc
if(! -e $src.mgz) then
  echo "INFO: Converting $src to $srcminc"
  mri_convert $src $srcminc
else
  echo "INFO: Converting $src.mgz to $srcminc"
  mri_convert $src.mgz $srcminc
  if($status) exit 1;
endif

## Convert the target volume to minc ##
if($#targminc == 0) set targminc = $tmpdir/targ-$$.mnc
if(! -e $targ.mgz) then
  echo "INFO: Converting $targ to $targminc"
  mri_convert $targ $targminc
else
  echo "INFO: Converting $targ.mgz to $targminc"
  mri_convert $targ.mgz $targminc
endif

## run minctracc to get intersession registration ##
minctracc $MTArgs $srcminc $targminc $xfm
set mtstatus = $status;

## cleanup ##
if($cleanup) then
  echo "INFO: cleaning up temporary files"
  rm -f $srcminc
  rm -f $targminc
endif

if($mtstatus)  echo "ERROR: minctracc exited with $mtstatus"

exit $mtstatus;
###############################################


############--------------##################
parse_args:

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

  set flag = $argv[1]; shift;

  switch($flag)

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

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

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

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

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

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

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

    case "-nocleanup":
      set cleanup = 0;
      breaksw

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

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

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

    default:
      set MTArgs = ($MTArgs $1); shift;
      exit 1
      breaksw
  endsw

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

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

  if($#src == 0) then
    echo "ERROR: must supply source volume";
    exit 1;
  endif

  if(! -r $src) then
    echo "ERROR: source volume $src is not readable";
    exit 1;
  endif

  if($#targ == 0) then
    echo "ERROR: must supply target volume";
    exit 1;
  endif

  if(! -r $targ) then
    echo "ERROR: target volume $targ is not readable";
    exit 1;
  endif

  if($#xfm == 0) then
    echo "ERROR: must supply output xfm file name";
    exit 1;
  endif
  set xfmdir = `dirname $xfm`;
  mkdir -p $xfmdir
  if(! -w $xfmdir) then
    echo "ERROR: cannot write to $xfmdir":
    exit 1;
  endif

  if(! -w $tmpdir) then
    echo "ERROR: cannot write to $tmpdir":
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo "USAGE: xsanatreg"
  echo "   -src      srccordir  : directory of source COR volume (required)"
  echo "   -targ     targcordir : directory of target COR volume (required)"
  echo "   -xfm      file       : file in which to store transform  (required)"
  echo "   -tmpdir   dir        : directory for temporary storage (/tmp)"
  echo "   -srcminc  file       : file name for source minc (auto)"
  echo "   -targminc file       : file name for target minc (auto)"
  echo "   -nocleanup           : do not delete temporary minc files"
  echo "   -version             : print version and exit"
  echo "   -umask umask    "
  echo "   minctracc options"
exit 1;
