#!/usr/bin/perl

# perform an lshistory on the contents of a directory
# report all the create version events in a tabular format
# elements across the top, dates down the side.
#
# cells contain all versions created for that element on that day
# and are hlinks to the actual version
#
# top row lists the elements, and each entry is a link to the
# /main of the extended namespace for that element.

use lib "lib";
use CCwwwutil;

$arg = shift;
$now = localtime();
$title = "History of a ClearCase directory ";
$mailto = "Marc Girod";
$mailaddr = "marc.girod\@nokia.com";
$ENV{PATH} = "/opt/rational/clearcase/bin:/bin";

# print out HTML header
print <<HTML;
Content-type: text/html

<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
$now
<hr>
HTML

# simplify argument: 
#   1) no .. allowed
#   2) replace // by /
#   3) strip leading and trailing spaces and /'s
$arg =~ s/\.\.//g;
while ($arg =~ s/\/\//\//g) {};
$arg =~ /^\s*\/?(.*?)\/?\s*$/;
$arg = $1;

# if you name this script "history" and want to include it in
# an <a href ... > tag, do this in your referring doc:
#  <a href=http://server/cgi-bin/history?/path/to/directory>Dir Hist</a>
# (where cgi-bin is where your server keeps its CGI scripts)

$dir = viewify(rootify($arg));
$dirinurl = $dir;
$dirinurl =~ s/\+/%2b/g;

print "<h2>Files in: " . rootify($arg) . "</h2>\n";
print "<table border>\n";

# the %header array contains the column numbers for file elements
$header{"/BEGIN"} = 0;
$header{"/END"} = 1;

# index into %header array
$colindex = 2;

# the @row array contains the data for the row.  It is indexed
# [ indirectly via the %header array ] on the names of the elements

# begin the top row with the date
$row[$header{"/BEGIN"}] = "<tr><th>Date \\ File</th>";

# scan the directory
open (DIR, "cleartool find $dir -nxname -type f -nrecurse -print |");
while ( <DIR> ) {
      chop; # \n

      # parse out filename
      /^(.*\/)([^\/]*)$/;

      # and save it
      push(@files, $2);

      # update %header array so that this element can be found
      $header{$2} = $colindex++;

      # update @row array with what we WANT to find.
      #$row[$header{$2}] = "<a href=\"file:$dir/$2\">$2</a>";
      $row[$header{$2}] = "<a href=\"/cgi-bin/$cccat?$dirinurl/$2\">$2</a>";
}
close DIR;

# end the top row with this.
$row[$header{"/END"}] = "</tr>\n";

# scan 'create version' events
open (HIST, "cleartool lshistory $dir | grep 'create version' | ");
# clear the date so the top row gets printed from within the loop
$olddate = "";
while (<HIST>) {
      chop; # \n

      # parse the event - formatted text from lshistory
      ($datetime, $user, $junk1, $junk2, $element) = split(" ");

      # further parse the timestamp
      ($date) = split(/\./ , $datetime);

      # whenever the day changes, make a new row
      if ( $date ne $olddate ) {
              # reset date
              $olddate = $date;

              # end each row with this
              $row[$header{"/END"}] = "</tr>\n";

              # print [ and clear ] the row in sorted order
              &print_row();

              # start new row
              $row[$header{"/BEGIN"}] = "<tr><th>$date</th>";
      } 

      # further parsing of element - get rid of "
      $element =~ s/\"//g;

      # element is actually file@@version - parse these
      $element =~ /^(.*\/)([^\/]*)@@(.*)$/;
      $element = $2;
      $version = $3;

      # cell data starts out empty
      $cell = "";

      # scan the list of files for the one this event changes
      foreach $file (@files) {

	  # for the element this event changes,
	  if ( $file eq $element ) {
	      # put which version was created into the cell
	      #$cell .= "<nobr><a href=file:$dirinurl/$element\@\@$version>$version</a>: $user</nobr><br>";
	      #$cell .= "<nobr><a href=\"/cgi-bin/$cccat?$dirinurl/$element\@\@$version\">$version</a>: $user</nobr><br>";
	      $cell .= "<nobr><a href=\"/cgi-bin/$cccat?$dirinurl/$element\@\@$version\">$version</a>: <a href=\"/cgi-bin/$ccdiff?$dirinurl/$element\@\@$version\">$user</a></nobr><br>";
	  }
      }

      # finally, put the cell into the row
      $row[$header{$element}] .= $cell;

}

close HIST;

# final row - end it thus:
$row[$header{"/END"}] = "</tr>";

# print it
&print_row();

# be done with the table
print "</table>\n\n";

# put the `browse' table below
&print_browse_table();

#
# print footer
#
print "<hr>\n<address>\nThis page is automagically generated.\n"
    . "Mail your remarks to <a href=\"mailto:" . $mailaddr . "\">"
    . $mailto ."</a>\n</address>\n";

# be done with the page
print "</body>\n</html>\n";
exit 0;

#######
sub print_browse_table {
  if (-d "$dir") {
    print "<h2>Browse:</h2>\n"
        . "<table border cellpadding=4>\n"
        . "<tr>\n";

    if ($arg) {
      print "  <th><a href=\"/cgi-bin/cchist?".parent($arg)."\">..</a></th>\n";
    }

    opendir (DIR,"$dir");

    while ($item = readdir(DIR)) {
      unless (($item eq ".") || ($item eq "..") || ($item eq "lost+found")) {
        if (-d "$dir/$item") {
          print "  <td><a href=\"/cgi-bin/cchist?$arg/$item\">$item</a></td>\n";
        }
      }
    }

    print "</tr>\n</table>\n";
  }
}

#######
sub parent {
  my $dir = shift;

  $dir =~ s/\+/%2b/g;
  $dir =~ /^\s*\/?(.*?)\/?\s*$/;
  $dir = $1;

  if ($dir =~ /(.*)\//) { $dir = $1; }
  else                  { $dir = ""; }
}

sub append {
  my ($dir, $subdir) = @_;

  $dir =~ /(.*?)\/*\s*$/;
  $dir = $1;
  $subdir =~ /^\s*\/*(.*?)\/*\s*$/;
  if ($1) { $dir = "$dir/$1"; }
  else    { $dir; }
}

#######
sub print_row {
# print and clear out the @row array in sorted order

	local($element, $column) = ("","");
	print "  <!-- row -->\n";

	# print the first column first and clear it
	print "  ".$row[$header{"/BEGIN"}]."\n";
	$row[$header{"/BEGIN"}] = "";

	# print every column in the row
	foreach $element (sort keys(%header)) {

		# skip the first and last rows
		next if $element eq "/BEGIN";
		next if $element eq "/END";

		# what column is this element in?
		$column = $header{$element};

		# make empty cells have a border
		$row[$column] = "<br>" if ! $row[$column];

		# print that column and clear it.
		print "  <td>$row[$column]</td>"."\n";
		$row[$column] = "";
	}

	# NOW print the designated last row and clear it
	print "  ".$row[$header{"/END"}]."\n";
	$row[$header{"/END"}] = "";
}

