#!/usr/bin/perl
use NOCpulse::SatCluster;
use NOCpulse::Config;
use NOCpulse::Object;
use Data::Dumper;

NOCpulse::Object::SystemIni('/etc/NOCpulse.ini');
my $config = NOCpulse::Config->new;
my $cluster = SatCluster->newInitialized($config);
$cluster->refreshHAView;
open(SPCONF,'>/etc/spread.conf');
print SPCONF "DebugFlags = { PRINT EXIT }\n";
print SPCONF "EventLogFile = /var/log/spread.log\n";
print SPCONF "EventTimeStamp\n";
print SPCONF "DangerousMonitor = true\n";

# WARNING: EVIL EVIL HACK HERE. FIX IS SOME SORT OF NET+NODE DESCRIP FILE
# GENERATED OUT OF PROVISIONING SCRIPT:
open(SPNAME,'>/etc/spread.procname');
my $pppaddr = $cluster->get_currentNode->addressOnInterface('plip0');
if ($pppaddr eq '63.121.136.253') {
	print SPNAME 'sat-ha1'
} elsif ($pppaddr eq '63.121.136.254') {
	print SPNAME 'sat-ha2'
} else {
	print SPNAME 'localhost'
}
close(SPNAME);

if ($cluster->get_isHA) {
	print SPCONF "Spread_Segment 63.121.136.253:4803 {\n";
	print SPCONF "        sat-ha1               63.121.136.253  {\n";
	print SPCONF "           D 63.121.136.253\n";
	print SPCONF "           C 127.0.0.1\n";
	print SPCONF "        }\n";
	print SPCONF "}\n";
	print SPCONF "Spread_Segment 63.121.136.254:4803 {\n";
	print SPCONF "        sat-ha2               63.121.136.254  {\n";
	print SPCONF "           D 63.121.136.254\n";
	print SPCONF "           C 127.0.0.1\n";
	print SPCONF "        }\n";
	print SPCONF "}\n";
} else {
	print SPCONF "Spread_Segment  127.0.0.255:4803 {\n";
	print SPCONF "        localhost               127.0.0.1  {\n";
	print SPCONF "           D 127.0.0.1\n";
	print SPCONF "           C 127.0.0.1\n";
	print SPCONF "        }\n";
	print SPCONF "}\n";
}

close(SPCONF);


