#!/usr/bin/perl
# Build user-friendly web-page

my $indexplace="/home/ftp/pub/fido/husky/debian/dists/unstable";
my $debhome="/home/ftp/pub/fido/husky/debian";
my $href="http://husky.sourceforge.net/debian";

# begin html index
open (OUT, "> $indexplace/index.html");
print OUT "<html>\n<head><title>Packages for unstable</title></head>\n<body>\n";
print OUT "For stable (woody) packages go here:\n";
print OUT "<a href=\"http://husky.sourceforge.net/debian/dists/stable/index.html\">stable</a>\n";
print OUT "<p><hr width=\"100%\">\n<pre>\n";

# scan packages
open (ls, "cd $indexplace; ls -1 -I\"*.html\" 2>/dev/null |");
while ( <ls> ) {
    chop;
    &parse_packages($_);
}
close (ls);

# end html index
print OUT "</pre>For stable (woody) packages go here:\n";
print OUT "<a href=\"http://husky.sourceforge.net/debian/dists/stable/index.html\">stable</a>\n";
print OUT "<hr width=\"100%\">\n<address>\n";
$tm = gmtime();
print OUT "Generated $tm UTC by <a href=\"$href/buildpage.pl\">buildpage 2.0</a> - Copyright (C) 2002\n";
print OUT "<a href=\"mailto:pavel\@collage.etel.ru\">Pavel Andreew</a>\n";
print OUT "</address>\n<br></p>\n</body>\n</html>\n";
close (OUT);
exit 0;

sub parse_packages {
    local ($pdir) = $_;
    local ($line, $source, $filename);

    open (IN, "$indexplace/$pdir/binary-i386/Packages");
    while ( <IN> ) {
	chop;
	$line = ch_ltgt( $_ );
	if ( $line =~ /^Package:.*/ ) {
	    &parse_changes($package, $source, $filename);
	    ( $key, $package ) = split( /: /, $line, 2);
	    $source = "";
	    $filename = "";
	}
	if ( $line =~ /^Source:.*/ ) {
	    ( $key, $source ) = split( /: /, $line, 2);
	}
	if ( $line =~ /^Filename:.*/ ) {
	    ( $key, $filename ) = split( /: /, $line, 2);
	    print OUT "$key: <a href=\"$href/$filename\">$filename</a>\n";
	}
	else {
	    print OUT "$line\n";
	}
    }    
    &parse_changes($package, $source, $filename);
    close (IN);
}

sub ch_ltgt {
    local ($line) = $_;
    $line =~ s/>/\&gt;/g;
    $line =~ s/</\&lt;/g;
    return $line;
}

sub parse_changes {
    local ($package) = $_[0];
    local ($source) = $_[1];
    local ($filename) = $_[2];
    local ($line, $prn);

    if ( $filename eq "" ) {
	return;
    };
    if ( $source ne "" ) {
	$filename =~ s/$package/$source/g;
    };
    $filename =~ s/.deb/.changes/g;

    open(CH, "$debhome/$filename") || return;
    $prn = "no";
    while ( <CH> ) {
	chop;
	$line = $_;
	if ( $line =~ /^Changes:.*/ ) {
	    $prn = "yes";
	    print OUT "<b>Last changes:</b>\n";
	}
	else {
	    if ( $line =~ /^Files:.*/ ) {
		$prn = "no";
	    }
	    else {
		if ($prn eq "yes") {
		    print OUT ch_ltgt($line)."\n";
		}
	    }
	}
    }
    close (CH);
    print OUT "\n";
}
