#!/ccpdisk/xtal/ccp4_gui/local/bin/tclsh8.3
#
# htmlise_logs.tcl
#
# Author: Peter Briggs     
# CVS id string: $Id: htmlise_logs.tcl,v 1.5 2006/01/10 16:29:53 pjx Exp $
#
# Process a text-based log file and turn it into
# html. Add tags for highlighting key lines in
# different colours
#
# Usage: htmlise_log.tcl <file>
#
puts "htmlise_log.tcl: starting"

# Initialise some counters for links
set nfail 0
set nerror 0
set nwarn 0

# Initialise text
set failtext ""
set errortext ""
set warntext ""

# Initialise line counter
set lcount 0

# Flags for printing lists of warnings, failures and errors
set link_warnings 0
set link_errors 1
set link_failures 1

# Was a file specified?
if { $argc != 1 } {
  puts "Usage: htmlise_log.tcl <file>"
  exit
} else {
  set f [lindex $argv 0]
}

# Check file exists
if { ![file exists $f] } {
  puts "$f: not found"
  exit
}

# File names for output
# Write the three components (head, foot and body) to
# individual files and then join them together at the end
set f1 $f.html
set fb $f.body.html
set fh $f.head.html
set ff $f.foot.html

# Maximum length of lines written out
set linelen 100

# Open files
set fp [open $f "r"]
set fp1 [open $fb "w"]

# Write the body first
# We also keep track of the warnings, failures and errors to
# write to the head of the HTML file at the end

# Set a flag indicating that the pre flag is open
puts $fp1 "<pre>"
set pre_open 1

# Read and process lines one at a time
while { [gets $fp line] > -1 } {

  # Increment line counter
  incr lcount

  # Remove any html tags found in the output
  while { [regsub "<" $line "\\&lt;" line] } { }
  while { [regsub ">" $line "\\&gt;" line] } { }

  # Replace tabs with spaces
  while { [regsub "\t" $line "      " line] } { }

  # Break long lines into several lines for display
  set start 0
  set index 0
  set line0 ""
  if { [set len [string length $line]] <= $linelen } {
      # Nothing to do
      set line0 $line
  } else {
      # Chop up the line
      while { $len > $index } {
	  # Index = the position of the end of the line segment
	  set index [expr $start + $linelen]
	  if { $index >= $len } {
	      # The chunk fits entirely into the next line
	      set chunk [string range $line $start end]
	  } else {
	      # Grab the next chunk
	      set chunk [string range $line $start [expr $index]]
	  }
	  # Append the chunk
	  if { [string length $line0] > 0 } {
	      append line0 "\n"
	  }
	  append line0 $chunk
	  # Step on for next cycle
	  set start [expr $index + 1]
      }
  }
  set line $line0

  switch -regexp -- "$line" {
    ^auto-test-ccp4: {
       if { $pre_open } { puts $fp1 "</pre>" }
       puts $fp1 "<h3>$line</h3>"
       if { $pre_open } { puts $fp1 "<pre>" }
    }
    (WARNING|Warning|warning) {
       incr nwarn
       puts $fp1 "<a name=\"warn$nwarn\"></a>"
       puts $fp1 "<font color=\"blue\">$line</font>"
       append warntext "<a href=\"\#warn$nwarn\">\[$nwarn\]</a> "
    }
    "\[^A-Za-z0-9_/\]+(ERROR|Error)" {
       incr nerror
       puts $fp1 "<a name=\"error$nerror\"></a>"
       puts $fp1 "<font color=\"red\">$line</font>"
       append errortext "<a href=\"\#error$nerror\">\[$nerror\]</a> "
    }
    (failed) {
       incr nfail
       puts $fp1 "<a name=\"fail$nfail\"></a>"
       puts $fp1 "<font color=\"red\">$line</font>"
       append failtext "<a href=\"\#fail$nfail\">\[$nfail\]</a> "
    }
    default {
       puts $fp1 $line
    }
  }
  # End of switch statement
}
# End of while loop

# Close the files
close $fp1
close $fp

# Write HTML header
set fph [open $fh "w"]
puts $fph "<html>
<head>
<title>[file tail $f1]</title>
</head>
<body bgcolor=\"#ffffff\">
<h1>$f1</h1>
<p><a href=\"\#end_of_log\">\[Go to end of logfile\]</a>"

# Write links to warnings, failures and errors
if { $nwarn > 0 && $link_warnings } {
   puts $fph "<p><b>$nwarn warning messages:</b></p>"
   puts $fph "<ul>\n<li>\n$warntext\n</li>\n</ul>"
}
if { $nfail > 0 && $link_failures } {
   puts $fph "<p><b>$nfail failure messages:</b></p>"
   puts $fph "<ul>\n<li>\n$failtext\n</li>\n</ul>"
}
if { $nerror > 0 && $link_errors } {
   puts $fph "<p><b>$nerror error messages:</b></p>"
   puts $fph "<ul>\n<li>\n$errortext\n</li>\n</ul>"
}
close $fph

# Write the HTML footer
set fpf [open $ff "w"]
puts $fpf "</pre>"
puts $fpf "<a name=\"end_of_log\"></a>
</body>
</html>\n"
close $fpf

# Join the files using cat, and delete temp files
if { [catch { exec cat $fh $fb $ff > $f1 } err ] } {
  puts "Failed to join files: $err"
}
foreach fdel [list $fh $fb $ff] {
  if { [file exists $fdel] } {
    file delete $fdel
  }
}

# Finish
puts "Finished."
exit 0

