#!%PERL%

#    =====================================================================
#    |                   SPALEWARE LICENSE (Revision 0.1)                |
#    |-------------------------------------------------------------------|
#    | This file is part of a package called "GenEthic" and is           |
#    | licensed under SPALEWARE. You may freely modify and distribute    |
#    | this package or parts of it. But you MUST keep the SPALWARE       |
#    | license in it!                                                    |
#    |                                                                   |
#    =====================================================================

$maxidle = '180';

$file = $ARGV[0];

if ( -d "$file" ) { $ok; }
else {
	print STDERR "Syntax error: $0 <GenEthic_home_dir/tmp/keepalive>\n";
	exit 1;
}

$last = `%CAT% $file`;
chop $last;

$time = time;

if ( abs($last-$time) > $maxidle ) {
	print STDERR "over $maxidle sec inactivity, restarting the bot\n";
	$pid = `%PS% aux | %GREP% -v grep | %GREP% genethic | %AWK% '{print \$2}'`;
	chop $pid;
	if ( $pid =~ /^[0-9]+$/ ) {
		print STDERR "Seems there is a running process (PID: $pid), killing it\n";
		system("%KILL% -9 $pid");
	}
	system("%SU% genethic");
	print STDERR "Restarted GenEthic\n";
} else {
	$diff = $time - $last;
	if(!$diff) { $diff = '0'; }
	print "All running fine, last update $diff sec ago\n";
}
