#!/bin/csh -f
# ORIGINAL VERSION BY Jenni Pacheco
# MODIFIED BY David Koh
#	      Louis Vinke
#
# DEPENDENCIES:
#    - Snapshots:
#	- snap_tkmedit.csh $s
#	- snap_tksurfer.csh $s
#	- snap_tkmedit.tcl
#       - snap_tksurfer.tcl
#
#
# CHANGE LOG:
# 2006-06-06: (steph)
#    - added -o argument so the user can specify an output html file.  If -o
#      flag is not used, snapshots.html is output with all images in the 
#      $SUBJECTS_DIR/rgb/snaps directory
# 2006-08-30: (david)
#    - added -overwrite flag so the user can specify to re-take previously
#      captured images
#2006-10-12: (jenni)
#   -i wrote new snapshot scripts, so that it would go faster. I've put those
#    in and taken out the old snapshot scripts.
#
#
#
#
#

#set echo=1

set subjects = ();
# default html output file
set outputFile = ()
set sumlog = ();

set DetailedSnapshots = 0
set overwrite = 0

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

# ---
echo ""
echo "===================="
echo "Snapshots are about to be collected.  Your screen saver should"
echo "be disabled and no windows should be overlapping the tkmedit and"
echo "tksurfer screens that will open.  Ctrl+C now if you are not ready!"
echo "===================="
echo ""

sleep 6

foreach s ($subjects)

    if (! -e $SUBJECTS_DIR/QA/$s/rgb/ ) then
	mkdir -p $SUBJECTS_DIR/QA/$s/rgb/
    endif
    if (! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/ ) then
	mkdir -p $SUBJECTS_DIR/QA/$s/rgb/snaps/
    endif
    echo ""
    echo "     Taking snapshots for subject $s" | tee -a $sumlog
    
    set takeSnaps = 0 
    # tkmedit   
    foreach reg (aseg-cor1 aseg-cor2 aseg-cor3 aseg-cor4 aseg-cor5 aseg-cor6 aseg-cor7 aseg-templh aseg-temprh)
	if ( (! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/${s}_${reg}.gif) && (! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/${s}_${reg}.rgb) || ($overwrite == 1) ) then
	    set takeSnaps = 1
	endif
    end
    if($takeSnaps == 1) then
        $RECON_CHECKER_SCRIPTS/snap_tkmedit.csh $s
    else
	echo "      Tkmedit images already exist for subject $s. Skipping this step..." | tee -a $sumlog
	sleep 2
    endif

    #tksurfer
    set takeSnaps = 0
    foreach reg (parc_lh_lat parc_lh_med parc_lh_inf parc_rh_lat parc_rh_med parc_rh_inf)
	if ( (! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/${s}_${reg}.gif) && (! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/${s}_${reg}.rgb) || ($overwrite == 1) ) then
	    set takeSnaps = 1
	endif
    end
    if ($takeSnaps == 1) then
        $RECON_CHECKER_SCRIPTS/snap_tksurfer.csh $s
    else
	echo "      Tksurfer images already exist for subject $s. Skipping this step..." | tee -a $sumlog

	sleep 2
    endif

	if ($DetailedSnapshots == 1) then
		echo ""
	        echo "     Taking detailed snapshots for subject $s" | tee -a $sumlog
		if( ! -e $SUBJECTS_DIR/QA/$s/rgb/snaps/snapshot-talairach-C-128.gif || ($overwrite == 1) ) then 
			$RECON_CHECKER_SCRIPTS/takesnapshotpreset.sh -twas $s
		else
			echo "      Detailed images already exist for subject $s. Skipping this step..." | tee -a $sumlog
			sleep 2
		endif
	endif

end #foreach s ($subjects)

echo ""
echo "     Converting snapshots to tiff and making html page..."
echo ""

# images: <$SUBJID>_<region>.gif
if ( $#outputFile == 0 ) then
	
	echo "    ERROR: Output html filename for snapshots not specified." | tee -a $sumlog
	echo "" | tee -a $sumlog
else
    foreach s ($subjects)
	cd $SUBJECTS_DIR/QA/$s/rgb/snaps
	set subjOutputFile = ${s}.html
	if (`ls | egrep -c "\.rgb"`) then
	    foreach f (*.rgb)
	       convert -scale 300x300 $f ${f:r}.gif
		rm $f
	    end
	endif

	# write html
	echo '<html><head><style>body {font-family:Verdana;}</style>' > $subjOutputFile
	echo "<title>Snapshots</title>" >> $subjOutputFile
	echo '<center><font size="+2">'$s Snapshots'</font></center>' >> $subjOutputFile
	echo '</head><body bgcolor="#C0C0C0"><center><TABLE>' >> $subjOutputFile

	set titlelist = ( "talairach" "aseg" "surfs" "inflated" "curv" "parc" )
	set titleindex = 1
	foreach reglist ( "cor sag hor" "aseg-cor1 aseg-cor2 aseg-cor3 aseg-cor4 aseg-cor5 aseg-cor6 aseg-cor7 aseg-templh aseg-temprh" "cor1 cor2 cor3 cor4 cor5 cor6 cor7 templh temprh" "lh_lat lh_med lh_inf rh_lat rh_med rh_inf" "curv_lh_lat curv_lh_med curv_lh_inf curv_rh_lat curv_rh_med curv_rh_inf" "parc_lh_lat parc_lh_med parc_lh_inf parc_rh_lat parc_rh_med parc_rh_inf" )
	    echo '<TR>'  >> $subjOutputFile
	    if( $DetailedSnapshots == 1 && $titleindex <= 3 ) then
		set linkname = "$s-$titlelist[$titleindex]-QA.html"
		if( $titleindex == 3 ) set linkname = "$s-whitematter-QA.html"
		echo '<TD VALIGN=CENTER><A HREF="'$linkname'">'"$titlelist[$titleindex]</A></TD>" >> $subjOutputFile
	    else
		echo "<TD VALIGN=CENTER>$titlelist[$titleindex]</TD>" >> $subjOutputFile
	    endif
	    @ titleindex = $titleindex + 1
	    foreach reg ( $reglist )
		echo '<TD ALIGN=CENTER VALIGN=BOTTOM>' >> $subjOutputFile

		# take the string, get rid of the aseg-cor1 part, replace it with $reg
		# to form the image name
		echo '<IMG SRC="'$SUBJECTS_DIR/QA/$s/rgb/snaps/${s}_${reg}.gif'" width="300" height="300">' >> $subjOutputFile

		echo '<div style="text-align: center">'" $reg</div></A></TD>" >> $subjOutputFile
	    end
	    echo '</TR>' >> $subjOutputFile
	end

	echo '</TABLE></center>' >> $subjOutputFile
	
	if ($DetailedSnapshots == 1) then
		echo '  <left><br /><font size="+1">- Detailed Snapshots -</font><br /><a href="'$SUBJECTS_DIR/QA/$s/rgb/snaps/$s-skullstrip-QA.html'">''<font size="+1">'"Skullstrip (detailed) </font></a> <br />" >> $subjOutputFile
		echo '   <a href="'$SUBJECTS_DIR/QA/$s/rgb/snaps/$s-talairach-QA.html'">''<font size="+1">'"Talairach (detailed)</font></a> <br />" >> $subjOutputFile
		echo '   <a href="'$SUBJECTS_DIR/QA/$s/rgb/snaps/$s-whitematter-QA.html'">''<font size="+1">'"White matter (detailed)</font></a> <br />" >> $subjOutputFile
		echo '   <a href="'$SUBJECTS_DIR/QA/$s/rgb/snaps/$s-aseg-QA.html'">''<font size="+1">'"AutoSegment (detailed)</font></a> <br /></left>" >> $subjOutputFile
	endif

	echo '</body></html>' >> $subjOutputFile    
    end
endif

cd $SUBJECTS_DIR

echo '<html><head>' > $outputFile
echo '   <title>Subject Index</title><center><font size="+2">Subject Index</font></center></head><body>' >> $outputFile
foreach s ($subjects)
    echo '   <a href="'$SUBJECTS_DIR/QA/$s/rgb/snaps/${s}.html'">''<font size="+1">'"$s</font></a> <br />" >> $outputFile
end

echo "   </body></html>" >> $outputFile

echo "" >> $sumlog
echo "View your snapshots by opening this file in an internet browser:" | tee -a $sumlog
echo "$SUBJECTS_DIR/$outputFile" | tee -a $sumlog
echo "" | tee -a $sumlog


# ----

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

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

    case "-detailed"
      set DetailedSnapshots = 1;
      breaksw

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

    case "-overwrite"
      set overwrite = 1;
      breaksw
    # ----

    default:
        echo ERROR: Flag $flag unrecognized. See -help for list of proper flags.
        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;
############--------------##################

