#!/s/std/bin/perl 

#
# outputs postscript
#
# takes arguments 

# idf    map all switches in IDF and core
# group  map all switches in and above specified layer
# room   map all switches in room, use splat.idfs to map idf numbers to room


#use strict;
use CSL::Pg;
use CSL::DBs;
use DBI;
use Fcntl;
use POSIX ":sys_wait_h";
use Getopt::Std;

my $stpgraph_command = "/p/lab/network/stpgraph/stpgraph";
my @switches;

my %opts;
my %idf_room_numbers;

getopt("vril",\%opts);

#if($opts{i} && $opts{g} || $opts{i} && $opts{r} || $opts{g} && $opts{r}){
#	print("You can only map one of idf,group,room\n\n");
#	usage();
#}

if(!$opts{v}){
        printf(STDERR "Using Default Vlan\n");
        $opts{v}=1;
};
printf(STDERR "My vlan is %s\n",$opts{v});

if($opts{i}){
	printf(STDERR "My IDF is %s\n",$opts{i});
};

if($opts{l}){
	printf(STDERR "My Layer is %s\n",$opts{l});
};

if($opts{r}){
	printf(STDERR "My Room is %s\n",$opts{r});
};

my $dbh = DBI->connect ($CSL::DBs::csdb) or die "Could not connect to database";

get_idf_room_data();

my $query = $dbh->prepare ("select * from splat.devices");
$query->execute ();

while (my $entry = $query->fetchrow_hashref)
  {

	# Adding algorithm
	#  Always add core devices
	#  Add other devices that match given flags
	#

	if($entry->{'network_layer'} eq "core"){
                        printf(STDERR "Adding core device %s to list\n",$entry->{'name'});
                        push @switches, $entry->{'name'};
                        next;
        }

        if($opts{i}){
		unless($entry->{'idf'} eq $opts{i}){
			printf(STDERR "Skipping device %s in idf %s which doesn't match passed idf %s\n",$entry->{'name'},$entry->{'idf'},$opts{i});
			next;	
		}
	}	

	if($opts{r}){
                unless($idf_room_numbers{$entry->{'idf'}} eq $opts{r}){
                        printf(STDERR "Skipping device %s in room %s which doesn't match passed room %s\n",$entry->{'name'},$idf_room_numbers{$entry->{'idf'}},$opts{r});
                        next;
                }
        }
	
	if($opts{l}){
		unless($entry->{'network_layer'} eq $opts{l}){
                        printf(STDERR "Skipping device %s of layer %s which doesn't match passed layer %s\n",$entry->{'name'},$device_layers{$entry->{'network_layer'}},$opts{l});                        
			next;
                }

	}


	printf(STDERR "Adding device %s to list\n",$entry->{'name'});
	push @switches, $entry->{'name'};

  }
$query->finish;
$dbh->disconnect;

$vlan = scalar $opts{v};

system("$stpgraph_command -cCSL@5 -g ps @switches");

exit();

sub usage(){
	print("usage: ./l2grapher.pl -v <VLAN> -i <IDF> -l <Layer> -r <Room>\n");
	print("		-v <VLAN> :  VLAN to use for spanning tree map\n");
	print("		-i <IDF>  :  Map core and switches from IDF\n");
        print("		-l <Layer>:  Map layer, includes higher layers\n");
        print("		-r <Room> :  Map core and switches from room \n");
	exit(1);
}

sub get_idf_room_data{
	my $query = $dbh->prepare ("select * from splat.idfs");
	$query->execute ();
	while (my $entry = $query->fetchrow_hashref){
        	$idf_room_numbers{$entry->{'name'}} = $entry->{'room'};	
	}
	$query->finish;
}

