#!/usr/bin/perl -w
use strict;

# $Id: debget-madison,v 1.2 2005-07-11 21:22:33 roderick Exp $

# XXX
#
# - unimplemented switches
#   -a, --architecture=ARCH    only show info for ARCH(s)
#   -b, --binary-type=TYPE     only show info for binary TYPE
#   -c, --component=COMPONENT  only show info for COMPONENT(s)
#   -g, --greaterorequal       show buildd 'dep-wait pkg >= {highest version}' info
#   -G, --greaterthan          show buildd 'dep-wait pkg >> {highest version}' info
#   -h, --help                 show this help and exit
#   -r, --regex                treat PACKAGE as a regex
#   -s, --suite=SUITE          only show info for this suite
#   -S, --source-and-binary    show info for the binary children of source pkgs
#   ARCH, COMPONENT and SUITE can be comma (or space) separated lists, e.g.
#     --architecture=m68k,i386

use Debian::Debget	qw(
    $Debug
    $Exit
    $Me
    cmp_debian_versions
    getopt
    madison
    xwarn
);

my $Version	= q$Revision: 1.2 $ =~ /(\d\S+)/ ? $1 : '?';

my @Option_spec = (
    'debug|d+'	=> \$Debug,
    'help'	=> sub { usage() },
    'version'	=> sub { print "$Me version $Version\n"; exit },
);

my $Usage = <<EOF;
usage: $Me [switch]... package...
switches:
     --debug		turn debugging on (multiple times for more)
     --help		show this and then die
     --version		show the version ($Version) and exit
Use \`perldoc $Me\' to see the full documentation.
EOF

sub usage {
    warn "$Me: ", @_ if @_;
    # Use exit() rather than die(), as Getopt::Long does eval().
    print STDERR $Usage;
    exit 1;
}

sub init {
    $| = 1;
    getopt -bundle, @Option_spec or usage if @ARGV;
}

# auric% ./madison xgraph
#     xgraph |   11.3.2-2 |        stable | source, alpha, arm, hppa, i386, ia64, m68k, mips, mipsel, powerpc, s390, sparc
#     xgraph |   11.3.2-2 |       testing | source, alpha, arm, hppa, i386, ia64, m68k, mips, mipsel, powerpc, s390, sparc
#     xgraph |   11.3.2-2 |      unstable | source, alpha, arm, hppa, i386, ia64, m68k, mips, mipsel, powerpc, s390, sparc

# merkel% madison libc6
#      libc6 | 2.2.5-11.8 |     oldstable | arm, hppa, i386, m68k, mips, mipsel, powerpc, s390, sparc
#      libc6 | 2.3.2.ds1-22 |        stable | arm, hppa, i386, m68k, mips, mipsel, powerpc, s390, sparc
#      libc6 | 2.3.2.ds1-22 |       testing | arm, hppa, i386, m68k, mips, mipsel, powerpc, s390, sparc
#      libc6 | 2.3.2.ds1-22 |      unstable | arm, hppa, i386, m68k, mips, mipsel, powerpc, s390, sparc
#      libc6 |    2.3.4-2 |  experimental | mips
#      libc6 |    2.3.4-3 |  experimental | sparc
#      libc6 |    2.3.5-1 |  experimental | i386, mipsel, powerpc

sub work {
    my ($package) = @_;

    my $r = madison $package;
    if (!$r || !%$r) {
	xwarn "couldn't get version info for $package\n";
	return;
    }

    my $fmt = "%10s | %10s | %13s | %s\n";
    my %ver_dist = %$r;

    for my $ver (sort cmp_debian_versions keys %ver_dist) {
    	for my $dist (sort keys %{ $ver_dist{$ver} }) {
	    printf $fmt, $package, $ver, $dist,
		join ", ", @{ $ver_dist{$ver}{$dist} };
	}
    }

}

sub main {
    init;
    @ARGV or usage "no package specified\n";
    work $_ for @ARGV;
    return 0;
}

$Exit = main || $Exit;
$Exit = 1 if $Exit && !($Exit % 256);
exit $Exit;

__END__

=head1 NAME

debget-madison - show what versions of a package are in Debian

=head1 SYNOPSIS

B<debget-madison> [I<switch>]... I<package>...

=head1 DESCRIPTION

B<debget-madison> shows what versions of a (source or binary) package
are available in Debian.  It's similar to the B<madison> tool, but it
can be used from anywhere on the Internet.

=head1 OPTIONS

=over 4

=item B<--debug>

Turn debugging on.  Specify multiple times for more detail.

=item B<--help>

Show the usage message and die.

=item B<--version>

Show the version number and exit.

=back

=head1 SEE ALSO

debget(1), L<http://packages.debian.org>

=head1 AVAILABILITY

The code is licensed under the GNU GPL and distributed as part of Debian.

=head1 AUTHOR

Roderick Schertler <roderick@argon.org>

=cut
