#!/usr/bin/perl
require "ctime.pl"; 

#will return the process ID's belonging to postmaster, must suspend them all

	system("rm -f postmasterids");
	system("ps -C postmaster -o pid= > postmasterids");
#	system("sed -n '1,1p' postmasterids > postmasterid");


open (FILE,"postmasterids") || die "could not open postmasterids file\n$!"; 
$PID=""; 

while(<FILE>)
	{ 
	$PID=$_;	
#	print $PID;

	#kill -19 PID   SIGSTOP
	#stop the process to ensure no more writes to the database
	system ("kill -19 $PID");
	} 
close(FILE); 

	system ("cp -f /home/postgres/data/query_log /home/postgres/data/temp_pass_file");

#read every line with sed, find the queries, exclude the system messages and database vacuuming, pipe into pass_file
	system("sed -n '1~1p' /home/postgres/data/temp_pass_file | grep -a 'query:' |grep -v 'vacuum' | grep -v 'SQL_ASCII' | grep -v 'getdatabaseencoding' | grep -v 'version' >> /home/postgres/data/pass_file");


#make sure that there aren't any lingering queries in the log file that didn't get sent to the pass_file
	system("diff /home/postgres/data/query_log /home/postgres/data/temp_pass_file > /home/postgres/data/new_query_log");

#clean up a bit
	system("rm -f /home/postgres/data/temp_pass_file");
#one big log file
#	system("touch /var/log/postgreslog");
#	system("cat /home/postgres/data/query_log >> /var/log/postgreslog");
#suck out the contents but leave the file intact, postgres daemon is looking for only this file
#	system("sed 's/ *$//' /home/postgres/data/query_log > /home/postgres/data/query_log");
	
#make sure that the query_log is initialized with the results of the diff
	system("cat /home/postgres/data/new_query_log > /home/postgres/data/query_log");
	system("rm -f /home/postgres/data/new_query_log");
	system("chown postgres:postgres /home/postgres/data/query_log");
	system("chmod 664 /home/postgres/data/query_log");


open (FILE,"postmasterids") || die "could not open postmasterids file\n$!"; 
$PID=""; 
while(<FILE>)
	{ 
	$PID=$_;	
#	print $PID;
	#let the process continue
	#kill -18 PID	SIGCONT
	system ("kill -18 $PID");
	} 
close(FILE); 
system("rm -f postmasterids");


#now read the pass_file into the remote database
use Pg;
require "./rederhandler.pl"; 
$title="Postgres Redundancy Program";

$conn=Pg::connectdb("host=flintstone port=5432 user=veritime dbname=veritime");
die $conn->errorMessage unless PGRES_CONNECTION_OK eq $conn->status;

eval	{ 
	open (PASS_FILE,"/home/postgres/data/pass_file") || die "could not open pass_file\n$!"; 
	}; 
if($@) 
	{ 
	&Er_Handler($@,$title); 
	} 
$errors=0;
while(<PASS_FILE>)
	{ 
	$line=$_;
	if ($line =~ /query:/)
		{
		#query sting equals string following the regular expression match
		#print $line;
		#print "\n";

		$query_string=$';
		#strip leading spaces
		$query_string =~ s/^\s+// ;
		#strip trailing spaces
		$query_string =~ s/\s+$// ;
		print $query_string;
		print "\n";
		
		$error_message="";
		$result_status="";
		if ($errors eq 0)
			{
			eval	{	
	#			$result=$conn->exec("SELECT * from distributor_table ORDER BY distributor_id LIMIT 1;");
				$result=$conn->exec("$query_string");
				die $conn->errorMessage unless PGRES_TUPLES_OK eq $result->resultStatus;
				};
			if($@) 
				{ 
				&Er_Handler($@,$title);
				$error_message=$conn->errorMessage;
				if ($error_message eq "PGRES_CONNECTION_BAD")
					{
					$errors++;
					}
				$result_status=$result->resultStatus;
				if ($result_status eq "PGRES_FATAL_ERROR")
					{
					$errors++;
					}
				} 
			while(@row=$result->fetchrow) 
				{
				print @row, "\n";
				}
				#end while
			}
			#end if errors=0 begin else
		else
			{
			open(ERROR_FILE, ">>/home/postgres/data/error_file")|| die "could not open error_file\n$!";   # Open for appending
			print ERROR_FILE $query_string;
			close(ERROR_FILE);
			print "appending query string to error file";
			}
			#end if then else
		}
		#end if
	} 
	#end while
close(PASS_FILE);
system("rm -f /home/postgres/data/pass_file");
if ($errors > 0)
	{
	system ("cat /home/postgres/data/error_file > /home/postgres/data/pass_file");
	system("rm -f/home/postgres/data/error_file");
	system("touch /home/postgres/data/error_file");
	}
system("touch /home/postgres/data/pass_file");
