#!/usr/bin/perl
use warnings;
use strict;
use lib '../include';
use POSIX;

my $delaytime = 30;
my $poll_frequency = 60;

# Send a command to switch and return the data recvied from the switch
sub switch_exec($$) {
	my ($cmd, $conn) = @_;

	# Send the command and get data from switch
	my @data = $conn->cmd($cmd);
	my @lines = ();
	foreach my $line (@data) {
		# Remove escape-7 sequence
		$line =~ s/\x1b\x37//g;
		push (@lines, $line);
	}

	return @lines;
}

sub mylog {
	my $msg = shift;
	my $time = POSIX::ctime(time);
	$time =~ s/\n.*$//;
	printf STDERR "[%s] %s\n", $time, $msg;
}

if ($#ARGV != 1) {
	die("Error in arguments passed\n".
	       "./ssendfile.pl addr configfile\n");
}

my $conn = nms::switch_connect($ARGV[0]);
if (!defined($conn)) {
	die("Could not connect to switch.\n");
}

open(CONFIG, $ARGV[1]);
while (<CONFIG>) {
	my $cmd = $_;
	$cmd =~ s/[\r\n]+//g;
	print "Executing: `$cmd`\n";
#	if ($cmd =~ /ip ifconfig swif0 (\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3})/) {
#		print "New ip: $1\n";
#		$conn->cmd(	String => $cmd,
#				Timeout => 3);
#		$conn = nms::switch_connect($1);
#		if (!defined($conn)) {
#			die "Could not connect to new ip: $1\n";
#		}
#	}
#	else {
		my @data = switch_exec($cmd, $conn);
		foreach my $line (@data) {
			$line =~ s/[\r\n]+//g;
			print "$line\n";
		}
#	}
}
