#!/usr/bin/env perl

# Copyright (C) 2009  Joshua Hoblitt

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.01';

use Console;

use Getopt::Long qw( GetOptions :config auto_help auto_version );
use Pod::Usage qw( pod2usage );

my (
    $add,
    $console,
    $delete,
    $verbose,
);

GetOptions(
    'add|a'         => \$add,
    'console|c=s'   => \$console,
    'delete|d'      => \$delete,
    'verbose|v'     => \$verbose,
) || pod2usage( 2 );

my $hostname = shift @ARGV;
pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;

my $rcfile = Console::find_rcfile();
my $c = Console::read_rcfile($rcfile);
my $hosts = $c->get_hosts;

if (defined $add) {
    go_add();
    exit();
}

if (defined $delete) {
    go_delete();
    exit();
}

# default
go_list();
exit();


sub go_list
{
    pod2usage(
        -msg        => "--console is meaningless in this context",
        -exitval    => 2,
    ) if defined $console;

    if (defined $hostname) {
        die "unknown hostname: $hostname" unless exists $hosts->{$hostname}->{console};
        printf("%-20s %s\n", "# hostname", "console");
        printf("%-20s %s\n", $hostname, $hosts->{$hostname}->{console});
    } else {
        printf("%-20s %s\n", "# hostname", "console");
        foreach my $name (keys %$hosts) {
            printf("%-20s %s\n", $name, $hosts->{$name}->{console});
        }
    }
}

sub go_add
{
    pod2usage(
        -msg        =>"--console option is required under --add",
        -exitval    => 2,
    ) unless defined $console;
    pod2usage(
        -msg        => "<hostname> option is required under --add",
        -exitval    => 2,
    ) unless defined $hostname;
    pod2usage(
        -msg        => "--add and --delete are incompatible options",
        -exitval    => 2,
    ) if defined $delete;

    Console::add_host(config => $c, hostname => $hostname, console => $console);
}

sub go_delete
{
    pod2uage(
        -msg        => "--console and --delete are incompatible options",
        -exitval    => 2,
    ) if defined $console;
    pod2uage(
        -msg        => "<hostname> option is required under --delete",
        -exitval    => 2,
    ) unless defined $hostname;
    pod2uage(
        -msg        => "--add and --delete are incompatible options",
        -exitval    => 2,
    ) if defined $add;

    Console::delete_host(config => $c, hostname => $hostname);
}

__END__

=pod

=head1 NAME

console-config - configure the console util

=head1 SYNOPSIS

    console [--verbose] [<hostname>]

    or

    console [--verbose] --add --console <console hostname> <hostname>

    or

    console [--verbose] --delete <hostname>

=head1 DESCRIPTION

This program is used to configure the ".rc" file for the console util.

=head1 OPTIONS

=over 4

=item * --add|-a

Flag used to add a entry to the ".rc" file.  Requires both C<<hostname>> and
C<--console>.

=item * --console|-c <console hostname>

The name of the console to connect to for serial console access to
C<<hostname>>.

=item * --verbose|-r

Turns on informational/debugging messages to be sent to the
C<stderr>/C<stdout>.

=back

=head1 RCFILE

This program will attempt to read an C<rcfile> from F<$HOME/.Consolerc>.

=head1 CREDITS

Just me, myself, and I.

=head1 SUPPORT

Please contact the author directly via e-mail.

=head1 AUTHOR

Joshua Hoblitt <jhoblitt@cpan.org>

=head1 COPYRIGHT

Copyright (C) 2009  Joshua Hoblitt.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

The full text of the license can be found in the L<perlgpl> Pod as supplied
with Perl 5.8.1 and later.

=head1 SEE ALSO

L<Config::YAML>, L<Console>, L<console>, L<console-ipp-defaults>

=cut
