#!/usr/bin/perl

use strict;
use warnings;
use App::Tel;
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
    -t          sets the timeout

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

Example: tel -x changes.txt 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

=cut

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

    my %opts;
    getopts('vhldx:c:t:m:p:', \%opts);

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

    if ($opts{v}) {
        print "tel version " . $App::Tel::VERSION . "\n";
    }

    die pod2usage( -verbose => 1 ) 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->{'profile'}->{autocmds});
            $self->disconnect();
        }
    }
}

sub usage {


}

sub handle_backspace {
    ${$_[0]}->send("\b");
    return 1;
}

sub handle_ctrl_z {
    ${$_[0]}->send("exit\r");
    return 1;
}

1;
