#!/usr/bin/perl
#  $Id: db_master_conf 22408 2011-04-04 16:38:57Z teetov $
# -----------------------------------------------------------------------------
#  Add Master IP address into postgres pg_hba.conf config file
# ------------------------------------------------------------------------------
#  Author: A.baranetsky
#  QA by:  Christopher C Gettings
#  Copyright: videoNEXT Network Solution LLC, 2010
# -----------------------------------------------------------------------------

use strict;
use warnings;


$#ARGV >= 0  || die "master IP adress is required";
my $pgsql_hba = "$ENV{APL_DB_DATA}/pg_hba.conf";

#
# Check if master IP adress is in pg_hba.conf
#
open(CONF,$pgsql_hba) ||die "Cant open: $pgsql_hba";
my @conf=<CONF>;
close CONF;


my $masterIp = $ARGV[0];
my $ind = 0;

foreach(@conf) {
	if (/^host\s+all\s+all\s+$masterIp\s+255.255.255.255\s+trust/) {
		$ind = 1;
		last;
	}
}


#
# Add master IP it it's missing
#
if (!$ind) {
    system("$ENV{APL}/db/bin/db_access_conf MASTER:$masterIp");
    sleep 3; # Wait for postmaster to restart
}
