#!/usr/bin/perl

use v5.14;
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use DNSSec qw(keyinfo);
use List::Util qw(max);
use if $ENV{DEBUG} => 'Smart::Comments';

sub pretty {
    my $hash = shift;
    my @fields = do {
	my @x;
	while (@_) {
	    push @x, [shift, shift];
	}
	@x;
    };
    my $maxl = max map { length $_->[0] } @fields;

    my @pretty;
    foreach my $f (@fields) {
	my $sub = $f->[1];
	push @pretty, sprintf '%*s: %s',
	    $maxl, $f->[0], $hash->$sub,
    }
    return @pretty;
}

sub main {
    my $domain;
    GetOptions(
        'h|help' => sub { pod2usage(-exitval => 0) },
        'm|man'  => sub {
            pod2usage(
                -exitval   => 0,
                -verbose   => 2,
                -noperldoc => system('perldoc -V >/dev/null'),
              ),
              ;
        },
    ) and $domain = shift @ARGV // pod2usage;
    say $domain, "\n", '-' x length $domain;

    # get the dnskeys directory from the public available information
    my @ki = keyinfo $domain;
#    use Data::Dumper;
#    die Dumper \@ki;

    foreach my $ki (@ki) {
	say '** DNS Key';
	say "\t", join "\n\t", pretty $ki->{key}, 
		Algorithm => 'algorithm',
		Flags => 'flags',
		Protocol => 'protocol' ,
		Key => 'key',
		Keytag => 'keytag';
	say '** DNS Key Digest';
	say "\t", join "\n\t", pretty $ki->{digest},
		Algorithm => 'algorithm',
		Digest => 'digest',
		'Digest Type' => 'digtype',
		Keytag => 'keytag',
    }

}



exit main @ARGV if not caller;

__END__
=head1 NAME
 
 dnssec-info - get various informations about a dnssec domain

=head1 SYNOPSIS

 dnssec-info <domain>

=head1 DESCRIPTION

B<dnssec-info> retrieves varios information about a dnssec domain.

=head1 OPTIONS

=over

=item B<-h>|B<--help>
=item B<-m>|B<--man>

Helpful information.

=back

=cut
