#!/usr/local/bin/perl

# Copyright 2001-2003, Paul Johnson (pjcj@cpan.org)

# This software is free.  It is licensed under the same terms as Perl itself.

# The latest version of this software should be available from my homepage:
# http://www.pjcj.net

require 5.6.1;

use strict;
use warnings;

our $VERSION = "0.22";

use Devel::Cover::DB 0.22;

use Getopt::Long;

use Pod::Usage;

my $Options =
{
    coverage => [],
    delete   => 0,
    file     => [],
    option   => [],
    report   => "",
    summary  => 1,
};

sub get_options
{
    die "Bad option" unless
        GetOptions($Options,            # Store the options in the Options hash.
                   "write:s" => sub
                   {
                       @$Options{qw(write summary)} = ($_[1], 0)
                   },
                   qw(
                       coverage=s
                       delete!
                       help|h!
                       file=s
                       info|i!
                       option=s
                       report=s
                       summary!
                       version|v!
                     ));
}

sub main
{
    get_options;

    my $format = "Devel::Cover::Report::\u$Options->{report}";
    if (length $Options->{report})
    {
        eval ("use $format");
        if ($@)
        {
            print "Error: $Options->{report} ",
                  "is not a recognised output format\n\n$@";
            exit 1;
        }
    }

    print "$0 version $VERSION\n" and exit 0 if $Options->{version};
    pod2usage(-exitval => 0, -verbose => 0)  if $Options->{help};
    pod2usage(-exitval => 0, -verbose => 2)  if $Options->{info};

    my $dbname = shift @ARGV || "cover_db";

    if ($Options->{delete})
    {
        for my $del ($dbname, @ARGV)
        {
            print "Deleting database $del\n";
            my $db = Devel::Cover::DB->new(db => $del);
            $db->delete;
        }
        exit 0;
    }

    print "Reading database from $dbname\n";
    my $db = Devel::Cover::DB->new(db => $dbname);

    for my $merge (@ARGV)
    {
        print "Merging database from $merge\n";
        my $mdb = Devel::Cover::DB->new(db => $merge);
        $db->merge($mdb);
    }

    if (exists $Options->{write})
    {
        $dbname = $Options->{write} if length $Options->{write};
        print "Writing database to $dbname\n";
        $db->write($dbname);
    }

    return unless $Options->{summary} || $Options->{report};

    $Options->{coverage}    = [ $db->collected ] unless @{$Options->{coverage}};
    $Options->{show}        = { map { $_ => 1 } @{$Options->{coverage}} };
    $Options->{show}{total} = 1 if keys %{$Options->{show}};

    $db->calculate_summary(map { $_ => 1 } @{$Options->{coverage}});

    print "\n\n";

    $db->print_summary(@{$Options->{coverage}}) if $Options->{summary};

    return unless length $Options->{report};

    @{$Options->{file}} = sort $db->cover->items unless @{$Options->{file}};

    $format->report($db, $Options)
}

main

__END__

=head1 NAME

cover - report coverage statistics

=head1 SYNOPSIS

 cover -help -info -version -summary
       -report report_format -option option
       -file filename -coverage criterion -write [db]
       coverage_database [coverage_database ...]

=head1 DESCRIPTION

Report coverage statistics in a variety of formats.

The summary option produces a short textual summary.  Other reports are
available by using the report option.

The following reports are currently available:

 text                  - detailed textual summary
 html                  - detailed HTML reports

=head1 OPTIONS

The following command line options are supported:

 -summary              - give summary report          (default on)
 -report report_format - report format required       (default none)
 -option               - options for report

 -file filename        - only report on the file      (default all)
 -write [db]           - write the merged database    (default off)
 -delete               - drop database(s)             (default off)

 -coverage criterion   - report on criterion  (default all available)

 -h -help              - show help
 -i -info              - show documentation
 -v -version           - show version

=head1 DETAILS

Any number of coverage databases may be specified on the command line.
These databases will be merged and the reports will be based on the
merged information.  If no databases are specified the default database
(cover_db) will be used.

The -write option will write out the merged database.  If no name is
given for the new database, the first database read in will be
overwritten.  When this option is used no reports are generated by
default.

Specify -file options to report on specific files.  Specify -coverage
options to report on specific criteria.  By default all available
information on all criteria in all files will be reported.

=head1 EXIT STATUS

The following exit values are returned:

0   All operaions were completed successfully.

>0  An error occurred.

=head1 SEE ALSO

 Devel::Cover

=head1 BUGS

Did I mention that this is alpha code?

See the BUGS file.

=head1 VERSION

Version 0.22 - 2nd September 2003

=head1 LICENCE

Copyright 2001-2003, Paul Johnson (pjcj@cpan.org)

This software is free.  It is licensed under the same terms as Perl itself.

The latest version of this software should be available from my homepage:
http://www.pjcj.net

=cut
