#!/usr/bin/env perl
use strict;
use warnings;

use IO::Interactive qw(is_interactive);

use CPAN::Audit;

our $VERSION = "1.001";

__PACKAGE__->run( @ARGV ) unless caller;

use constant EXIT_NORMAL =>  0;
use constant EXIT_USAGE  =>  2;
use constant EXIT_BASE   => 64;

sub run {
	my( $class, @args ) = @_;

	my( $opts ) = $class->process_options( \@args );

	$class->usage(EXIT_NORMAL) if $opts->{help};

	if( $opts->{version} ) {
		print "$0 version $VERSION using:\n\tCPAN::Audit     @{[ CPAN::Audit->VERSION ]}\n\tCPAN::Audit::DB @{[ CPAN::Audit::DB->VERSION ]}\n";
		exit(EXIT_NORMAL);
		}

	if( $opts->{fresh_check} ) {
		require CPAN::Audit::FreshnessCheck;
		CPAN::Audit::FreshnessCheck->import
		}

	my $command = shift @args;
	$class->usage(EXIT_USAGE) unless defined $command;

	my %extra = (
		interactive => is_interactive(),
		);

	my $audit = CPAN::Audit->new( %$opts, %extra );

	my $advisory_count = $audit->command( $command, @args );

	my $exit_code = do {
		   if( $advisory_count == 0 ) { EXIT_NORMAL }
		elsif( $advisory_count > 0  ) { EXIT_BASE + $advisory_count }
		};

	exit( $exit_code );
	}

sub process_options {
	my( $class, $args ) = @_;
	require Getopt::Long;

	my $options = {};

	my %params;
	my $params = {
		'ascii'       => \$params{ascii},
		'f|fresh'     => \$params{fresh_check},
		'help|h'      => \$params{help},
		'no-color'    => \$params{no_color},
		'no-corelist' => \$params{no_corelist},
		'perl'        => \$params{include_perl},
		'quiet|q'     => \$params{quiet},
		'verbose|v'   => \$params{verbose},
		'version'     => \$params{version},
		};

	my $ret = Getopt::Long::GetOptionsFromArray( $args, $options, %$params ) or $class->usage(EXIT_USAGE);

	\%params;
	}

sub usage {
	require Pod::Usage;
	require FindBin;

	my( $exit_code ) = @_;
	no warnings qw(once);
	Pod::Usage::pod2usage( -input => $FindBin::Bin . "/" . $FindBin::Script );
	exit( $exit_code );
	}

__END__

=head1 NAME

cpan-audit - Audit CPAN modules

=head1 SYNOPSIS

cpan-audit [options] [command]

Commands:

    module         [version range]    audit module with optional version range (all by default)
    dist|release   [version range]    audit distribution with optional version range (all by default)
    deps           [directory]        audit dependencies from the directory (. by default)
    installed                         audit all installed modules
    show           [advisory id]      show information about specific advisory

Options:

    --ascii         use ascii output
    --freshcheck|f  check the database for freshness (CPAN::Audit::FreshnessCheck)
    --help|h        show the help message and exit
    --no-color      switch off colors
    --no-corelist   ignore modules bundled with perl version
    --perl          include perl advisories
    --quiet         be quiet
    --verbose       be verbose
	--version       show the version and exit

Examples:

    cpan-audit dist Catalyst-Runtime
    cpan-audit dist Catalyst-Runtime 7.0
    cpan-audit dist Catalyst-Runtime >5.48

    cpan-audit module Catalyst 7.0

    cpan-audit deps .
    cpan-audit deps /path/to/distribution

    cpan-audit installed
    cpan-audit installed local/

    cpan-audit show CPANSA-Mojolicious-2018-03

=head1 DESCRIPTION

C<cpan-audit> is a command line application that checks the modules or
distributions for known vulnerabilities. It is using its internal
database that is automatically generated from a hand-picked database
L<https://github.com/briandfoy/cpan-security-advisory>.

C<cpan-audit> does not connect to anything, that is why it is
important to keep it up to date. Every update of the internal database
is released as a new version. Ensure that you have the latest database
by updating L<CPAN::Audit> frequently; the database can change daily.
You can use enable a warning for a possibly out-of-date database by
adding C<--freshcheck>, which warns if the database version is older
than a month:

	% cpan-audit --freshcheck ...
	% cpan-audit -f ...

	% env CPAN_AUDIT_FRESH_DAYS=7 cpan-audit -f ...

=head2 Finding dependencies

C<cpan-audit> can automatically detect dependencies from the following
sources:

=over

=item C<Carton>

Parses C<cpanfile.snapshot> file and checks the distribution versions.

=item C<cpanfile>

Parses C<cpanfile> taking into account the required versions.

=back

It is assumed that if the required version of the module is less than
a version of a release with a known vulnerability fix, then the module
is considered affected.

=head2 Exit values

In prior versions, C<cpan-audit> exited with the number of advisories
it found. Starting with 1.001, if there are advisories found, C<cpan-audit>
exits with 64 added to that number.

=over 4

=item * 0 - normal operation

=item * 2 - problem with program invocation, such as bad switches or values

=item * 64+n - advisories found. Subtract 64 to get the advisory count

=back

=head1 LICENSE

Copyright (C) Viacheslav Tykhanovskyi.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
