#!/usr/bin/perl
#
# sbsdiff.pl
# (c) Geoff Huston 2004
#
# output HTML file of a side-by-side difference of two files
#
# sbsdiff.pl <file1> <file2> >output.html
#
# requires: sed, wdiff
# generates temporary files in /tmp (/tmp/htmlsbs.<n>.<pid>)
#

$| = 1 ;

$sed = "/usr/bin/sed" ;
$wdiff = "/usr/local/bin/wdiff"

print("Content-type: text/html\n\n") ;


if ((!($file1 = shift)) || (!($file2 = shift))){
  print("<html><head><title>sbsdiff - error</title></head>\n") ;
  print("<body bgcolor=\"white\">");
  print("<p><b>Usage:</b> sbsdiff file1 file2</p></body></html>\n") ;
  exit ;
  }

if (!(-e $file1)) {
  print("<html><head><title>sbsdiff - error</title></head>\n") ;
  print("<body bgcolor=\"white\">");
  print("<p><b>sbsdiff - file1 not found ($file1)</p></body></html>\n") ;
  exit ;
  }

if (!(-e $file2)) {
  print("<html><head><title>sbsdiff - error</title></head>\n") ;
  print("<body bgcolor=\"white\">");
  print("<p><b>sbsdiff - file2 not found ($file2)</p></body></html>\n") ;
  exit ;
  }

print("<p>$file1</p>\n");
print("<p>$file2</p>\n");

$f1 = $file1 ;
$f2 = $file2 ;

$a1 = "/tmp/htmlsbs.1.$$";
$a2 = "/tmp/htmlsbs.2.$$";
$a5 = "/tmp/htmlsbs.5.$$";

system("$sed -e 's/&/\\&amp;/g' -e 's/</\\&lt;/g' -e 's/>/\\&gt;/g' $f1 > $a1") ;
system("$sed -e 's/&/\\&amp;/g' -e 's/</\\&lt;/g' -e 's/>/\\&gt;/g' $f2 > $a2") ;
system("$wdiff -n -w \"<t2>\" -x \"</t2>\" -y \"<t1>\" -z \"</t1>\" $a2 $a1 >$a5");

$t1 = $f1 ;
$t1 =~ s/^.*\/// ;
$t2 = $f2 ;
$t2 =~ s/^.*\/// ;

print("\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
print("<html>\n<head>\n") ;
print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n");
print("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n") ;
print("<title>sbsdiff $f1 $f2</title>\n") ;
print("</head>\n<body>\n<!-- Generated by sbsdiff v1 -->\n");
print("<style>\n"); 
#print("tr { font-size: 9pt; height: 1em; }\n");
print("tr { height: 1em; }\n");
print("td { white-space: pre; font-family: monospace; }\n");
print("th { font-size: 12pt; }\n");
print(".small  { font-size: 9pt; }\n");
print(".left   { background-color: #FFFFFF; }\n");
print(".right  { background-color: #F0F0F0; }\n");
print(".diff   { background-color: lightblue; }\n");
print(".insert { background-color: #C0C0C0; color: green; }\n");
print(".delete { background-color: #C0c0c0; color: red; }\n");
print(".void   { background-color: lightyellow; }\n");
print("</style>\n");
print("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr bgcolor=\"orange\"><th>$t1</th><th> </th><th>$t2</th><tr>\n");

$stacklines = 0;
$color="0";
$scolor = 0 ;
open(I,"$a5") ;
while ($l = <I>) {
  chop($l);
  $pl = $l ;
  $pl =~ s/</&lt;/go;
  $pl =~ s/>/&gt;/go;
  if ($color == 1) {
    $l = "<t1>$l";
    }
  elsif ($color == 2) {
    $l = "<t2>$l";
    }
  $scolor = $color ;
  $color = 0 ;
  $sl = $l ;
  $sl =~ s/\<t.\>.*\<\/t.\>//go;
  if ($sl =~ /<t1>/) {
    $color = 1 ;
    $l = "$l</t1>";
    }
  elsif ($sl =~ /<t2>/) {
    $color = 2 ;
    $l = "$l</t2>";
    }

  $ol = $l;
  $ol =~ s/<t1>/<strong><font color=green>/go;
  $ol =~ s/<\/t1>/<\/font><\/strong>/go;
  $ol =~ s/<t2>/<strike><font color=red>/go;
  $ol =~ s/<\/t2>/<\/font><\/strike>/go;
  $ol = "<b>$scolor</b> " . $ol . " <b>$color</b>";

  $l1 = $l2 = "";
  if ($l) {  
    $l1 = $l ;
    $l1 =~ s/\<t2\>[^\<]*\<\/t2>//go;
    if ($l1) {
      $l1 =~ s/<t1>/<span class="insert">/go;
      $l1 =~ s/<\/t1>/<\/span>/go;
      }
    $l2 = $l ;
    $l2 =~ s/\<t1\>[^\<]*\<\/t1\>//go;
    if ($l2) {
      $l2 =~ s/<t2>/<span class="delete"><strike>/go;
      $l2 =~ s/<\/t2>/<\/strike><\/span>/go;
      }
    }

  print("<tr><td class=\"left\" >$l1</td><td> </td><td class=\"right\">$l2</td></tr>\n");
  }

print("<tr><td class=\"left\"><hr></td><td> </td><td class=\"right\"><hr></td></tr>\n");
print("</table>\n");

print("</body>\n</html>\n");

unlink($a1);
unlink($a2);
unlink($a5);
exit ;
