#!/s/std/bin/perl

use CSL::Pg;
use CSL::DBs;
use DBI;

use Storable;

my %list;

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


#
# Add to hash splat attached hosts
#
my $query = $dbh->prepare ("select p.device, p.interface, s.hostname 
from splat.port_mapping p join splat.hosts h on p.idf = h.idf and 
p.jacknum = h.jacknum join splat.hostnames s on h.part = s.part and 
h.interface_number = s.interface_number"); 
$query->execute ();
while ($entry = $query->fetchrow_hashref())
  {
	$list{"$entry->{device}:$entry->{interface}"} = $entry->{hostname};
        #printf("%s %s %s\n",$entry->{device},$entry->{interface},$entry->{hostname});
	
  }
$query->finish;



#
# Add to hash ports on devices not managed by splat
#
$query = $dbh->prepare ("select device,interface  from splat.switch_interfaces where device in (select 
name from splat.devices where type = 'switch' and splat_managed is NULL)");
$query->execute ();
while ($entry = $query->fetchrow_hashref())
  {
  	$list{"$entry->{device}:$entry->{interface}"} = "unmanaged";
  }
$query->finish;

#
# Delete from hash ports that are uplinks
#
$query = $dbh->prepare ("select * from splat.uplinks");
$query->execute ();
while ($entry = $query->fetchrow_hashref())
  {
	delete $list{"$entry->{device}:$entry->{interface}"};	
  }
$query->finish;




$dbh->disconnect;

store \%list, '/tmp/splat-attach-list';
