#!/usr/bin/perl

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

my $IFCONFIG = "/sbin/ifconfig";
my $GATED= "/usr/sbin/gated";

my $fn = "/usr/local/federation/etc/gated.conf";
my @live_ones;

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

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

my $f = new IO::File(">$fn") || die "Can't open $fn: $!\n";
print $f <<END;
smux off;
rip off;
ospf on {
    backbone {
END
print $f "\tinterface $cif { passive; };\n";
print $f <<END;
    };
    area 0.0.0.2 {
	authtype none;
END
foreach my $i (@live_ones) {
    print $f "\tinterface $i { priority 1; };\n";
}
print $f <<END;
    };
};
END
$f->close();

$f = new IO::File(">/proc/sys/net/ipv4/ip_forward") || 
    die "Can't open sys forwarding file:$!\n";
print $f "1\n";
$f->close();
system("$GATED -f $fn");

