#!/bin/csh -f
set VERSION = '$Id$';
set inputargs = ($argv);

set nloop      = ();
set nclustfile = ();
set nsmooth    = ();
set trgsubj    = ();
set dof        = ();
set thmin      = ();
set thmax      = ();
set thsign     = ();
set minarea    = ();
set tmpdir     = ();
set hemi = lh;

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

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

@ nsubjects = $dof + 1;

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

if($#tmpdir == 0) set tmpdir = `dirname $nclustfile`/surfclustpdf-tmp;
mkdir -p $tmpdir

##### Create a log file ######
set LF = $nclustfile.log
rm -f $LF

echo "surfclustpdf log file" >> $LF
echo $VERSION >> $LF
id            >> $LF
pwd           >> $LF
echo $0         >> $LF
echo $inputargs >> $LF
uname -a        >> $LF
date            >> $LF

set StartDate = `date`;

# Create the temp gdf file
set gdf = $tmpdir/tmp.gdf
rm -f $gdf
echo "GroupDescriptorFile 1" >> $gdf
echo "Class Class1"          >> $gdf
@ nthsubj = 1;
while($nthsubj <= $nsubjects)
  echo "Input $trgsubj Class1" >> $gdf
  @ nthsubj = $nthsubj + 1;
end

rm -f $nclustfile
@ nthtrial = 1;
while($nthtrial <= $nloop)

  set seed = `date | awk '{print $4}' | sed 's/\://g'`;

  echo "-------------------------------------------------" >> $LF
  echo $nthtrial $seed `date` >> $LF

  set cmd = (mris_glm --allowsubjrep --fsgd $gdf --hemi $hemi\
         --gcv 1 --surfmeas thickness --synth $seed \
         --trgsubj $trgsubj --sigt $tmpdir/sigt bfloat );
  if($#nsmooth != 0) set cmd = ($cmd --nsmooth $nsmooth)
  echo $cmd >> $LF
  $cmd  >> $LF
  if($status) exit 1;
    

  set cmd = (mri_surfcluster --hemi $hemi --src $tmpdir/sigt bfloat \
             --srcsubj $trgsubj --thmin $thmin --minarea $minarea);
  if($#thmax  > 0) set cmd = ($cmd --thmax  $thmax)
  if($#thsign > 0) set cmd = ($cmd --thsign $thsign)
  set cmd = ($cmd --sum $tmpdir/sum.tmp);

  echo $cmd >> $LF
  $cmd  >> $LF
  if($status) exit 1;

  set nc = `cat $tmpdir/sum.tmp | awk '{if($1 == "NClusters") print $2}'`;
  echo $nthtrial $nc `date` | tee -a $LF

  echo "$nc $seed" >> $nclustfile

  @ nthtrial = $nthtrial + 1;
end

set EndDate = `date`;

echo " "
echo "Started at $StartDate"
echo "Ended   at $EndDate"
echo " "
echo "Done"
echo " "

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

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

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

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

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

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

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

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

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

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

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

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

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

    case "-help":
      goto help;
      breaksw

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

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

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

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

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

end

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

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

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

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

  if($#trgsubj == 0) then
    echo "ERROR: no target subject specified"
    exit 1;
  endif

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

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

  if($#minarea == 0) then
    echo "ERROR: no min area specified"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: surfclustpdf"
  echo ""
  echo "Required Arguments:";
  echo "   -o filename : output file with number of clusters"
  echo "   -nloop nloop : number of simulation trials"
  echo "   -smoothsteps nsteps"
  echo "   -trgsubj targetsubject"
  echo "   -dof dof"
  echo "   -thmin   thmin"
  echo "   -thmax   thmax (default is infinite)"
  echo "   -thmsign thsign (<abs>,pos,neg)"
  echo "   -minarea minarea (mm^2)"
  echo ""
  echo "Optional Arguments:";
  echo "   -tmpdir dir : default is subdir of output file"
  echo "   -help"

  if($PrintHelp) \
  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

