#!/usr/local/bin/perl
#
# Maintain the global caches for ARP, bridge tables,
# and ether details
#
# $Id: 3cglobalcache,v 1.4 1999/06/03 15:09:22 trockij Exp $
#
# Copyright (C) 1998 Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use A3Com::BridgeTable;
use A3Com::ARP;
use A3Com::EtherDetail;
use Getopt::Std;

sub help;
sub read_list;
sub update_arp_cache;
sub update_bridge_table;
sub update_ether_detail;

getopts ("hdb:a:D:p:", \%opt);

help() if ($opt{"h"});

my %GLOBALPARMS = ();
if ($opt{"p"}) {
    $GLOBALPARMS{"GLOBALCACHEDIR"} = $opt{"p"};
}

#
# bridge table
#
update_bridge_table ($opt{"b"}) if ($opt{"b"});


#
# ARP table
#
update_arp_cache ($opt{"a"}) if ($opt{"a"});


#
# port details
#
update_ether_detail ($opt{"D"}) if ($opt{"D"});

exit;


sub read_list {
    my $f = shift;

    open (IN, $f) || return ();

    my @l;

    while (<IN>) {
    	next if (/^\s*#/ || /^\s*$/);
	chomp;
	push (@l, $_);
    }
    close (IN);

    return (@l);
}


sub help {
    print <<EOF;
usage: 3cglobalcache [-h] [-d] [-b file] [-a file] [-D file] [-p path]
    -d        debug
    -b host   host to get bridge tables from
    -D host   host to get ethernet detail
    -a file   file of hosts to update ARP cache from
    -a host   host to update ARP cache from
    -p path   path to global cache dir
    -h        this help

EOF
}


sub update_arp_cache {
    my $f = shift;

    if (-f $f) {
	@arp_list = read_list ($f);
    } else {
    	@arp_list = ($f);
    }

    foreach my $sw (@arp_list) {

print "ARP $sw\n" if ($opt{"d"});

    	my $n = new A3Com::ARP (
	    SWITCH => $sw,
	    GLOBALCACHE => 0,
	    CACHE => 0,
	    %GLOBALPARMS,
	);

	if (!defined $n) {
	    # error
	    next;
	}

	my %a = $n->arp_table;

	$n->globalcache (1);

    	$n->file_write;

	if ($n->error ne "") {
	    # error writing
	    print STDERR "error writing global arp cache: " . $n->error . "\n";
	    next;
	}

	my $h = $n->load_history ();

	if ($n->error ne "") {
	    # error reading
	}

	$h = $n->merge_current_with_history (\%a, $h, time);

	$n->save_history ($h);

	if ($n->error ne "") {
	    # error writing
	    print STDERR "error writing global arp history for $sw: " . $n->error . "\n";
	    next;
	}
    }
}


sub update_bridge_table {
    my $sw = shift;

print "BRIDGETABLE $sw\n" if ($opt{"d"});

    my $n = new A3Com::BridgeTable (
	SWITCH => $sw,
	GLOBALCACHE => 0,
	CACHE => 0,
	%GLOBALPARMS,
    );

    my %bt = $n->btable;

    next if ($n->error ne "");

    $n->globalcache (1);

    $n->file_write;

    if ($n->error ne "") {
	# error writing
	print STDERR "error writing global brige cache for $sw: " . $n->error . "\n";
	next;
    }
}


sub update_ether_detail {
    my $f = shift;

    @detail_list = read_list ($f);
    foreach my $sw (@detail_list) {
    }
}
