#! /bin/csh -f

#
# afni2bvol
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:16 $
#    $Revision: 1.2 $
#
# 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: afni2bvol,v 1.2 2007/01/09 22:41:16 nicks Exp $'

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

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

set inputargs = ($argv);

set inbrik = ();
set outstem = ();
set outext = bfloat;
set mfile = ();
set monly = 0;

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set MATLAB = `getmatlab`;
if($status) exit 1;

## Set path for matlab file ##
if(! $monly) then
  set MLF = "/tmp/a2b_$$.m"
  set QuitOnError = 1;
else
  set MLF = $mfile;
  set QuitOnError = 0;
endif

echo matlab file is $MLF 
rm -f $MLF;
echo "% Matlab file for afni2bvol `date`" >> $MLF
echo "% This file may be deleted "     >> $MLF
echo "% $MLF"                          >> $MLF;
echo "global QuitOnError;"             >> $MLF;
echo "QuitOnError = $QuitOnError;"     >> $MLF;
set c = "";
echo -n "afni2bvol("     >> $MLF;
foreach arg ($inputargs)
  echo -n "$c '$arg'"      >> $MLF;
  set c = ",";
end
echo -n ");" >> $MLF;
echo "if(QuitOnError) quit; end" >> $MLF;

echo "----------- Matlab file --------------" 
cat $MLF 
echo " " 
echo "-----------------------------------" 

if(! $monly ) then
  echo "------------------------------------------" 
  echo "------- matlab output --------------------" 
  cat $MLF | $MATLAB -display iconic 
  echo "------------------------------------------" 
  rm $MLF
endif

bfvcheck $outstem
if($status) then
  echo "ERROR: creating $outstem"
  exit 1
endif

echo "afni2bvol: done"

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

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

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

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

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

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

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

    case "-verbose":
      set verbose ;
      breaksw

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

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

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

end

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


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

  set errs = 0;

  if($#outstem == 0) then
    echo "ERROR: no output stem specified"
    set errs = 1;
  endif

  if($#inbrik == 0) then
    echo "ERROR: no input brik specified"
    set errs = 1;
  endif

  if($errs) then
    echo "ERRORS detected ... aborting"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo "USAGE: afni2bvol"
  echo "Options:";
  echo "   -inbrik afnibrik  : eg, 3d+orig.BRIK "
  echo "   -o bvolstem     : output volume "
  echo "   -oext  extension : <bfloat> or bshort"
  echo "   -version : print version and exit"
exit 1;


