#!/usr/bin/perl
use Newt qw(NEWT_FLAG_BORDER NEWT_ANCHOR_LEFT NEWT_ANCHOR_LEFT NEWT_GRID_FLAG_GROWX NEWT_GRID_FLAG_GROWY NEWT_FLAG_MULTIPLE NEWT_KEY_F12 NEWT_KEY_F11 NEWT_EXIT_HOTKEY);
use NOCpulse::Object;
use lib qw(/etc/rc.d/np.d);
use NOCpulse::Config;
use PhysCluster;
use Data::Dumper;

#####################################################
# Kluged in constants - need to move these somewhere.
my $orahome = '/home/oracle/OraHome1';
my $dbd = 'Oracle';
my $dbname = 'npcfdb';
my $nologdbname = 'npnolog';
my $username = 'guest';
my $password = 'guest';
my $protocol = 'https';
my $path = '/cgi-bin/fetch_nocpulseini.cgi';
my $privdev = 'lo';
#####################################################
 
$Object::config = NOCpulse::Config->new('/etc/rc.d/np.d/SysV.ini');
Object->defaultDebugObject->addstream(LEVEL=>1);

# Tried being graceful - the thing cores in certain circumstances
# that I just don't wanna figure out right now. So - we leave here
# with exits in appropriate places.
while ( 1 ) {
	my $cluster = PhysCluster->newInitialized;
	my $node;
	if (defined($cluster->objectNamed('PhysNode',1))) {
		$node = $cluster->objectNamed('PhysNode',1);
	} else {
		$node = PhysNode->newInitialized;
		$node->set_id(1);
		$cluster->add('PhysNode',$node);
	}
	my $ipaddr;
	if (defined($node->objectNamed('IpAddr',1))) {
		$ipaddr = $node->objectNamed('IpAddr',1);
	} else {
		$ipaddr = IpAddr->newInitialized;
		$ipaddr->set_id(1);
		$node->add('IpAddr',$ipaddr);
	}
	my $privip;
	if (defined($node->objectNamed('PrivateIpAddr','0'))) {
		$privip = $node->objectNamed('PrivateIpAddr','0');
	} else {
		$privip = PrivateIpAddr->newInitialized;
		$privip->set_id(0);
		$privip->set_dev($privdev);
		$node->add('PrivateIpAddr',$privip);
	}

	my $boxtype;
	if (-f '/etc/redhat-release') {
		open(FILE,'/etc/redhat-release');
		my $line = join('',<FILE>);
		close(FILE);
		my @parts = split(' ',$line);
		$boxtype = $parts[1];
	}
	if ( (! $boxtype) or ($boxtype eq 'Hat')) {
		# "Hat" is for testing on a box that isn't an NP box (Red Hat)
		$boxtype = "Backend_MOC";
	}
	my $npinitype = 'local';
	if ( $boxtype eq 'Satellite' ) {
		$npinitype = 'remote';
	}

	my $npiniconf;
	if ( $npinitype eq 'local' ) {
		if (! defined($cluster->objectNamed('LocalConfig','0'))) {
			$npiniconf = LocalConfig->newInitialized;
			$npiniconf->set_id(0);
			$npiniconf->set_dbd($dbd);
			$npiniconf->set_dbname('npcfdb');
			$npiniconf->set_orahome($orahome);
			$npiniconf->set_username($username);
			$npiniconf->set_password($password);
			$cluster->add('LocalConfig',$npiniconf);
		}
	} else {
		if (! defined($cluster->objectNamed('RemoteConfig','0'))) {
			$npiniconf = RemoteConfig->newInitialized;
			$npiniconf->set_id(0);
			$npiniconf->set_protocol($protocol);
			$npiniconf->set_path($path);
			$cluster->add('RemoteConfig',$npiniconf);
		}
	}


	my $nodenum = '1';
	if ( -f '/etc/satname' ) {
		open(FILE,'/etc/satname');
		$nodenum = join('',<FILE>);
		close(FILE);
	}
	my $nodenumControl = Newt::Entry(3,undef,$nodenum);
	my $ipaddress = $ipaddr->get_addr;
	my $ipaddrControl  = Newt::Entry(15,undef,$ipaddress);
	my $netdev = $ipaddr->get_dev;
	my $netdevControl = Newt::Entry(5,undef,$netdev);
	my $cidrmask = $ipaddr->get_mask;
	my $cidrmaskControl = Newt::Entry(3,undef,$cidrmask);
	my $hostname = $node->get_hostname;
	my $hostnameControl = Newt::Entry(20,undef,$hostname);
	my $gateway = $ipaddr->get_gate;
	my $gatewayControl = Newt::Entry(15,undef,$gateway);
	my $ntpservers = $cluster->get_ntpservers;
	my $ntpserversControl = Newt::Entry(20,undef,$ntpservers);
	my $nameservers = $cluster->get_nameservers;
	my $nameserversControl = Newt::Entry(20,undef,$nameservers);
	my $nssearch= $cluster->get_nssearchpath;
	my $nssearchControl = Newt::Entry(20,undef,$nssearch);
	my $smonaddr= $cluster->get_smonaddr;
	my $smonaddrControl = Newt::Entry(15,undef,$smonaddr);
	my $smonfqdn= $cluster->get_smonfqdn;
	my $smonfqdnControl = Newt::Entry(20,undef,$smonfqdn);
	my $portaladdr= $cluster->get_portalAddress;
	my $portaladdrControl = Newt::Entry(15,undef,$portaladdr);
	my $supersputControl;
	if ($cluster->get_superSputEnabled) {
		$supersputControl = Newt::Checkbox("","*");
	} else {
		$supersputControl = Newt::Checkbox("");
	}

	Newt::Init();
	Newt::Cls();
	Newt::PushHelpLine();
	Newt::DrawRootText(0, 0, "NOCpulse Node Configuration");
	my $panel = Newt::Panel(5,15,'Node Configuration');

	$panel->Add(0,0,Newt::Label('Node number'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,0,$nodenumControl,NEWT_ANCHOR_LEFT);
	$panel->Add(2,0,Newt::Label('Net device'),NEWT_ANCHOR_LEFT);
	$panel->Add(3,0,$netdevControl,NEWT_ANCHOR_LEFT);
	$panel->Add(0,2,Newt::Label('IP address'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,2,$ipaddrControl,NEWT_ANCHOR_LEFT);
	$panel->Add(2,2,Newt::Label('CIDR mask'),NEWT_ANCHOR_LEFT);
	$panel->Add(3,2,$cidrmaskControl,NEWT_ANCHOR_LEFT);
	$panel->Add(0,4,Newt::Label('Gateway addr'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,4,$gatewayControl,NEWT_ANCHOR_LEFT);
	$panel->Add(2,4,Newt::Label('Engine server address'),NEWT_ANCHOR_LEFT);
	$panel->Add(3,4,$smonaddrControl,NEWT_ANCHOR_LEFT);

	if ($boxtype ne 'Satellite') {
		$panel->Add(0,5,Newt::Label('Hostname'),NEWT_ANCHOR_LEFT);
		$panel->Add(1,5,$hostnameControl,NEWT_ANCHOR_LEFT);
	} else {
		$panel->Add(0,5,Newt::Label('Enable SuperSput?'),NEWT_ANCHOR_LEFT);
		$panel->Add(1,5,$supersputControl,NEWT_ANCHOR_LEFT);
	}

	$panel->Add(0,6,Newt::Label('Name servers'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,6,$nameserversControl,NEWT_ANCHOR_LEFT);
	if ($npinitype eq 'local') {
		$panel->Add(0,7,Newt::Label('NTP servers'),NEWT_ANCHOR_LEFT);
		$panel->Add(1,7,$ntpserversControl,NEWT_ANCHOR_LEFT);
	} else {
		$panel->Add(0,7,Newt::Label(''),NEWT_ANCHOR_LEFT);
		$panel->Add(1,7,Newt::Label(''),NEWT_ANCHOR_LEFT);
	}
	$panel->Add(0,8,Newt::Label('NS search path'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,8,$nssearchControl,NEWT_ANCHOR_LEFT);
	$panel->Add(0,10,Newt::Label('Engine FQDN'),NEWT_ANCHOR_LEFT);
	$panel->Add(1,10,$smonfqdnControl,NEWT_ANCHOR_LEFT);
	if ($npinitype eq 'local') {
		$panel->Add(0,11,Newt::Label('Portal address'),NEWT_ANCHOR_LEFT);
		$panel->Add(1,11,$portaladdrControl,NEWT_ANCHOR_LEFT);
	} else {
		$panel->Add(0,11,Newt::Label(''),NEWT_ANCHOR_LEFT);
		$panel->Add(1,11,Newt::Label(''),NEWT_ANCHOR_LEFT);
	}
	$panel->Add(0,12,Newt::Label(''),NEWT_ANCHOR_LEFT);
	$panel->Add(1,12,Newt::Label(''),NEWT_ANCHOR_LEFT);
	my $cancelbutton = Newt::Button('Cancel',0);
	$cancelbutton->Tag("CANCEL");
	$panel->Add(3,12,$cancelbutton,NEWT_ANCHOR_LEFT,0,0,1);
	my $savebutton = Newt::Button('Save',0);
	$savebutton->Tag("SAVE");
	$panel->Add(4,12,$savebutton,NEWT_ANCHOR_LEFT,0,0,1);
	my ($reason,$data) = $panel->Run;
	Newt::Finished();
	if ($reason ne NEWT_EXIT_HOTKEY) {
		if ( $data->Tag() eq 'CANCEL' ) {
			print "Changes aborted\n";
			exit;
		}
	}
	# Now move data from form back into cluster
	#my $nodenum = '1';
	if ( $nodenumControl->Get() ) {
		open(FILE,'>/etc/satname');
		print FILE $nodenumControl->Get();
		close(FILE);
	}
	$ipaddr->set_addr($ipaddrControl->Get() || undef);
	$ipaddr->set_dev($netdevControl->Get() || undef);
	$ipaddr->set_mask($cidrmaskControl->Get() || undef);
	$node->set_hostname($hostnameControl->Get() || undef);
	$ipaddr->set_gate($gatewayControl->Get() || undef);
	$cluster->set_ntpservers($ntpserversControl->Get() || undef);
	$cluster->set_nameservers($nameserversControl->Get() || undef);
	$cluster->set_nssearchpath($nssearchControl->Get() || undef);
	$cluster->set_smonaddr($smonaddrControl->Get() || undef);
	$cluster->set_smonfqdn($smonfqdnControl->Get() || undef);
	$cluster->set_portalAddress($portaladdrControl->Get() || undef);
	$cluster->set_smontestaddr($cluster->get_smonaddr);
	$cluster->set_smontestfqdn($cluster->get_smonfqdn);
	$cluster->set_sshaddr($cluster->get_smonaddr);
	$cluster->set_sshmask('0.0.0.0');
	$cluster->set_sshfqdn($cluster->get_smonfqdn);
	$cluster->set_haFailoverEnabled(0);
	if ( $supersputControl->Checked() ) {
		$cluster->set_superSputEnabled(1);
	} else {
		$cluster->set_superSputEnabled(0);
	}

	# Validate the data
	my $portalip = $portaladdrControl->Get();
	if ( $cluster->isValid and (($npinitype eq 'remote') or ($portalip)) ) {
		# Write the data
		$cluster->writeToFile;
		if ($npinitype eq 'local') {
			open(FILE,">$orahome/network/admin/tnsnames.ora");
			print FILE "$dbname= \n";
			print FILE "  (DESCRIPTION = \n";
			print FILE "      (ADDRESS = (PROTOCOL = TCP)(HOST = $portalip )(PORT = 1521)) \n";
			print FILE "    (CONNECT_DATA = (SID = $dbname)) \n";
			print FILE "  ) \n\n";
			print FILE "$nologdbname= \n";
			print FILE "  (DESCRIPTION = \n";
			print FILE "      (ADDRESS = (PROTOCOL = TCP)(HOST = $portalip )(PORT = 1521)) \n";
			print FILE "    (CONNECT_DATA = (SID = $nologdbname) \n";
			print FILE "  ) \n";
			close(FILE);
		}
		exit;
	} else {
		$| = 1;
		if (! (($npinitype eq 'remote') or ($portalip))) {
			print "\nPortal IP address required for this node type\n";
		}
		print "\nPress a key to continue editing: ";
		my $junk;
		read(STDIN,$junk,1);
	}
}
