#!/usr/bin/tcsh -f

# Copyright © 2021 The General Hospital Corporation (Boston, MA) "MGH"
#
# Terms and conditions for use, reproduction, distribution and contribution
# are found in the 'FreeSurfer Software License Agreement' contained
# in the file 'LICENSE' found in the FreeSurfer distribution, and here:
#
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
#
# Reporting: freesurfer@nmr.mgh.harvard.edu

set VERSION = 'vertexvol mockbuild-local';

if($?FS_GII == 0) setenv FS_GII ""
set outfile = ();
set subject = ();
set hemi = ();
set TH3Flag = 1;

set tmpdir = ();
set cleanup = 1;
set LF = ();

set inputargs = ($argv);
set PrintHelp = 0;
if($#argv == 0) goto usage_exit;
set n = `echo $argv | grep -e -help | wc -l` 
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif
set n = `echo $argv | grep -e -version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

source $FREESURFER_HOME/sources.csh

goto parse_args;
parse_args_return:
goto check_params;
check_params_return:

set subjdir = $SUBJECTS_DIR/$subject
cd $subjdir/surf
pwd

# Compute mid.area. Not necessary when using TH3, but other programs may 
# need it
set ud = `UpdateNeeded ${hemi}.area.mid$FS_GII ${hemi}.area$FS_GII  ${hemi}.area.pial$FS_GII`
if($ud) then
  set cmd=(mris_calc -o ${hemi}.area.mid$FS_GII ${hemi}.area$FS_GII add ${hemi}.area.pial$FS_GII)
  echo "$cmd"
  $cmd
  if($status) goto error_exit;
  set cmd=(mris_calc -o ${hemi}.area.mid$FS_GII ${hemi}.area.mid$FS_GII div 2)
  echo "$cmd"
  $cmd
  if($status) goto error_exit;
endif

if($TH3Flag == 0) then
  # Compute volume = mid.area * thickness -- not accurate
  set cmd = (mris_calc -o $outfile$FS_GII)
  set cmd = ($cmd ${hemi}.area.mid$FS_GII mul ${hemi}.thickness$FS_GII)
  echo "$cmd"
  $cmd
  if($status) goto error_exit;
  if(-e $subjdir/mri/.xdebug_mris_calc) rm -f $subjdir/mri/.xdebug_mris_calc
  if(-e $subjdir/surf/.xdebug_mris_calc) rm -f $subjdir/surf/.xdebug_mris_calc
else
  # Compute volume as sum of 3 tetrahedra (TH3)
  # Algorithm supplied by Anderson Winkler
  set white = $subjdir/surf/$hemi.white
  set pial = $subjdir/surf/$hemi.pial
  set label = $subjdir/label/$hemi.cortex.label
  set ud = `UpdateNeeded $outfile $white $pial $label`
  if($ud) then
    set cmd = (mris_convert --volume $subject $hemi $subjdir/surf/$outfile)
    echo "$cmd"
    $cmd
    if($status) goto error_exit;
  endif
endif

echo " "
echo "vertexvol Done"

exit 0

###############################################

############--------------##################
error_exit:
date
echo "ERROR: vertexvol"
echo $cmd
exit 1;
###############################################

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

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

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

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

    case "--th3":
      set TH3Flag = 1;
      breaksw

    case "--no-th3":
      set TH3Flag = 0;
      breaksw

    case "--lh":
      set hemi = lh
      breaksw

    case "--rh":
      set hemi = rh
      breaksw

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

    case "--nolog":
    case "--no-log":
      set LF = /dev/null
      breaksw

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

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

    case "--cleanup":
      set cleanup = 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:

if($#subject == 0) then
  echo "ERROR: must spec subject"
  exit 1;
endif
if($#hemi == 0) then
  echo "ERROR: must spec hemi"
  exit 1;
endif

if(! -e $SUBJECTS_DIR/$subject) then
  echo "ERROR: cannot find $subject"
  exit 1;
endif

if($#outfile == 0) set outfile = $hemi.volume$FS_GII

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

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

############--------------##################
usage_exit:
  echo ""
  echo "vertexvol -- computes vertex-wise volume (and mid.area)"
  echo "  --s subject"
  echo "  --lh or --rh"
  echo "  --o outfile : default is ?h.volume"
  echo "  --th3, --no-th3 : use or don't TH3, default is to use" 
  echo ""

  if(! $PrintHelp) exit 1;
  echo $VERSION
  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'
exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP
