#!/bin/csh -ef
# original by Vasanth Pappu

set subjects = ();
set Details = 0;
set missing_outputfiles = 0;
goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

# ---
echo "In output_file_checker"
if ($Details) echo "SUBJECTS_DIR is $SUBJECTS_DIR "
if ($Details) echo ""
#sleep 2;

foreach s ($subjects)

pushd $SUBJECTS_DIR/$s > /dev/null

# first find out the number of 0??.mgz files
#set zerozeroMGZfiles = `ls -1rt mri/orig/0??.mgz`
set zerozeroMGZfiles = `ls -1rt $SUBJECTS_DIR/$s/mri/orig/* | egrep "0[0-9][0-9].mgz"`


# then attach rest of files
set files69 = (mri/rawavg.mgz mri/orig.mgz mri/nu.mgz mri/transforms/talairach.auto.xfm mri/transforms/talairach.xfm mri/T1.mgz mri/brainmask.auto.mgz mri/brainmask.mgz mri/transforms/talairach.lta mri/norm.mgz mri/transforms/talairach.m3z mri/transforms/talairach.m3z.inv.x.mgz mri/transforms/talairach.m3z.inv.y.mgz mri/transforms/talairach.m3z.inv.z.mgz mri/nu_noneck.mgz mri/transforms/talairach_with_skull.lta mri/aseg.auto.mgz mri/aseg.mgz stats/aseg.stats mri/brain.mgz mri/brain.finalsurfs.mgz mri/wm.seg.mgz mri/wm.asegedit.mgz mri/wm.mgz mri/filled.mgz scripts/ponscc.cut.log surf/lh.orig.nofix surf/lh.smoothwm.nofix surf/lh.inflated.nofix surf/lh.qsphere.nofix surf/lh.orig surf/lh.white surf/lh.curv surf/lh.area surf/lh.pial surf/lh.thickness surf/lh.smoothwm surf/lh.sulc surf/lh.inflated mri/lh.ribbon.mgz surf/lh.sphere surf/lh.sphere.reg surf/lh.rh.sphere.reg surf/lh.avg_curv label/lh.aparc.annot stats/lh.aparc.stats label/lh.aparc.a2005s.annot surf/rh.orig.nofix surf/rh.smoothwm.nofix surf/rh.inflated.nofix surf/rh.qsphere.nofix surf/rh.orig surf/rh.white surf/rh.curv surf/rh.area surf/rh.pial surf/rh.thickness surf/rh.smoothwm surf/rh.sulc surf/rh.inflated mri/rh.ribbon.mgz surf/rh.sphere surf/rh.sphere.reg surf/rh.lh.sphere.reg surf/rh.avg_curv label/rh.aparc.annot stats/rh.aparc.stats label/rh.aparc.a2005s.annot mri/aparc+aseg.mgz) 
# original output_file_order_file: /space/boba/1/users/nrecon/scripts//output_file_order_file1
#set output_file_order_file = /space/boba/1/users/nrecon/scripts/output_file_order_file1
echo "checking if output file order file is set..."
if( $?output_file_order_file ) then
    set files_in_order = (`cat $output_file_order_file`);
else
    echo "output_file_order_file not specified, using default order"
    set files_in_order = (`echo $files69`)
endif

set files70plus = "${zerozeroMGZfiles} $files_in_order"
if ($Details) echo "checking these files: "
if ($Details) echo "${files70plus}"

# list the files/check to see all files exist
# check to see if all files exist
echo "check to see if all files exist..."
set commonOrder = ($files70plus)
foreach f ($commonOrder)
    if (! -e $f) then
	echo "$s missing output files"
	if ($Details) echo "$s has no $f"
	set missing_outputfiles = 1;
	goto theend;
    endif 
end 
set realOrder = `ls -1rt $files70plus`


# check if both lists are same size
if ($#realOrder == $#commonOrder) then 
    if ($Details) echo "found $#realOrder files ..."
else
    echo "ERROR: some files are missing."
    exit 1;
endif



# check list against common order
set ok = 1;
set x = 1;
set nextx = 2;
@ realOrder_plus1 = $#realOrder + 1;
if ($Details) echo "realOrder_plus1 is $realOrder_plus1"
#foreach x (`seq 1 $#realOrder`)
while ($x < $realOrder_plus1)
    #if ($Details) echo "x is $x" 
    #sleep 1;
    #echo "looking at commonOrder[$x] is $commonOrder[$x]"
    if ($realOrder[$x] == $commonOrder[$x]) then
	if ($Details) echo "$realOrder[$x] last modified `stat -c %y $realOrder[$x]`"
	@ x = $x + 1; @ nextx = $nextx + 1;
	continue;
    # some exceptions (files that are generated in the same step that may have very close date stamps)
    else if ($commonOrder[$x] == "mri/filled.mgz" && $realOrder[$x] == "scripts/ponscc.cut.log" && $commonOrder[$nextx] == "scripts/ponscc.cut.log" && $realOrder[$nextx] == "mri/filled.mgz") then
	if ($Details) echo "$realOrder[$x] last modified `stat -c %y $realOrder[$x]`"
	if ($Details) echo "$realOrder[$nextx] last modified `stat -c %y $realOrder[$nextx]`"
	@ x = $x + 2; @ nextx = $nextx + 2;
	continue;
    else
	if ($Details) echo "ERROR: order is not commmon order: stopped at $realOrder[$x], should be $commonOrder[$x] ..."
	
	@ a = $x - 2; 
	@ z = $x + 4;
	if ($Details) echo "real order:		common order:"
	if ($Details) echo "..........		........."
	if ($Details) echo "..........		........."
	if ($Details) echo "..........		........."
	foreach az (`seq $a $z`)
	    if ($Details) echo "$realOrder[$az]		$commonOrder[$az]"
	end
	if ($Details) echo "..........		........."
	if ($Details) echo "..........		........."
	if ($Details) echo "..........		........."
	set ok = 0;
	break;
    endif
    @ x = $x + 1; @ nextx = $nextx + 1;
end





if ($ok) then
    if ($Details) echo ""
    if ($Details) echo "real file order agrees with common file order"
    echo "$s outputfiles_OK"
else
    echo "$s outputfiles_NOTOK"
endif 

theend:

popd > /dev/null


end










exit 0;


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

  set flag = $argv[1]; shift;

  switch($flag)

    case "-s":
    case "-subjid":
        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
      breaksw

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

    case "-outputfileorderfile"
	set output_file_order_file = $argv[1]; shift;
      breaksw

    case "-details":
	set Details = 1;
      breaksw

    case "-summary":
	set Details = 0;
      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 "USAGE ERROR: must specify a subjid"
        exit 1;
    endif
goto check_params_return;
############--------------##################


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





##############--------------##################
##############--------------##################
usage_err:
  echo "USAGE: flags:"
  echo "    "
  echo "-s"
  echo "-subjid"
  echo "-sf"
  echo "	a file of a list of subjects"
  echo "-outputfileorderfile"
  echo "	the file which has the order of the output files"
  echo "-details"
  echo "-summary"
  exit 1
##############--------------##################
##############--------------##################

