#!/usr/bin/perl
#
# $FreeBSD: head/databases/pgpool-II-33/files/pgpool_remote_start 340872 2014-01-24 00:14:07Z mat $

use strict;
use warnings;

my ($host, $dir) = @ARGV;

my $logger = "logger -t pgpool -p local0.info";
my $pgctl = "/usr/local/bin/pg_ctl -D $dir";
my ($fh, $log);

if (not open($fh, "ssh pgsql\@$host $pgctl -t 5 -w -s -m fast start 2>&1 |")) {
  die "cannot open ssh: $!";
}
if (not open($log, "| $logger")) {
  die "cannot open logger: $!";
}
while (<$fh>) {
  print $log $_;
}
close($fh) or die "close(ssh) failed: $!";
close($log) or die "close(logger): $!";

system("ssh pgsql\@$host $pgctl status 2>&1") == 0 or die "pg_ctl status failed";
