#!/usr/bin/env perl

# Copyright (C) 2009  Joshua Hoblitt

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

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

use Config::YAML;
use Console;

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

my (
    $user,
    $verbose,
);

GetOptions(
    'user|u=s'  => \$user,
    'verbose|v' => \$verbose,
) || pod2usage( 2 );

my $hostname = shift @ARGV;
die "a hostname is required" unless defined $hostname;

$user ||= $ENV{USER};

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

my $console = $hosts->{$hostname}->{console};
die "error with rcfile: $rcfile, console for $hostname is not defined"
    unless defined $console;

my $cmd = "ssh $user:$hostname\@$console";
print $cmd, "\n" if $verbose;
system($cmd);

__END__

=pod

=head1 NAME

console - access a remote serial console

=head1 SYNOPSIS

    console [--user <username>] [--verbose] <hostname>

=head1 DESCRIPTION

This program looks for mounted volumes, at a configurable interval, and records
statistics about them into a database.

=head1 OPTIONS

=over 4

=item * --db|-d <db name>

Name of database (C<namespace>) to write too.

Optional if defined in the F<.nebdiskdrc> file or the appropriate environment
variable is set.

=item * --user|-u <db username>

Username to authenticate with.

Optional if defined in the F<.nebdiskdrc> file or the appropriate environment
variable is set.

=item * --pass|-p <db password>

Password to authenticate with.

Optional if defined in the F<.nebdiskdrc> file or the appropriate environment
variable is set.

=item * --debug

This flag prevents the program from "daemonizing" so output can be sent to
C<stdout>/C<stderr> and a debugger used on the running process.

=item * --pidfile <filename>

Filename to log the running daemon's PID too.

Optional, if this option is omitted and no value is defined in the
F<.nebdiskrc> then no pidfile is created.

=item * --stop|-s

Uses the pidfile to kill an already running daemon.

=item * --restart|-r

Uses the pidfile to kill an already running daemon and then starts a new
instance.

=item * --verbose|-r

Turns on informational/debugging messages to be sent to the
C<stderr>/C<stdout>.  This option is probably not very usefully unless combined
with C<--debug>.

=back

=head1 ENVIRONMENT

These environment variables may be used in place of the specified command line
options.  All command line option will override the corresponding environment
value.

=over 4

=item * C<NEB_DB>

Equivalent to --db|-d

=item * C<NEB_USER>

Equivalent to --user|-u

=item * C<NEB_PASS>

Equivalent to --pass|-p 

=back

=head1 RCFILE

This program will attempt to read an C<rcfile> from F<$HOME/.nebdiskrc> and if
found will use it's values to override program defaults.  Please not that the
precedence for values is: command line, rcfile, environment variables.

The format of the C<rcfile> is in L<YAML> format and uses the following values:

    ---
    db: nebulous
    dbpass: '@neb@'
    dbuser: nebulous
    mounts:
      - /mnt
      - /tmp
      - /usr
    pidfile: /var/tmp/nebdiskd
    poll_interval: 5

The values C<db>, C<dbpass>, C<dbuser>, and C<pidfile> have the same semantics
as their command line and/or environment variable equivalents.

=over 4

=item * C<mounts>

A list of "paths" to C<stat(2)> before mounted volumes are polled.  The propose
of this option is to attempt to keep "automounted" volumes mounted while this
program is running.

This value may be omitted or left blank.

=item * C<poll_interval>

The number of seconds to wait between checks for mounted volumes.

This value may be omitted or left blank.  The default value is C<60>s.

=back

=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>

=cut
