#!/usr/bin/perl

use strict;
use warnings;
use App::Tel;
use Getopt::Long qw (:config pass_through);
use Getopt::Std;
use Pod::Usage;
use v5.10;

__PACKAGE__->main() unless caller;

=head1 NAME

tel - script for router logins

=head1 SYNOPSIS

tel gw1-my-device gw2-my-device ...

Options:
    -h          help message
    -l          logging
    -d          debug mode
    -v          version
    -p          connection port
    -m          connection method
    -x          runs script file
    -c          runs commands
    -a          autocommands
    -t          sets the timeout
    -s          sleep between commands

=head1 OPTIONS

=over 4

=item -c

Runs a series of commands that are separated by semicolons.

Example: tel -c "show ver; sh run" gw1-my-device

=item -l

Enables logging to a file /tmp/gw1-my-device.log.

Example: tel -l gw1-my-device

=item -d

Enables debugging, which will warn you if some of the runtime modules fail to
load.

Example: tel -d gw1-my-device

=item -x

Runs a script file, which is a list of commands for the device.  Can be
specified multiple times to run multiple scripts.

Example: tel -x changes.txt gw1-my-device

=item -a

Specify autocommands to run.  This overrides any commands loaded from profiles
and is used for all devices during the session.

Example: tel -a "sh run int vlan35; conf t" gw1-my-device

=item -p

Specify connection port.  This overrides any specific port in loaded profiles
and is used for all devices during the session.

Example: tel -p 22 gw1-my-devices

=item -m

Specify connection method.  This overrides specific methods in loaded profiles
and is used for all devices during the session.

Example: tel -m ssh gw1-my-devices

=item -t

Specify timeout in seconds.  The default is 90 seconds.  This overrides
specific methods in loaded profiles and is used for all devices during the
session.

Example: tel -t 30 gw1-my-devices

=item -s

Specify timeout in seconds to sleep between commands.  This only applies to
commands specified for -x, -c or -a.  This will use Time::HiRes if it's
installed for sub-second sleep values like 0.5.

Example: tel -s 3 -x myfile.txt gw1-my-devices

=back

=cut

sub main {
    my $self = App::Tel->new();
    $self->{config} = $self->load_config();

    my %opts;
    GetOptions("xcript=s@" => \$opts{x});   # scripts to run
    getopts('vhlda:c:t:m:p:s:', \%opts);

    $self->{timeout} = $opts{t} ? $opts{t} : 90;
    $self->{opts} = \%opts;

    if ($opts{v}) {
        VERSION_MESSAGE();
    }

    die HELP_MESSAGE() if ($opts{h});
    die pod2usage() if (!scalar @ARGV && !scalar keys %opts);

    for (@ARGV) {
        # default profile always loads before anything else.  replace == 1
        $self->profile('default', 1);
        $self->hostname($_);
        $self->login($self->hostname);
        if ($self->connected) {
            $self->enable();
            $self->logging() if ($self->{opts}->{l});
            $self->control_loop();
            $self->disconnect();
        }
    }
}

=head2 HELP_MESSAGE

Boilerplate routine for Getopt::Std

=cut

sub HELP_MESSAGE {
    pod2usage( -verbose => 1 );
}

=head2 VERSION_MESSAGE

Boilerplate routine for Getopt::Std

=cut

sub VERSION_MESSAGE {
    say "tel version " . $App::Tel::VERSION;
}

1;
