#!/usr/bin/env perl
#
# $Id: pdbout2html 6286 2008-05-21 15:54:31Z vriend $
#
# (C) R.W.W. Hooft, EMBL, 1996
#
$TITLE="WHAT IF Check report";
$WHATCHECKURL="http://www.cmbi.ru.nl/gv/whatcheck/";
$WHATCHECKREF="<A HREF=$WHATCHECKURL>WHAT_CHECK</A>";
$INTROURL="http://www.cmbi.ru.nl/gv/checkhelp/";
$LITREF="<A HREF=${WHATCHECKURL}whatifrefs.html>[REF]</A>";
$WIREF="<A HREF=http://swift.cmbi.ru.nl/>http://swift.cmbi.ru.nl/</a>";
$DSSPREF="<A HREF=http://swift.cmbi.ru.nl/gv/dssp/>the DSSP page</a>";
$INTROREF="<A HREF=${INTROURL}>introduction</A>";

open(TXT,"<pdbout.txt");
open(TEX,"<pdbout.tex");
open(HTML,">pdbout.html");

sub makegiffile {
    # make the GIF file
  ($psname)=@_;
  $gifname=$psname;
  $gifname=~s/\.eps/\.gif/;
  $ppmname=$psname;
  $ppmname=~s/\.eps/\.ppm/;
  if ($psname=~/^sct/) {
    open(FIL,">>$psname");
    print FIL "showpage\n";
    close(FIL);
  }
  system("gs -sDEVICE=ppmraw -sOutputFile=$ppmname -q -dNOPAUSE $psname quit.ps &&\
            pnmcrop $ppmname 2>/dev/null | ppmtogif >  x.gif &&\
            mv x.gif $gifname ; rm $ppmname");
  # Skip the line in the TXT file.
  while (<TXT>) { last if /In the TeX/ };
  # Include the GIF in the HTML file.
  print HTML $separator; $separator="";
  print HTML "<IMG SRC=$gifname>\n";
}

sub qprint {
  my($line)=join(' ',@_);
  $line=~s/\&/\&amp;/g;
  $line=~s/\</\&lt;/g;
  $line=~s/\>/\&gt;/g; 
  $line=~s/\[REF\]/$LITREF/g;
  $line=~s/\http\:\/\/swift\.cmbi\.ru\.nl\//$WIREF/; 
  $line=~s/\<A\ HREF=http\:\/\/swift\.cmbi\.ru\.nl\/\>http\:\/\/swift\.cmbi\.ru\.nl\/\<\/a\>gv\/dssp\//$DSSPREF/;  
  print HTML $line;
}

sub secstrblock {
  #skip until end of the note in TeX file
  while (<TEX>) { last if /^\\end\{note\}/ };
  # and copy TXT file verbatim until empty line.
  while (<TXT>) { 
    qprint $_;
    last if /^\s*$/;
  }
  print HTML $separator; $separator="";
  print HTML "<PRE>\n";
  # now copy preformatted until next 3 open lines in sequence
  $emp=0;
  while (<TXT>) { 
    next if /DSSP\.EXE/;
    next if /Secondary structure assignment/;
    qprint $_;
    if (/^\s*$/) { $emp++ } else { $emp=0 };
    last if $emp>2;
  }
  print HTML "</PRE>\n";
}

sub handlesupertable {
  # skip table in TeX
  while (<TEX>) { last if /^\\end\{supertabular\}\\end\{center\}/ };
  $any=0;
  # Copy verbatim from TXT until first empty line after table. Or "And"
  $andsoon="";
  print HTML $separator; $separator="";
  print HTML "<PRE>\n";
  while (<TXT>) {
    last if ($any && /^\s*$/);
    next if /^\s*$/;
    if (/And so on/) {
      $andsoon=$_;
      last;
    }
    $any=1;
    qprint $_;
  }
  print HTML "</PRE>\n";
  if ($andsoon) {
    print HTML $andsoon;
    # skip it in the TeX file
    <TEX>;
  }
}


# Skip headers

while (<TEX>) { last if /End of the preface/ }; <TEX>;
while (<TXT>) { 
  if (/Date : (.*)/) {
    $date=$1;
  }
  last if /^=================/ 
};

print HTML <<END;
<HTML><HEAD>
<TITLE>$TITLE</TITLE>
</HEAD><BODY>
<H1>$TITLE</H1>
This file was created $date from $WHATCHECKREF output
by a conversion script. If you are new to WHAT_CHECK, please study
the <A HREF="http://swift.cmbi.ru.nl/gv/pdbreport">pdbreport pages</A>.
There also exists a <A href="http://swift.cmbi.ru.nl/gv/whatcheck/htmlintro.html">legend</A> 
to the output.
END

# Parse TeX

while (<TEX>) {
  chomp;
  if (/^\s*$/) {
    if ($inparbox) {
      $separator="<BR>\n";
    } else {
      $separator="<P>\n";
    }
  } elsif (/^\\sect\{([^\}]*)\}/) {
    print HTML "<H2>Verification log for $1</H2>\n";
    $separator="";
  } elsif (/^\\(begin|end)\{(note|error|warning)\}/) {
    #idle
  } elsif (/^\\parbox\{1\\textwidth\}/) {
    print HTML "$separator" if $separator; $separator="";
    $inparbox=1;
  } elsif (/^\}\% End of ParBox/) {
    $inparbox=0;
  } elsif (/^\\halfpage/) {
      #skip until end of the note in TeX file
      while (<TEX>) { 	last if /End of ParBox/     };
      $inparbox=0;
      # skip empty in TXT file;
      while (<TXT>) { last unless /^\s$/ };
      print HTML "<PRE>\n$_";
      # and copy TXT file verbatim until empty line.
      while (<TXT>) { 
        qprint $_;
	last if /^\s*$/;
      }
      print HTML "</PRE>\n";
  } elsif (/^\\fullpage/) {
    $inparbox=1;
  } elsif (/^\$\\left\[/) {
    $bars=1;
  } elsif (/^\\right\]\$/) {
    $bars=0;
  } elsif (/^\$?\\begin\{array\}/) {
    print HTML $separator; $separator="";
    print HTML "<PRE>\n";
  } elsif (/^\\end\{array\}/) {
    print HTML "</PRE>\n";
  } elsif (/^\\hdr\{([^\}]*)\}/) {
    $separator="";
    print HTML "$1<BR>\n";
    # skip header in TXT file;
    while (<TXT>) { last if /\Q$1\E/ };
  } elsif (/^\\ssect\{([^\}]*)\}/) {
    $separator="";
    $number++;
    print HTML "<A NAME=$number><H3>$1</H3></A>\n";
  } elsif (/^\\begin\{center\}\\begin\{supertabular\}/) {
    &handlesupertable();
  } elsif (/^\\begin\{tabular\}/) {
    # skip table in TeX
    $n=-1;
    while (<TEX>) { $n++; last if /^\\end\{tabular\}/ };
    # Copy verbatim from TXT until first empty line after table.
    if ($n>0) {
      $any=0;
      print HTML $separator; $separator="";
      print HTML "<PRE>\n";
      while (<TXT>) {
	last if ($any && /^\s*$/);
	next if /^\s*$/;
	$any=1;
	qprint $_;
      }
      print HTML "</PRE>\n";
    } else {
      print HTML "<PRE> (none) </PRE>\n";
    }
  } elsif (/^\\showsect\{([^\}]*)\}/) {
    $separator="";
    $number++;
    print HTML "<A NAME=$number><H4>$1</H4></A>\n";
    # skip TXT file to same.
    while (<TXT>) { last if /\Q$1\E/};
    # Special case code for Secondary structure information
    if (/Secondary structure/) {
      &secstrblock();
    }
  } elsif (/^\\psplot\{([^\}]*)\}/) {
    &makegiffile($1)
  } elsif (/^\\bsplot\{([^\}]*)\}/) {
   &makegiffile($1) 
  } elsif (/TRAILER\.TEX/) {
    last;
  } else {
    # print the equivalent line from the TEXT file instead.
    print HTML $separator; $separator="";
    while (<TXT>) {
      unless (/^\s*$/) {
	if ($bars) {
	  chomp;
	  qprint " |$_|\n";
	} else {
	  qprint $_;
	}
	last;
      }
    }
  }
}

print HTML "<PRE>\n";

while (my $line = <TXT>)
{
	print HTML "$line";
}
print HTML "</PRE>\n";

print HTML "</BODY></HTML>\n";
#unless (<TXT>) { warn "End of file reached on TXT!\n"; unlink("pdbout.html"); }
#unless (<TEX>) { warn "End of file reached on TEX!\n"; unlink("pdbout.html"); }

#while (my $line = <TXT>)
#{
#	print "TXT> $line";
#}

#while (my $line = <TEX>)
#{
#	print "TEX> $line";
#}
