#!/usr/bin/perl

# restore_command helper: wait until the server log shows a shutdown request.
# This script accepts two arguments:
# - A log file to monitor, inherited from the server spawned.
# - A timeout value, defined by $PostgreSQL::Test::Utils::timeout_default.

use strict;
use warnings FATAL => 'all';
use Time::HiRes qw(usleep);

my ($logfile, $timeout) = @ARGV;

print STDERR "restore_command waiting for shutdown\n";
for (my $i = 0; $i < $timeout * 10; $i++)
{
	open my $fh, '<', $logfile or die "could not open $logfile: $!";
	exit 1 if grep { /received \w+ shutdown request/ } <$fh>;
	close $fh;
	usleep(100_000);
}
die "timed out waiting for shutdown request\n";
