#!/bin/tcsh -f

# normalizes for ICV

set Debug = 0;

set subjects = ();
set segmentations = ();
set statsfilepath = ();
set tmpSegValsFile = /tmp/gparcmeannorm$$ 
cp /dev/null $tmpSegValsFile

# ----------------------
if ($#argv == 0) goto usage_err;

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:
# ----------------------







##***********ENTER YOUR VARIABLES***********##
##set up your subjects here - use variable from subjects,csh or list IDs

if ($Debug) then 
    echo ""
    echo "subjects are $subjects"
    echo ""
    echo "segmentations are $segmentations"
    echo ""
    echo "statsfilepath is $statsfilepath"
    echo ""
endif # if ($Debug) then



### START FOREACH SEGNAME, then SUBJECT
foreach name ($segmentations)
foreach s ($subjects)

    set statsfile = $SUBJECTS_DIR/$s/$statsfilepath
    if (! -e $statsfile) then
	    echo "$statsfile for $s does not exist ..."
	    continue;
    endif


    ###############################################
    # check to see if name exists
    set matchline = `cat $statsfile | egrep -c -w "[[:space:]]${name}[[:space:]]"`
	#echo "cat $statsfile | egrep -c -w [[:space:]]${name}[[:space:]]"
    if ($matchline == 0) then
	set namevol = "."
    else if ($matchline == 1) then
	set namevol = (`cat $statsfile | egrep -w "[[:space:]]${name}[[:space:]]" | gawk '{print $4}'`)
    else
	echo "$matchline matches found for name $name... exiting ..."
	exit;
    endif
					    
    # get ICV
    set icv = `$RECON_CHECKER_SCRIPTS/gnicv ${s}`   
    set namevolnorm = `echo "scale=9;$namevol / $icv * 100" | bc`

    echo "$namevolnorm" >> $tmpSegValsFile


end
end

set parcmeannorm = `cat $tmpSegValsFile | gawk -f $RECON_CHECKER_SCRIPTS/gavg.awk`
set parcstdnorm = `cat $tmpSegValsFile | gawk -f $RECON_CHECKER_SCRIPTS/gstd.awk`

echo "$parcmeannorm +/- $parcstdnorm"


exit 0;









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

  set flag = $argv[1]; shift;

  switch($flag)

    case "-s":
        if ( $#argv == 0) goto arg1err;
        set proceed = 1;
        while ( $#argv != 0 && $proceed )
            set subjects = ($subjects $argv[1]); shift;
            if ( $#argv != 0 ) then
                set proceed = `echo "$argv[1]" | gawk '{ if (substr($1, 1, 1) == "-") {print "0"} else {print "1"} }'`;
            endif
        end
        #echo "$subjects"
        #echo ""
      breaksw

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

    case "-lf":
    case "-log":
      set log = $argv[1]; shift;
      breaksw

    case "-statsfilepath":
      set statsfilepath = $argv[1]; shift;
      breaksw

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

    case "-segs":
    case "-segmentations":
	if ( $#argv == 0) goto arg1err;
        set proceed = 1;
        while ( $#argv != 0 && $proceed )
            set segmentations = ($segmentations $argv[1]); shift;
            if ( $#argv != 0 ) then
                set proceed = `echo "$argv[1]" | gawk '{ if (substr($1, 1, 1) == "-") {print "0"} else {print "1"} }'`;
            endif
        end
        #echo "$segmentations"
        #echo ""
    breaksw

    case "-segsf":
    case "-segsfile":
      if ( $#argv == 0) goto arg1err;
      set segmentations = `cat $argv[1]`; shift;
      breaksw

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

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


############--------------##################
check_params:
    if($#subjects == 0) then
        echo "ERROR: must specify a subject name"
        exit 1;
    endif

    if($#segmentations == 0) then
        echo "ERROR: must specify a segmentation name (ex. Left-Hippocampus)"
	echo "exactly as it appears in the aseg.stats file"
	echo "use flag -segmentations"
	echo ""
        exit 1;
    endif

    if($#statsfilepath == 0) then
        echo "ERROR: must specify a stats file path"
	echo "give the path from SUBSDIR/SUBJID/<path/to/stats/file>"
	echo "use the flag -statsfilepath"
	echo "example path:"
	echo "-statsfilepath stats/aseg.stats"
        exit 1;
    endif

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

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

############--------------##################
usage_err:
  echo ""
echo "USAGE:"
echo ""
  echo "-s <list of subjects>"
  echo "-sf <file which has list of subjects>"
  echo "-segs -segmentations <list of segmentations on which to report values>"
  echo "-segsf -segsfile <file which has list of segmentations>"
  echo "-statsfilepath <path of file where stats are; starts from SUBSDIR/SUB/>"
  echo ""
  echo ""
  echo "example:"
  echo ""
  echo "$RECON_CHECKER_SCRIPTS/gparcmeannorm \ "
  echo "    -s s1 s2 s3 s4 \ "
  echo "    -statsfilepath stats/aseg.stats \ "
  echo "    -segs Right-Caudate"
  echo ""
  echo ""
  echo ""

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