#!/usr/bin/perl -w
#
#

use strict;
use lib '../include';

use Data::Dumper;

use nms qw(switch_connect switch_exec);
use ios;

#my $t = nms::ios_connect('62.148.36.12', 'c', 'c', 'c');

#print STDERR "No such network 192.168.1.0\n" if nms::ios_getroute($t, "192.168.1.0") == 0;

#nms::ios_close($t);

my $one = 0;

my $switchip;
#$switchip = "87.76.250.226" if $one;
$switchip = "192.168.2.150" if $one;
$switchip = "192.168.1.1" unless $one;
my $switch = switch_connect($switchip);

my $cmds1 = <<EOF
sys hostname es-3024
ip igmpsnoop enable

sys sw vlan type 802.1q
sys sw vlan1q svlan setentry MNGT 248 25 fixed tag
sys sw vlan1q svlan active 248
sys sw vlan1q svlan setentry e01-1sw 1 25 fixed untag

sys sw vlan1q svlan setentry MNGT 248 1 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 2 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 3 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 4 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 5 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 6 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 7 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 8 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 9 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 10 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 11 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 12 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 13 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 14 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 15 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 16 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 17 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 18 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 19 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 20 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 21 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 22 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 23 forbidden tag
sys sw vlan1q svlan setentry MNGT 248 24 forbidden tag
EOF
;
#
foreach (split(/\n+/, $cmds1)) {
	print "Sending: '$_'\n";
	switch_exec($_, $switch);
}

my $cmd;
$cmd = "ip ifconfig swif0 192.168.1.1/24" if $one;
$cmd = "ip ifconfig swif0 192.168.2.150/24" unless $one;
print "Sending '$cmd'\n";
my $pid = fork();
if ($pid == 0) {
	print "Sending ifconfig.. $cmd\n";
	switch_exec($cmd, $switch);
	exit 0;
} else {
	#switch_exec($cmd, $switch, 1);
	print "Sleeping...\n";
	sleep 1;
}
$switch->close();
print "Reconnecting... ";
$switchip = "192.168.1.1" if $one;
$switchip = "192.168.2.150" unless $one;
autoflush STDOUT 1;
print "Connecting to: $switchip...\n";
my $i = 1;
while(1) {
	$switch = switch_connect($switchip);
	
	#print (defined($switch)? $switch : "foo") ." <---- switch\n";
	if (defined($switch) || $switch) {
		last;
	}
	printf("Waited $i seconds...\r");
	$i++;
	sleep 1;
}
print "\n";

print $switch."\n";
$cmd = "sys sw vlan1q svlan cpu 248";
print "Sending '$cmd'\n";
#print "XXX: No we did not...";
#print Dumper($switch->cmd("ip ifconfig"));
#print Dumper($switch->cmd("ip route status"));
switch_exec($cmd, $switch, 1);
$switch->close();
