#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts;
use Getopt::Long;
use Pod::Usage;

=head1 NAME

    la-sql-updsshfp - Update ssh finger print from ssh-keygen output

=head1 SYNOPSIS

    la-sql-updsshfp host

    la-sql-updsshfp host -f file

=cut

GetOptions(
    'c|config=s'        => \my $config,
    'b|base=s'          => \my $base,
    'f|file=s'          => \my $file,
    'help'              => sub { pod2usage(0) },
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base basename

Query this specific base instead of the default one.

=item -f|--file file

Specify the file containing the ssh-keygen output. By default data are read from
standard input.

=back

=cut

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase && $labase->load or die "Cannot load base";

$labase->unexported(1);

my $host = $ARGV[0];

my $object = $labase->get_object('nethost', $host) or die 
    "Can't find nethost $host\n";

my $text = '';
while (my $line = <STDIN>) {
    $text .= $line;
}

$object->set_fields('sshfpUpdate', $text) or die "Can update base\n";

$labase->commit;

print "Done\n";
