package H1::ZTEMSAN;
use Moose;

with 'H1::ZTE';
use Data::Dump qw(dump);

sub connect {
	my ( $self,$ip ) = @_;

	my $t = Net::Telnet->new( Timeout => 20, Port => 1123, Prompt => '/#/' );

	$t->dump_log('/tmp/log') if $ENV{DEBUG};

	warn "open";
	$t->open( $ip );

	$t->waitfor('/Login:/');
	$t->print('root');
	$t->waitfor('/Password:/');
	$t->print('root');
	$t->waitfor('/#/');

	warn "login OK";

	return $t;
}

sub parse {
	my ( $self, $out ) = @_;

	my $hash;
	my $section = '';
	my $last_line;
	foreach my $line ( split(/[\n\r]+/, $out) ) {
		warn "# $line\n";
		if ( $line =~ m/^(\S+)\s*:\s*(\S*.*?)\s*$/ ) {
			my ($n,$v) = ($1,$2);
			$n =~ s/\(.+\)//;
			if ( $v =~ s/\s+(\S+)\s*:\s*(\S*.*?)\s*$// ) {
				# strip second column
				my ($n2,$v2) = ($1,$2);
				$n2 =~ s/\(.+\)//;
				$hash->{ $section . $n2 } = $v2;
				warn "## $n2 = $v2\n";
			}
			$hash->{ $section . $n } = $v;
			warn "## $n = $v\n";
		} elsif ( $line =~ m/^-+$/ ) {
			$section = $last_line . '_';
		}
		$last_line = $line;
	}

	warn "## hash = ",dump $hash;

	return $hash;
}


sub commands {{
	'show adsl port %s' => [],
	'show adsl port %s line-config' => [],
	'show adsl port %s physical-table' => [],
	'show adsl port %s perf-data' => [],
}}

sub fixup_row {
	my ( $self, $row ) = @_;
	# cleanup column names
	foreach my $n ( grep { m/[:\s]/ } keys %$row ) {
		my $v = delete $row->{$n};
		$n =~ s/\s+/_/g;
		$n =~ s/:$// && $v =~ s/\skbps$//; # special case
		$row->{$n} = $v;
	}

	return $row;
}

1;

