#! /usr/bin/perl
#
# Plot the history of a debian package from the changelog, displaying
# when each release of the package occurred, and who made each release.
# To make the graph a little more interesting, the debian revision of the
# package is used as the y axis.
#
# Pass this program the changelog(s) you wish to be plotted.
#
# GPL copyright 1999 by Joey Hess <joey@kitenet.net>

use Date::Parse;
use Getopt::Long;
use IO::File;
use POSIX qw{tmpnam};
use lib '@pkgdatadir@';
use checkgettext;

setlocale(LC_MESSAGES(), "");
textdomain("devscripts");

my $progname;
($progname=$0) =~ s|.*/||;

my $usage = gettext(<<'__end__');
Usage: plotchangelog [options] changelog ..
	-v	  --no-version	  Do not show package version information.
	-m	  --no-maint	  Do not show package maintainer information.
	-u        --urgency       Use larger points for higher urgency uploads.
	-l        --linecount     Make the Y axis be number of lines in the
	                          changelog.
	-g "commands"             Pass "commands" on to gnuplot, they will be
	--gnuplot="commands"      added to the gnuplot script that is used to 
				  generate the graph.
	-s file   --save=file     Save the graph to the specified file in
	                          postscript format.
	          --verbose       Outputs the gnuplot script.
                  --help          Show this message.
                  --version       Display version and copyright information.
__end__

my $versioninfo = sprintf gettext(<<'__end__'), $progname, '@VERSION@';
This is %s, from the Debian devscripts package, version %s
This code is copyright 1999 by Joey Hess <joey@kitenet.net>.
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the
GNU General Public License, version 2 or later.
__end__

my ($no_version, $no_maintainer, $gnuplot_commands,
    $save_filename, $verbose, $linecount, $bugcount, $help, $showversion)="";

my $ret=GetOptions(
		   "no-version|v", \$no_version,
		   "no-maint|m", \$no_maintainer,
		   "gnuplot|g=s", \$gnuplot_commands,
		   "save|s=s", \$save_filename,
		   "urgency|u", \$show_urgency,
		   "verbose", \$verbose,
		   "l|linecount", \$linecount,
		   "b|bugcount", \$bugcount,
		   "help", \$help,
		   "version", \$showversion,
);

if ($help) {
    print $usage;
    exit 0;
}

if ($showversion) {
    print $versioninfo;
    exit 0;
}

if (! $ret || !@ARGV) {
    print STDERR $usage;
    exit 1;
}

my %data;
my ($package, $version, $maintainer, $date, $urgency)=undef;
my $data_tmpfile=tmpfile();
my $script_tmpfile=tmpfile();
my %pkgcount;
my $c;

# Changelog parsing.
foreach (@ARGV) {
    if (/\.gz$/) {
	open F,"zcat $_|" || die "$_: $!";
    }
    else {
	open F,$_ || die "$_: $!";
    }

    while (<F>) {
	chomp;
	# Note that some really old changelogs use priority, not urgency.
	if (/^(\w+.*?)\s+\((.*?)\)\s+.*?;\s+(?:urgency|priority)=(.*)/i) {
	    $package=lc($1);
	    $version=$2;
	    if ($show_urgency) {
		$urgency=$3;
		if ($urgency=~/high/i ne undef) {
		    $urgency=2;
		}
		elsif ($urgency=~/medium/i ne undef) {
		    $urgency=1.5;
		}
		else {
		    $urgency=1;
		}
	    }
	    else {
		$urgency=1;
	    }
	    undef $maintainer;
	    undef $date;
	    $c=0;
	}
	elsif (/^ -- (.*?)  (.*)/) {
	    $maintainer=$1;
	    $date=str2time($2);

	    # Strip email address.
	    $maintainer=~s/<.*>//;
	    $maintainer=~s/\(.*\)//;
	    $maintainer=~s/\s+$//;
	}
	elsif (/^(\w+.*?)\s+\((.*?)\)\s+/) {
	    print STDERR sprintf gettext("Parse error on \"%s\"\n"), $_;
	}
	elsif ($linecount && /^  /) {
	    $c++; # count changelog size.
	}
	elsif ($bugcount && /^  /) {
	    # count bugs that were said to be closed.
	    my @bugs=m/#\d+/g;
	    $c+=$#bugs+1;
	}

	if (defined $package && defined $version &&
	    defined $maintainer && defined $date && defined $urgency) {
	    $data{$package}{$pkgcount{$package}++}=
		[$linecount || $bugcount ? $c : $version,
		 $maintainer, $date, $urgency];
	    undef($package, $version, $maintainer, $date, $urgency);
	}
    }

    close F;
}

my $header=q{
set key below title "key" box
set timefmt "%m/%d/%Y %H:%M"
set xdata time
set format x "%m/%y"
set yrange [0 to *]
};
if ($linecount) {
    $header.="set ylabel '" . gettext("Changelog length") . "'\n";
}
elsif ($bugcount) {
    $header.="set ylabel '" . gettext("Bugs closed") . "'\n";
}
else {
    $header.="set ylabel '" . gettext("Debian version") . "'\n";
}
if ($save_filename) {
    $header.="set terminal postscript color solid\n";
    $header.="set output '$save_filename'\n";
}
my $script="plot ";
my $data='';
my $index=0;
my %maintdata;

# Note that "lines" is used if we are also showing maintainer info,
# otherwise we use "linespoints" to make sure points show up for each
# release anyway.
my $style = $no_maintainer ? "linespoints" : "lines";

foreach $package (keys %data) {
    my $oldmaintainer="";
    my $oldversion="";
    # It's crucial the output is sorted by date.
    foreach $i (sort {$data{$package}{$a}[2] <=> $data{$package}{$b}[2]}
		keys %{$data{$package}}) {
	my $v=$data{$package}{$i}[0];
	$maintainer=$data{$package}{$i}[1];
	$date=$data{$package}{$i}[2];
	$urgency=$data{$package}{$i}[3];

	my $y;

	# If it's got a debian revision, use that as the y coordinate.
	if ($v=~m/(.*)-(.*)/) {
	    $y=$2;
	    $version=$1;
	}
	else {
	    $y=$v;
	}

	# Now make sure the version has no more than 1 decimal point in
	# it. Otherwise, the "set label" command below could fail.
	# This also deals with version numbers of debian-only packages
	# to some extent.
	($y)=$y=~m/(^[^.]*(?:\.[^.]*)?)/;
		
	if (lc($maintainer) ne lc($oldmaintainer)) {
	    $oldmaintainer=$maintainer;
	}
		
	my ($sec, $min, $hour, $mday, $mon, $year)=localtime($date);
	my $x=($mon+1)."/$mday/".(1900+$year)." $hour:$min";
	$data.="$x\t$y\n";
	$maintdata{$oldmaintainer}{$urgency}.="$x\t$y\n";
		
	if ($oldversion ne $version && ! $no_version) {
	    # Upstream version change. Label it.
	    $header.="set label '$version' at '$x',$y left\n";
	    $oldversion=$version;
	}
    }
    $data.="\n\n"; # start new dataset
    # Add to plot command.
    $script.="'$data_tmpfile' index $index using 1:3 title '$package' with $style, ";
    $index++;
}

# Add a title.
my $title.="set title '";
$title.=$#ARGV > 1 ? gettext("Graphing Debian changelogs") :
    gettext("Graphing Debian changelog");
$title.="'\n";

# Annoyingly, we have to use 2 temp files. I could just send everything to
# gnuplot on stdin, but then the pause -1 doesn't work.
open (DATA, ">$data_tmpfile") || die "$data_tmpfile: $!";
open (SCRIPT, ">$script_tmpfile") || die "$script_tmpfile: $!";
print DATA $data;
if (! $no_maintainer) {
    foreach $maintainer (sort keys %maintdata) {
	foreach $urgency (sort keys %{$maintdata{$maintainer}}) {
	    print DATA $maintdata{$maintainer}{$urgency}."\n\n";
	    $script.="'$data_tmpfile' index $index using 1:3 title '$maintainer' with points pointsize ".(1.5 * $urgency).", ";
	    $index++;
	}	
    }
}
$script=~s/, $/\n/;
$script=qq{
$header
$title
$gnuplot_commands
$script
};
$script.="pause -1 '" . gettext("Press Return to continue.") . "'\n"
    unless $save_filename;
print SCRIPT $script;
print $script if $verbose;
close SCRIPT;
close DATA;

system "gnuplot",$script_tmpfile;
unlink $script_tmpfile,$data_tmpfile;

# Safely get a temporary file.
sub tmpfile {
    do { 
	$name=tmpnam();
    } until $fh=IO::File->new($name,O_RDWR|O_CREAT|O_EXCL);
    return $name;
}
