#!/usr/bin/perl -w
#
# Perl script to create html snippets from activitymail output
#
# activity2snippet Copyright (C) Dorando 2003
#
# WIP   /             / Initial release
#
# Permission is granted to redistribute this script under the terms of the 
# GNU General Public License http://www.gnu.org/copyleft/gpl.html

use strict;
my $version='WIP 27 May 2003';

# loading required modules
use Getopt::Long qw(:config gnu_getopt);
use Pod::Usage;
use Date::Parse; use Date::Format;

# defining global variables
use vars qw(
$opt_bugzilla $opt_config $opt_dummy %opt_dirs $opt_extendtime $opt_help
$opt_module %opt_names $opt_prefix $opt_verbose
);

# default values for options
$opt_extendtime='%A %o %B, %Y at %T';
$opt_prefix="/home/cvs/";

# load an ArgvFile if this module is available, otherwise try a perl script
if(eval 'require Getopt::ArgvFile qw(argvFile);')
{argvFile(startupFilename => '.cvs2chrono', home=>1);}
else{eval "require '$ENV{'HOME'}/.cvs2chrono'";}

# parse command line options
GetOptions('bugzilla|b=s'    => \$opt_bugzilla,
           'config|c=s'      => \$opt_config,
           'cvs|options=s'   => \$opt_dummy,
           'dir|directory=s' => \%opt_dirs,
           'extendtime|e=s'  => \$opt_extendtime,
           'help|h|?'        => \$opt_help,
           'local|l'         => \$opt_dummy,
           'module|m=s'      => \$opt_module,
           'name=s'          => \%opt_names,
           'prefix|p'        => \$opt_prefix,
           'output|o'        => \$opt_dummy,
           'reverse|r'       => \$opt_dummy,
           'small|s'         => \$opt_dummy,
           'verbose|V:i'     => \$opt_verbose,
           'webcvs|w=s'      => \$opt_dummy
          ) || pod2usage(1);

# print usage
if($opt_help){pod2usage(1)};

# load additional config file
if($opt_config){eval "require '$opt_config'"};

# reading the message in
my @lines=<stdin>;

# defining local variables
my $length=@lines;
my $i=0;
my $logtext="";
my ($date,$author,$boundary);

# go through the header
until($lines[$i] eq "Log Message:\n" or $i >= $length)
{
	if($lines[$i] =~ "Date: (.*)\n"){$date = str2time($1);}
	elsif($lines[$i] =~ "From: (.*)@")
	{$author = ($opt_names{$1} ? $opt_names{$1} : $1);}
	elsif($lines[$i] =~ "Content-Type: .*; boundary=\"(.*)\""){$boundary = $1;}

	$i++;
}

# if this is not a normal Log Messege, abort. Does happen on newdir
if($lines[$i+1] ne "-----------\n"){exit;}

$i=$i+2;

# need to guess when the log message ends
until($lines[$i+2] =~ "^------------{0,3}\n"
   or $lines[$i] eq "--$boundary\n"
   or $i >= $length)
{
	# we don't want newlines at start so reset logtext if that happens
	if($logtext eq "\n"){$logtext="";}

	$logtext.=$lines[$i];
	$i++;
}

# remove last newline
chomp($logtext);

# get module name
my ($module) = $lines[$i+3] =~ "^    $opt_prefix(.+?)(/|:)";

# stop if we're in single module mode and this isn't the correct one
if($opt_module and $module ne $opt_module){exit;}; 

# gather file list
my %files;
until($lines[$i] eq "--$boundary\n" or $i >= $length)
{
	our ($dir, $state);
	if($lines[$i] =~ "^    $opt_prefix(.+?/|.*)(.*):\n"){$dir = $2}
	elsif($lines[$i] =~ "^        (.*)"){$files{"$dir\t$1"}=$state;}
	elsif($lines[$i] =~ "^(.*):\n"){$state = substr($1,0,1);}
	$i++;
}

# and lastly the revision info
until($i >= $length)
{
	if($lines[$i] =~ "^RCS file: $opt_prefix.+?/((.*)/|)(.*),v")
	{
		my $dir = $2; my $file = $3;
		my ($oldrev) = ( $lines[$i+1] =~ "retrieving revision (.*)\n");
		my ($rev) = ( $lines[$i+2] =~ "retrieving revision (.*)\n");
		$files{"$dir\t$file"}.="\t$oldrev\t$rev";
	}
	$i++;
}

# convert date to nicer format and generate filename
my $file = time2str("%Y%m%d%H%M%S%j%W", $date, "UTC");
$date = time2str($opt_extendtime, $date, "UTC");

# format the logtext
$_=$logtext;
 s/&/&amp;/g;
 s/</&lt;/g;
 s/>/&gt;/g;
 s#\n#<br />#g;
 s#(http://.+?\.(jpg|jpeg|png|mng|gif))#<img src="$1" alt="" />#g;
 if($opt_bugzilla){s{#([0-9]+)}{<a href="$opt_bugzilla$1">#$1</a>}ig}
$logtext=$_;

my $currentdir=".";
my $files;
my $class="";

foreach (sort(keys %files))
{
	my ($dir, $file) = split ("\t",$_);

	if ($currentdir ne $dir)
	{
		if($files){$files.="</td></tr>\n"}
		$currentdir = $dir;
		$files.='<tr class="f'.$class.'"><td>[<a href="'.$dir.($dir ? "/":"").'#dirlist">'.
		($opt_dirs{$dir} ? $opt_dirs{$dir} : $dir).'</a>]</td><td>';
		$class = ! $class;
	}

	my ($state,$rev1,$rev2) = split("\t",$files{$_});
	if($state eq "A")
	{
		$files.="<a href=\"".$dir.($dir ? "/" : "").$file."?rev=1.1&amp;content-type=text/vnd.viewcvs-markup\">$file</a>* ";
	}
	elsif($state eq "R")
	{
		$files.="<del>$file</del> ";
	}
	elsif($state eq "M")
	{
		$files.="<a href=\"".$dir.($dir ? "/" : "").$file.".diff?r1=$rev1&amp;r2=$rev2\">$file</a> ";
	}
}

$files.='</td></tr>';

use Fcntl;
until(sysopen(SNIPPET,"snippets/$file", O_WRONLY | O_CREAT | O_EXCL)){$file.="0";}

#FIXME Remove after test
#print SNIPPET <<"HTMLHEADER";
#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
#<html>
#<head>
#<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
#<title>CVS Log</title>
#<style type="text/css">
#body,th{font:13px monospace}
#table{width:100%;border:solid 1px black;border-spacing:0px}
#th{background-color:#90EE90}
#td{background-color:#F5F5DC;padding:3px;border-top:solid 1px black;border-bottom:solid 1px black}
#.f td{background-color:#D3D3D3;border:none}
#.f1 td{background-color:#C0C0C0;border:none}
#</style>
#<base href="http://cvs.mess.org:6502/cgi-bin/viewcvs.cgi/" />
#</head>
#<body><div>
#HTMLHEADER

#print SNIPPET "<table cellspacing=\"0\">\n<colgroup><col /><col /></colgroup>\n".
#              "<tr><th colspan=\"2\">$date by <i>$author</i></th></tr>\n".
#              "<tr><td colspan=\"2\">$logtext Hello Hello Hello<img src=\"http://www.mess.org/gfx/messlogo.gif\" /></td></tr>\n".
#              "<tr class=\"f\"><td width=\"20%\">Hello Hello Hello Hello Hello</td><td>YO</td></tr>\n".
#              "</table>\n";
#
#print SNIPPET "<table>".
#              "<tr><th><i>$author</i><br />$date</th><td style='border:none'>$logtext</td></tr>".
#              "$files\n".
#              "</table>\n"
#;
#
#FIXME


print SNIPPET "<table>\n<colgroup><col width=\"20%\" /><col /></colgroup>\n".
              "<tr><th colspan=\"2\">$date by <i>$author</i></th></tr>\n".
              "<tr><td colspan=\"2\">$logtext</td></tr>\n".
              "$files\n".
              "</table>\n";
close SNIPPET;


__END__
