# heartbeat-lib.pl
# Common functions for heartbeat tool configuration

do '../web-lib.pl';
&init_config();

$ha_cf = $config{'ha_cf'} ? $config{'ha_cf'} : "$config{'ha_dir'}/ha.cf";
$haresources = $config{'haresources'} ? $config{'haresources'}
				      : "$config{'ha_dir'}/haresources";
$authkeys = $config{'authkeys'} ? $config{'authkeys'}
				: "$config{'ha_dir'}/authkeys";
$resource_d = $config{'resource_d'} ? $config{'resource_d'}
				    : "$config{'ha_dir'}/resource.d";

sub get_ha_config
{
local @rv;
local $lnum = 0;
open(CONF, $ha_cf);
while(<CONF>) {
	s/\s+$//;
	s/#.*$//;
	if (/^(\S+)\s+(\S.*)$/) {
		push(@rv, { 'name' => $1,
			    'value' => $2,
			    'line' => $lnum });
		}
	$lnum++;
	}
close(CONF);
return @rv;
}

# find(name, &config)
sub find
{
local @rv;
foreach $c (@{$_[1]}) {
	if ($c->{'name'} eq $_[0]) {
		push(@rv, $c->{'value'});
		}
	}
return wantarray ? @rv : @rv==0 ? undef : $rv[0];
}

# find_struct(name, &config)
sub find_struct
{
local @rv;
foreach $c (@{$_[1]}) {
	if ($c->{'name'} eq $_[0]) {
		push(@rv, $c);
		}
	}
return wantarray ? @rv : @rv==0 ? undef : $rv[0];
}

# save_directive(&config, name, &values)
sub save_directive
{
local $lref = &read_file_lines($ha_cf);
local @old = &find_struct($_[1], $_[0]);
for($i=0; $i<@old || $i<@{$_[2]}; $i++) {
	if ($i >= @old) {
		# adding a directive
		push(@$lref, "$_[1]\t$_[2]->[$i]");
		push(@{$_[0]}, { 'name' => $_[1],
				 'value' => $_[2]->[$i],
				 'line' => scalar(@$lref)-1 });
		}
	elsif ($i >= @{$_[2]}) {
		# removing a directive
		splice(@$lref, $old[$i]->{'line'}, 1);
		splice(@{$_[0]}, &indexof($old[$i], @{$_[0]}), 1);
		&renumber($_[0], $old[$i]->{'line'}, -1);
		}
	else {
		# updating a directive
		splice(@$lref, $old[$i]->{'line'}, 1, "$_[1]\t$_[2]->[$i]");
		$old[$i]->{'value'} = $_[2]->[$i];
		}
	}
}

# renumber(&config, line, offset)
sub renumber
{
foreach $c (@{$_[0]}) {
	if ($c->{'line'} > $_[1]) {
		$c->{'line'} += $_[2];
		}
	}
}

sub list_resources()
{
local @rv;
local $lnum = 0;
open(RES, $haresources);
while(<RES>) {
	s/\s+$//;
	s/#.*$//;
	local @res = split(/\s+/, $_);
	if (@res > 0) {
		local $r = { 'node' => shift(@res),
			     'line' => $lnum };
		foreach $v (@res) {
			if ($v =~ /^[0-9\.\/]+$/) {
				push(@{$r->{'ips'}}, $v);
				}
			elsif ($v =~ /^IPaddr::(\S+)$/) {
				push(@{$r->{'ips'}}, $1);
				}
			else {
				push(@{$r->{'servs'}}, $v);
				}
			}
		push(@rv, $r);
		}
	$lnum++;
	}
close(RES);
return @rv;
}

sub create_resource
{
local $lref = &read_file_lines($haresources);
push(@$lref, &resource_line($_[0]));
&flush_file_lines();
}

sub modify_resource
{
local $lref = &read_file_lines($haresources);
$lref->[$_[0]->{'line'}] = &resource_line($_[0]);
&flush_file_lines();
}

sub delete_resource
{
local $lref = &read_file_lines($haresources);
splice(@$lref, $_[0]->{'line'}, 1);
&flush_file_lines();
}

sub resource_line
{
local @l = ( $_[0]->{'node'} );
push(@l, @{$_[0]->{'ips'}});
push(@l, @{$_[0]->{'servs'}});
return join(" ", @l);
}

sub get_auth_config
{
local $rv;
open(AUTH, $authkeys);
while(<AUTH>) {
	s/\r|\n//g;
	s/#.*$//;
	local @l = split(/\s+/, $_);
	if (@l > 0) {
		$rv->{shift(@l)} = \@l;
		}
	}
close(AUTH);
return $rv;
}

sub save_auth_config
{
local %auth;
open(AUTH, ">$authkeys");
if ($_[0]->{'auth'}) {
	print AUTH "auth ",join(" ", @{$_[0]->{'auth'}}),"\n";
	map { $auth{$_}++ } @{$_[0]->{'auth'}};
	}
foreach $k (keys %{$_[0]}) {
	if ($k ne 'auth') {
		print AUTH "# " if (!$auth{$k});
		print AUTH "$k ",join(" ", @{$_[0]->{$k}}),"\n";
		}
	}
close(AUTH);
}

1;

