#!/usr/bin/perl -w

#Throughout the day, we will add the rotated log files into a tar file
#At the end of the day, we will zip up the tar file.

=niranjan
#Get today's date. If there is any gz file with this date, then unzip it!!!!
#If there is any tar file but not this date's, then zip it!!!!
$datestr = `date +%Y-%m-%d`;
chomp ($datestr);
$zipfile = $datestr . "\." . "tar\.gz";
$tarfile = $datestr . "\." . "tar";
chdir ("/var/log/backup");

@gzfiles = `ls *.gz`;
$i = 0;
while ($gzfiles[$i] ne "")
{
	chomp ($gzfiles[$i]);
	if ($gzfiles[$i] eq $zipfile)
	{
		system ("gunzip $gzfiles[$i]");
		last;
	}
	$i++;
}

@tarfiles = `ls *.tar`;
$i = 0;
while ($tarfiles[$i] ne "")
{
	chomp ($tarfiles[$i]);
	if ($tarfiles[$i] ne $tarfile)
	{
		system ("gzip $tarfiles[$i]");
	}
	$i++;
}
=cut

my ($kernelrotate, $force_kernelrotate, $logrotate, $force_logrotate);
                                                                                                                             
$kernelrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate /etc/kernelrotate.conf" | tr -s " " | cut -d " " -f2`;
chomp ($kernelrotate);
                                                                                                                             
$force_kernelrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate -f /etc/kernelrotate.conf" | tr -s " " | cut -d " " -f2`;
chomp ($force_kernelrotate);
                                                                                                                             
$logrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate /etc/logrotate.conf" | tr -s " " | cut -d " " -f2`;
chomp ($logrotate);
                                                                                                                             
$force_logrotate = `ps -eo "%y %p %a" | grep -v grep | grep "/usr/sbin/logrotate -f /etc/logrotate.conf" | tr -s " " | cut -d " " -f2`;
chomp ($force_logrotate);
                                                                                                                             
$ziplog = `ps -eo "%y %p %a" | grep -v grep | grep "/etc/multiconf/scripts/ziplogfiles" | tr -s " " | cut -d " " -f2`;
chomp ($ziplog);
                                                                                                                             
if ((($kernelrotate eq "") || ($kernelrotate eq " ")) && (($force_kernelrotate eq "") || ($force_kernelrotate eq " ")) && (($logrotate eq "") || ($logrotate eq " ")) && (($force_logrotate eq "") || ($force_logrotate eq " ")) && (($ziplog eq "") || ($ziplog eq " ")))
{
        system ("/usr/sbin/logrotate /etc/logrotate.conf");
}
