#!/usr/bin/env perl

use 5.14.2;
use warnings;

use Zonemaster::CLI;
use File::Spec;
use autodie;

sub read_conf_file {
    # Returns list of command line parameters. List can be empty.
    my ($conf_file) = @_;
    my @lines;
    open my $fh, '<', $conf_file;
    while (<$fh>) {
        chomp;
        next if /^\s*$/;
        next if /^\s*#/;
        push @lines, $_;
    };
    return @lines;
}

# Load default arguments from file in home directory, if any
# This must be loaded before any global file to make the local
# file take precedence
my $home_dir  = ((getpwuid($<))[7]) || $ENV{HOME};
my $home_conf_file = File::Spec->catfile($home_dir, '.zonemaster', 'cli.args');

if (-r $home_conf_file) {
    my @lines = read_conf_file ($home_conf_file);
    unshift @ARGV, @lines;
}

# Load default arguments from global file, if any
my @global_conf = (
    '/etc/zonemaster/cli.args',
    '/usr/local/etc/zonemaster/cli.args'
    ); # Order is significant.
my $global_conf_file;

for my $p (@global_conf) {
    if ( -e $p and -r $p ) {
        $global_conf_file = $p;
        last;
    }
}

if ( defined $global_conf_file ) {
    my @lines = read_conf_file ($global_conf_file);
    unshift @ARGV, @lines;

}

Zonemaster::CLI->new_with_options->run;

=head1 NAME

zonemaster-cli - run Zonemaster tests from the command line

=head1 SYNOPSIS

    zonemaster-cli zonemaster.net
    zonemaster-cli --test=delegation --level=info --no-time zonemaster.net
    zonemaster-cli --test=delegation/delegation01 --level=debug zonemaster.net
    zonemaster-cli --list_tests

=head1 DESCRIPTION

L<zonemaster-cli> is a command-line interface to the L<Zonemaster> test engine.
It takes instructions the user provides as command line arguments, transforms
them into suitable API calls to the engine, runs the test suite and prints the
resulting messages. By default, the messages will be translated by the engine's
translation module, with the corresponding timestamp and logging level when
printed. See the available options below.

=head1 OPTIONS

=over

=item -h -? --usage --help

Print the available command line switches, then exit.

=item --version

Print the versions of this program as well as the ones from the underlying 
L<Zonemaster> test engine, then exit.

=item --level=LEVEL

Specify the minimum level of a message to be printed. Messages with this level
(or higher) will be printed. The levels are, from highest to lowest: 
CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG, DEBUG2 and DEBUG3.
The lowest three levels (DEBUG) add a significant amount of messages to be shown.
They reveal some of the internal workings of the test engine, and are probably 
not useful for most users.

Default: NOTICE

=item --locale=LOCALE

Specify which locale to be used by the translation system. If not given, the
translation system itself will look at environment variables to try and guess.
If the requested translation does not exist, it will fallback to the local
locale, and if that doesn't exist either, to English.

=item --[no-]json

Print results as JSON instead of human language.

Default: off

=item --[no-]json_stream, --[no-]json-stream

Stream the results as JSON. Useful to follow the progress in a
machine-readable way.

Default: off

=item --[no-]json_translate, --[no-]json-translate

Deprecated since v2023.1, use --no-raw instead.

For streaming JSON output, include the translated message of the tag.

=item --[no-]raw

Print messages as raw dumps (message identifiers) instead of translating them
to human language.

=item --[no-]time

Print the timestamp for each message.

Default: on

=item --[no-]show_level, --[no-]show-level

Print the severity level for each message.

Default: on

=item --[no-]show_module, --[no-]show-module

Print the name of the module which produced the message.

Default: off

=item --[no-]show_testcase, --[no-]show-testcase

Print the name of the test case (test case identifier) which produced the message.

Default: off

=item --ns=NAME[/IP]

Provide information about a nameserver, for undelegated tests. The argument
must be either: (i) a domain name and an IP address, separated by a single
slash character (/), or (ii) only a domain name, in which case a A and AAAA
records lookup for that name is done in the live global DNS tree (unless 
overridden by --hints) and from which the results of that lookup will be used.

This switch can be given multiple times. As long as any of these switches
are present, their aggregated content will be used as the
entirety of the parent-side delegation information.

=item --hints=FILENAME

Name of a root hints file to override the defaults.

=item --save=FILENAME

Write the contents of the accumulated DNS packet cache to a file with the given name
after the testing suite has finished running.

=item --restore=FILENAME

Prime the DNS packet cache with the contents from the file with the given name
before starting the testing suite. The format of the file should be from one
produced by the --save switch.

=item --[no-]ipv4

Allow the sending of IPv4 packets.

Default: on

=item --[no-]ipv6

Allow the sending of IPv6 packets.

Default: on

=item --list_tests, --list-tests

Print all test cases listed in the test modules, then exit.

=item --test=MODULE, --test=MODULE/TESTCASE, --test=TESTCASE

Limit the testing suite to run only the specified tests.
This can be the name of a testing module, in which case all test cases from
that module will be run, or the name of a module followed by a slash and the
name of a test case (test case identifier) in that module, or the name of the
test case.
Can be specified multiple times.
This option is case-insensitive.

=item --stop_level=LEVEL, --stop-level=LEVEL

Specify the minimum severity level after which the testing suite is terminated.
The levels are, from highest to lowest: CRITICAL, ERROR, WARNING, NOTICE,
INFO, DEBUG, DEBUG2 and DEBUG3.

=item --profile=FILE

Override the Zonemaster Engine default profile data with values from
the given profile JSON file.

=item --ds=KEYTAG,ALGORITHM,TYPE,DIGEST

Provide a DS record for undelegated testing (that is, a test where the
delegating nameserver information is given via --ns switches). The four pieces
of data (keytag, algorithm, type, digest) should be in the same format they would
have in a zone file.

=item --[no-]count

Print a summary, at the end of a run, of the numbers of messages for each severity
level that were logged during the run.

Default: off

=item --[no-]progress

Print an activity indicator ("spinner"). Useful to know that something is
happening during a run.

Default: on (if the process' standard output is a TTY)

=item --encoding=ENCODING

Specify the character encoding that is used for command line arguments. This
will be used to convert non-ASCII names to IDNA format, on which the testing
suite will then be run.

The default value will be taken from the C<LC_CTYPE> environment variable if
possible, and set to UTF-8 if not.

=item --nstimes

Print a summary, at the end of a run, of the times (in milliseconds) the zone's
name servers took to answer.

=item --dump_profile, --dump-profile

Print the effective profile used in JSON format, then exit.

=item --sourceaddr4=IPADDR

Specify the source IPv4 address used to send queries.
Setting an IPv4 address not correctly configured on a local network interface
fails silently.

=item --sourceaddr6=IPADDR

Specify the source IPv6 address used to send queries.
Setting an IPv6 address not correctly configured on a local network interface
fails silently.

=item --[no-]elapsed

Print elapsed time (in seconds) at end of a run.

Default: off

=back

=head1 PROFILES

The testing and result analysis performed by Zonemaster Engine is always
guided by a profile.
Zonemaster Engine has a default profile with sensible defaults.
Zonemaster CLI allows users to override the default profile data with
values from a profile JSON file with the C<--profile> option.
For details on profiles and how they're respresented in files, see
L<Zonemaster::Engine::Profile>.

=head1 CONFIGURATION

If there is a readable file F</etc/zonemaster/cli.args> (Linux style), each line
in that file will be prepended as an argument on the command line. If no
F</etc/zonemaster/cli.args> is found (or is not readable) but
F</usr/local/etc/zonemaster/cli.args> (FreeBSD style) is found and readable then
that file will be used instead. Only one global file is loaded.

If there is a readable file F<.zonemaster/cli.args> in the user's home
directory, it will be used in the same way even when a global file has been
loaded. Any argument in user's F<cli.args> will override the same argument in the
global config file.

For example, if one would like to by default run with the log
level set to DEBUG and with translation to human-readable messages turned off,
one could put this in the config file:

   --raw
   --level=DEBUG

Only one argument per line. If the argument has a value there must be a "="
between argument and value. A line starting with "#" is a comment. Comments
cannot be added on lines with arguments.

Any arguments actually given on the command line will override what is in any of
the loaded config files.

=head1 SEE ALSO

L<Zonemaster>

=head1 AUTHOR

Calle Dybedahl <calle@init.se> and others from the Zonemaster project
