#! /usr/bin/perl -w

# rembo-maint
# lance rembo en tache de fond et analyse file.log pour arrter rembo si bloqu

require '/usr/lib/kwartz/control/rembo.pl';

my $rembover = &rembover();
if ( uc($rembover) eq 'REMBO5') {
	$bindir='/usr/rembo5';
} else {
	$bindir='/usr/rembo';
}
$rembo="$bindir/rembo";
$filelog="$bindir/logs/file.log";

# debug
#print("$rembo -d -c /etc/rembo.conf -v 4 $ARGV[0]\n");

#not creating zombie processes when the parent process fails to wait() on its child processes (i.e. child processes are automatically reaped)
$SIG{CHLD} = 'IGNORE';

my $pid=fork();
if (! defined $pid ) {
	die "Echec fork:$!";
} elsif ($pid) {
	# parent: on attend fin de traitement.
	# ouverture fichier log
	if ( open(RLOG, "<$filelog") ) {
		# fin du fichier
		seek RLOG, 0, 2;
	} else {
		print STDERR "Can't open $filelog: $!\n";
	}
	$| =1;	# unbuffered output....
	while (kill 0, $pid) {
#		print "$pid..\n";
		while (<RLOG>) {
			print "$_";
			# analyse des blocages
				# cas statshared rembo2 echec mais de termina pas (ex Block missing in sequence)
				if ( /FILE SERVICE DISABLED. CHECK LOGS/) {
					# on force l'arrt....
					kill 15, $pid;
				}
		}
	  sleep 1;
	}
	# on termine la lecture du log (sinon rien affich si repository vide...
	while (<RLOG>) {
			print "$_";
 	}
	close RLOG;

#	waitpid($pid, 0);

	exit;
}
# processus fils

# il ne faut aps fermer STDIN sinon exec ne se termine pas avec rembo5..
close(STDOUT); # close(STDERR);

exec("$rembo -d -c /etc/rembo.conf -v 4 $ARGV[0]");
die "can't exec $rembo -d -c /etc/rembo.conf -v 4 $ARGV[0]: $!";



