#!/usr/bin/perl

use strict;
use warnings;
use App::Tel;
use Getopt::Std;
use v5.10;

__PACKAGE__->main() unless caller;

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

    my %opts;
    # need usage if no arguments supplied
    # -l logging
    # -d debug
    # -x <script file>
    # -c <command;command;command>
    # -t timeout
    # -m method
    # -p port

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

    $self->{timeout} = $opts{t} ? $opts{t} : 90;
    $self->{opts} = \%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 handle_backspace {
    ${$_[0]}->send("\b");
    return 1;
}

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

1;
