#! /bin/tcsh -f

#
# mri_mergelabels
#
# REPLACE_WITH_ONE_LINE_SHORT_DESCRIPTION
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: greve $
#    $Date: 2007/08/10 20:01:57 $
#    $Revision: 1.4 $
#
# 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
#

# evspatfil - front end for fast_evspatfil.m to filter
# a functional data set by projecting out the eigenvectors
# as computed from evfunc (fast_evfunc.m)
#
set VERSION = '$Id: mri_mergelabels,v 1.4 2007/08/10 20:01:57 greve Exp $'
set inputargs = ($argv);

set PWDCMD = `getpwdcmd`;

set inputlist = ();
set outlabel  = ();

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

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

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set outdir = `dirname $outlabel`;
mkdir -p $outdir

# Count the total number in the label #
set nlabelsum = 0;
foreach inlabel ($inputlist)
  set nlabel = `head -2 $inlabel | tail -1`;
  set nlabelsum = `echo "$nlabelsum + $nlabel" | bc -l`;
end

echo "#label , from subject ??" > $outlabel
echo $nlabelsum >> $outlabel

# Cat the data into the label #
foreach inlabel ($inputlist)
  tail +3 $inlabel >> $outlabel
end

echo "Done"

exit 0;

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

  set flag = $argv[1]; shift;

  switch($flag)

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

    case "-i"
      if ( $#argv == 0) goto arg1err;
      set input = $argv[1];shift;
      if(! -e $input) then
        echo "ERROR: $input does not exist"
        exit 1;
      endif
      set inputlist = ($inputlist $input);
      breaksw

    case "-d"
      if ($#argv == 0) goto arg1err;
      set d = $argv[1];shift;
      if(! -e $d) then
        echo "ERROR: $d does not exist"
        exit 1;
      endif
      set inputlist = ($inputlist `ls $d/*.label`);
      breaksw

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

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

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

    default:
      echo "ERROR: $flag not regocnized"
      exit 1;
      breaksw
  endsw

end

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

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

  if($#inputlist < 2) then
    echo "ERROR: not enough inputs specified"
    exit 1;
  endif

  if($#outlabel == 0) then
    echo "ERROR: no output specified"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: mri_mergelabels"
  echo ""
  echo "   -i label1 -i label2 ..."
  echo "   -o outputlabel"
  echo "   -d dir : all labels in directory dir"
  echo ""
  echo "This is a simple script that will merge two or more label"
  echo "files. It does this by catting the label files together"
  echo "(after removing the first two lines). It inserts a new"
  echo "header (the first two lines). The number of entries in"
  echo "the new file (ie, the number on the second line), is "
  echo "computed by summing those from the input files."
  echo ""
exit 1;
