#!/usr/bin/perl

use strict;
use IO::File;
use IO::Pipe;

my $IFCONFIG = "/sbin/ifconfig";
my $OSPFD= "/usr/local/sbin/ospfd";
my $SYSCTL= "/sbin/sysctl";
my $PKG_ADD="/usr/sbin/pkg_add";

my $fn = "/usr/local/federation/etc/ospfd.conf";
my @pkgs = ('/usr/local/federation/pkg/libevent-1.3b.tbz', 
    '/usr/local/federation/pkg/openospfd-4.0.tbz');
my @live_ones;

for my $p (@pkgs) {
    die "No such package $p\n" unless -r $p;
}

system("$PKG_ADD " . join(" ", @pkgs));

chomp (my $cif = `/usr/local/etc/emulab/control_interface`);

my $ifp = new IO::Pipe();
$ifp->reader("$IFCONFIG");
while (<$ifp>) {
    /^(\S+):.*UP/ && do {
	push(@live_ones, $1) unless $1 eq $cif || $1 eq "lo0";
    };
}
$ifp->close();

my $f = new IO::File(">$fn") || die "Can't open $fn: $!\n";
print $f <<END;
area 0.0.0.1 {
    interface $cif {
	passive 
    }
}
END
print $f <<END;
area 0.0.0.2 {
    auth-type none
END
foreach my $i (@live_ones) {
    print $f "\tinterface $i {\n\t}\n";
}
print $f "}\n";
$f->close();
system("chmod 600 $fn");
system("$SYSCTL -w net.inet.ip.forwarding=1");
system("$OSPFD -f $fn");

