#!/usr/bin/tcsh -ef


set inputargs = ($argv);
set AppendLog    = 1;
set AppendStatus = 1;
set LF_DEFAULT_NAME = recon-all-exvivo.log;
set LF = ()
set echo=1

if ($#argv == 0) then
   echo "usage: $0 -s <subject> [-[lr]h -nocerebellum>]"
   echo make sure to set the SUBJECTS_DIR first
   exit 1 ;
endif


set use_samseg = 0
set norecon=0
set cerebellum = ()
set opts=()
set run_bet = 0 ;
set mask_vol = 0
unset hemi
set cmdline = ($argv);
while( $#argv != 0 )

  set flag = $argv[1]; shift;
  switch($flag)
    case "-no-recon":
    case "-no_recon":
    case "-norecon":
       set norecon=1 ;
       breaksw ;
    case "--samseg":
    case "-samseg":
      if ( $#argv < 2) goto arg1err;
	set samseg_fname = $1;shift;
	set vol_fname = $1;shift;
	set use_samseg = 1
	breaksw
    case "--s":
    case "-subject":
    case "-s":
      if ( $#argv < 1) goto arg1err;
      set s=$1; shift;
      breaksw
    case "-lh":
    case "-LH":
       set hemi=lh; 
       breaksw
    case "-sd":
      if ( $#argv < 1) goto arg1err;
      setenv  SUBJECTS_DIR $argv[1]; shift;
      breaksw

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

    case "-help":
    case "--help:"
       echo "usage: $0 -s <subject> [-[lr]h -nocerebellum>]"
       echo make sure to set the SUBJECTS_DIR first
       echo make sure to set the SUBJECTS_DIR first
       exit 1 ;
       breaksw ;
     case "-mask":
	set mask_vol = 1
	set mask_fname = $argv[1]; shift;
       breaksw ;
     case "-run_bet":
     case "-runbet":
     case "-bet":
       set run_bet = 1 ;
       breaksw ;
     case "-nobet":
       set run_bet = 0 ;
       breaksw ;
     case "-renorm":
       set ifile=$argv[1]; shift;
       breaksw ;
     case "-fluidthresh"
       setenv fluidthresh $argv[1]; shift;
       breaksw 



     case "-i":
       set base=$argv[1]; shift;
       breaksw 
# these are all pass through commands with a single argument
     case "-openmp":
       if ( $#argv < 1) goto arg1err;
       set opts = ($opts $flag $1)
       shift;
       breaksw ;
     case "-no-wsatlas":
     case "-no-wgcasatlas":
     case "-wsatlas":
     case "-wsthresh":
       if ( $#argv < 1) goto arg1err;
       set opts = ($opts $flag $1)
       breaksw ;
# these are all pass through commands with no argument
       default: 
    case "-no-cerebellum":
    case "-nocerebellum":
    case "-NO-CEREBELLUM":
    case "-NOCEREBELLUM":
    case "-notalairach":
    case "-noskullstrip":
       set opts = ($opts $flag)
       breaksw
  endsw
end

set sd=$SUBJECTS_DIR/$s
if (! $?base ) then
  set base=$SUBJECTS_DIR/$s
endif

if (-e $sd == 0) then
    echo creating subject directory tree
    mksubjdirs $s
endif

# ------------ Create the log file --------------- #
if($#LF == 0) then
  set LF = ($sd/scripts/$LF_DEFAULT_NAME)
  if(-e $LF) then
    ls -l $LF
    if(! $AppendLog) then
      mv -f $LF $LF.old
    else
      # if running using -make, then dont bother with repeated info dumps
      echo "\n\n"  >> $LF
      echo "New invocation of recon-all-exvivo "  >> $LF
      echo "\n\n"  >> $LF
    endif
  else
    touch $LF
  endif
else
  if(-e $LF) then
    echo "\n\n"  >> $LF
    echo "New invocation of recon-all-exvivo "  >> $LF
    echo "\n\n"  >> $LF
  endif
endif
date >> $LF
echo $PWD >> $LF
echo $0 >> $LF
echo $inputargs >> $LF

setenv base $SUBJECTS_DIR/$s
set mdir=$sd/mri
set odir=$mdir/orig
set sdir=$sd/scripts
set ldir=$sd/label
echo recon-all-exvivo $cmdline > $sdir/recon-all-exvivo.cmd

if ( $?ifile ) then
  rsync -auv $ifile $sdir/label_intensities.txt |& tee -a $LF
endif

if (-e $sdir/label_intensities.txt & ! $?renorm) then
    set renorm = (-renorm $sdir/label_intensities.txt)
endif

set xfile = $sdir/expert.opts
if ($?hemi) then
if 0 then
    echo "mri_ca_register $renorm -${hemi}" > $xfile
    echo "mri_ca_normalize $renorm -${hemi}" >> $xfile
    echo "mri_em_register $renorm  -${hemi}" >> $xfile
    echo "mri_ca_label $renorm -${hemi}" >> $xfile
    echo "mri_cc -${hemi}" >> $xfile
    echo "mri_aparc2aseg --${hemi}" >> $xfile
    echo "mris_volmask --${hemi}-only" >> $xfile
    echo "mris_anatomical_stats -noglobal" >> $xfile
    echo "mri_relabel_hypointensities -${hemi}" >> $xfile
    echo "mri_segstats --no-global-stats" >> $xfile
endif
    set opts = ($opts -notalairach -${hemi})
#    set opts = ($opts -${hemi} -notalairach)
endif
if ($?renorm) then
    echo renorm = $renorm
    echo "mri_ca_register $renorm" > $xfile
    echo "mri_ca_normalize $renorm" >> $xfile
    echo "mri_em_register $renorm " >> $xfile
    echo "mri_ca_label $renorm" >> $xfile
#    set opts = ($opts -${hemi} -notalairach)
endif
if (-e ${xfile}) then
    set opts = ($opts -xopts-overwrite -expert ${xfile})
endif


if ($use_samseg) then
    if ($?hemi) then
    else
	set hemi=both
    endif
    make_exvivo_filled $s $samseg_fname $vol_fname $hemi
    recon-all -hires -s $s  -autorecon2-wm -autorecon3 -notalairach $opts -balabels
    exit 0
endif

mkdir -p $odir/norm
if (-e $odir/flash20.mgz) then
  set bvol = $odir/flash20.mgz
else if (-e $odir/flash20*.mgz) then
  set bvol = `ls -1 $odir/flash20*.mgz`
else
  set files = (`ls -1 $odir/*.mgz`)
  set bvol = $files[1]
endif

mri_compute_bias -c $ldir/../tmp/control.dat -sigma 8 -label $ldir/wm.label $bvol $odir/bias.mgz|& tee -a $LF
foreach f ($odir/*.mgz)
    set fonly = ${f:t}
    set fonly = ${fonly:r}
    if ($fonly == 001) then
	continue ;
    endif
    if ($fonly == bias) then
	continue ;
    endif
    if ($fonly == opt) then
	continue ;
    endif
    if ($fonly == fluid_mask) then
	continue ;
    endif
    if ($fonly == brainmask) then
	continue ;
    endif
    mri_apply_bias $f $odir/bias.mgz $odir/norm/${fonly}.corrected.mgz|& tee -a $LF
end

compute_label_vals.csh|& tee -a $LF
matlab < $FREESURFER_HOME/matlab/compute_opt_volume.m|& tee -a $LF
mri_convert $odir/fluid_mask.mgz $odir/fluid_mask.nii.gz|& tee -a $LF
if ($mask_vol) then
    mri_mask $odir/opt.mgz ${mask_fname} $odir/opt.mgz|& tee -a $LF
endif

if ($run_bet) then
  bet $odir/fluid_mask.nii.gz $odir/brainmask.nii.gz|& tee -a $LF
  mri_convert $odir/brainmask.nii.gz $odir/brainmask.mgz|& tee -a $LF
  mri_mask $odir/opt.mgz $odir/brainmask.mgz $odir/opt.mgz|& tee -a $LF
endif
cp $odir/opt.mgz $odir/001.mgz|& tee -a $LF
mri_convert -ns 1 --conform $odir/brainmask.mgz $mdir/brainmask.mgz|& tee -a $LF
cp  $mdir/brainmask.mgz $mdir/brainmask.auto.mgz|& tee -a $LF


# always do the following
if (-e $mdir/../tmp/control.dat) then
    set opts = ($opts  -canorm-usecps) ;
endif

if ($norecon) exit 0

recon-all -s $s -sd $SUBJECTS_DIR $opts

exit 0

if 0 then
    echo "mri_ca_register  -${hemi}" > xfile
    echo "mri_ca_normalize -${hemi}" >> xfile
    echo "mri_em_register  -${hemi}" >> xfile
    echo "mri_ca_label   -${hemi}" >> xfile
    echo "mri_cc -${hemi}" >> xfile
    echo "mri_aparc2aseg --${hemi}" >> xfile
    echo "mris_volmask --${hemi}-only" >> xfile
    echo "mris_anatomical_stats -noglobal" >> xfile
    echo "mri_relabel_hypointensities -${hemi}" >> xfile
    echo "mri_segstats --no-global-stats" >> xfile
endif


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

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

#-------------------------------------------------#
arg3err:
#-------------------------------------------------#
  echo "ERROR: flag $flag requires three arguments"
  exit 1

