#! /usr/bin/perl -wT
# Copyright  2001 Martin Kammerhofer <mkamm@gmx.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# @(#)$CVSHeader: scripts/cvsuplog2cvsweb.pl,v 1.13 2001/02/23 10:04:39 mkamm Exp $
# Convert cvsup(1) logfile to HTML.

require 5.003;
use strict;
use Getopt::Long;
{
    local $^W = 0;
    eval "use URI::Escape";
    if ($@) {
	# provide a dummy function if package URI::Escape is not available.
	sub uri_escape { return $_[0]; }
	# warn "$0: package URI::Escape is not available\n";
    }
}

my ($cvsweb, $urlsuffix, $branch, $cvsrootdir, $outfile) =
    (
     # "http://www.FreeBSD.org/cgi/cvsweb.cgi",
     "http://localhost/FreeBSD-WWW/cgi/cvsweb.cgi",
     "",	# e.g. "cvsroot=myproject&content-type=text/x-cvsweb-markup"
     "",	# e.g. "MAIN"
     "",	# e.g. "/home/ncvs"
     "-",
     );

my %optctl = (
	      "branch" => \$branch,
	      "cvsrootdir" => \$cvsrootdir,
	      "cvsweb" => \$cvsweb,
	      "outfile" => \$outfile,
	      "urlsuffix" => \$urlsuffix,
	      );

my $prog=$0;
$prog =~ s+^.*/++; # basename
$prog =~ s/\.p(er)?l$//; # truncate '.pl' suffix
my $version = '$Id: cvsuplog2cvsweb.pl,v 1.13 2001/02/23 10:04:39 mkamm Exp $'; # '
$version =~ s/^\s*\$Id: //;
$version =~ s/ \$\s*$//;

sub usage () {
    print STDERR
	"usage: $prog [--cvsweb=URL] [--urlsuffix=STR] [--branch=TAG]\n",
	"             [--cvsrootdir=DIR] [--outfile=FILE] cvsup.log\n",
	"   or: $prog --version\n";
    exit 64;
}

sub html_escape ($) {
    local $_ = shift or die;
    s/\&/&amp;/g;
    s/\"/&quot;/g;
    s/>/&gt;/g;
    s/</&lt;/g;
    return $_;
}

if (!GetOptions(\%optctl, "cvsweb|url=s", "branchtag|tag=s", "version!",
		"urlsuffix|suffix=s", "outfile|output=s",
		"cvsrootdir|repositorydir=s",)
    || $#ARGV > 0 # more than one filearg
    || $#ARGV == -1 && -t && !$optctl{version} # input from terminal
    )
{
    usage();
}
if ($optctl{version}) {
    print "$version\n";
    exit 0;
}
$cvsrootdir =~ s/*$;

if ($outfile ne "-") {
    close(STDOUT) or die;
    $outfile = $1 if $outfile =~ /^(.*)$/; # untaint
    open(STDOUT, "> $outfile") or
	die "$prog: redirect output to '$outfile': $!.\nStopped";
}

my (@f, $fname, $htm, $uri);
my $cvsweb_suffix = "";
$cvsweb_suffix = "?only_with_tag=" . $branch if $branch;
$cvsweb_suffix .= ($cvsweb_suffix ? "&" : "?") . $urlsuffix if $urlsuffix;

my $title = "$prog";
$title .= " $ARGV[0]" if $#ARGV == 0;
$title = html_escape($title); # paranoia
print <<EndOfHeader;
<HTML><HEAD>
<META NAME="generator" CONTENT="$version">
<TITLE>$title</TITLE></HEAD>
<BODY BGCOLOR=white><PRE>
EndOfHeader

while (<>) {
    @f = split;
    if ($f[0] eq 'Edit'
	|| $f[0] eq 'Checkout'
	|| $f[0] eq 'Create'
	|| $f[0] eq 'Delete') {
	# hyperlink the file name
	$fname = $f[1];
	$fname =~ s/,v$//;
	$htm = html_escape($fname); # be paranoid
	$uri = uri_escape("$cvsweb/$fname$cvsweb_suffix");
	s\Q$f[1]\E<A HREF="$uri">$htm</A>;
    } elsif ($f[0] eq 'Add' && $#f >= 1 && $f[1] eq 'delta') {
	# make the UTC date more readable
	s{\b(\d{4})\.(\d{2})\.(\d{2})\.(\d{2})\.(\d{2})\.(\d{2})\b}
	{$1-$2-$3 $4:$5:$6};
	# hyperlink the revision number to the delta (diff)
	my $committer = html_escape($f[4]);
	s/\Q$f[4]\E/$committer/; # be paranoid
	my ($delta, $prev) = ($f[2], $f[2]);
	if ($delta =~ /^\d+(?:\.\d+)*\.(\d+)$/) {
	    my $lsn = $1; # least significant number
	    $lsn -= 1;
	    if ($lsn) { # delta x.y => x.(y+1)
		$prev =~ s/$1$/$lsn/;
	    } else { # delta x.y => x.y.z.1
		$prev =~ s/\.\d+\.\d+$//;
	    }
	    # link the delta revision
	    $uri = uri_escape("$cvsweb/$fname") . "#"
		. uri_escape("rev$delta$cvsweb_suffix");
	    s\b\Q$delta\E\b<A HREF="$uri">$delta</A>;
	    # link the word "delta" to the patch
	    my $suffix = ($cvsweb_suffix ? "$cvsweb_suffix&" : "?");
	    $uri = uri_escape("$cvsweb/$fname.diff${suffix}r1=$prev&r2=$delta");
	    s\bdelta\b<A HREF="$uri">delta</A>;
	}
    } elsif ($f[0] eq 'Rsync'
	     || ($f[0] eq 'Append' && $#f >= 1 && $f[1] eq 'to')) {
	$htm = html_escape($f[-1]);
	$uri = uri_escape("file:$cvsrootdir/$f[-1]");
	s\Q$f[-1]\E<A HREF="$uri">$htm</A>;
    }
} continue {
    print;
}

print <<'EndOfFooter';
</PRE></BODY></HTML>
EndOfFooter

if ($outfile ne "-") {
    close(STDOUT) or die;
}

exit(1) unless $.;

__END__;

=head1 NAME

cvsuplog2cvsweb - convert a C<cvsup> log file to HTML

=head1 SYNOPSIS

=over 4

=item .

cvsuplog2cvsweb [--cvsweb=I<URL>] [--urlsuffix=I<STR>] [--branch=I<TAG>]
[--cvsrootdir=I<DIR>] [--outfile=I<FILE>] cvsup.log

=item .

cvsuplog2cvsweb --version

=back

=head1 DESCRIPTION

The cvsuplog2cvsweb program takes a logfile from C<cvsup> and converts
it into HTML. Names of changed (added, updated or deleted) files are
replaced with hyperlinks to a C<cvsweb> CGI script.

This means you can click on any of the updated files and see the CVS
log (change history) and have access to all the revisions and deltas.

(C<cvsup> is written by John Polstra <jdp@polstra.com>. It is a
network distribution package for CVS repositories. The cgi-script
C<cvsweb.cgi> was originally written by Bill Fenner
<fenner@freebsd.org> for the FreeBSD project. It allows browsing of
CVS-repositories with a HTML-browser. CVS is a popular version control
system.)

Options may be abbreviated to a unique prefix. The options are as
follows:

=over 4

=item --cvsweb=I<URL>

Specify URL of cvsweb.cgi script.

=item --urlsuffix=I<SFX>

Specify some extra information for appending to generated URLs. (You
should not type a leading C<?> or C<&> character because it will be
added automatically.)

=item --branch=I<TAG>

Tell C<cvsweb.cgi> that you are only interested in file revisions on
the specified branch.

=item --outfile=I<FILENAME>

Specify the output file. If no output file is specified then standard
output is used.

=item --cvsrootdir=I<DIR>

Add HTML C<file:> links to non-versioned (rsynced or appended) files.
The directory I<DIR> is prepended to the generated C<file:> URLs.

=item --version

Print version information and exit.

=back

=head1 EXAMPLE

Suppose you are running the FreeBSD operating system and want to upgrade your
sources from the RELENG_4 branch. You already have a working cvsup
config file in F</usr/share/examples/cvsup/4.x-stable-supfile>. Since
your nearest cvsup mirror is in Germany you use

C<cvsup -g -h cvsup.de.freebsd.org -L2
/usr/share/examples/cvsup/4.x-stable-supfile E<gt>cvsup.log>

Now you want to know what all this source updates are about and invoke

C<cvsuplog2cvsweb --cvsweb=http://www.FreeBSD.org/cgi/cvsweb.cgi
--branch=RELENG_4 E<gt>cvsuplog.html>

Open F<cvsuplog.html> with your favourite browser now!

=head1 BUGS

There is currently now way to specify defaults other than to edit the
program text.

You cannot show revisions and diffs of files which have been removed
in the MAIN branch. (This is a limitation of C<cvsweb.cgi>.)

=head1 AUTHOR

Martin Kammerhofer <mkamm@gmx.net>

