#!/usr/bin/perl

# Defines min/max age to report
my $maxInstallAge = '30';
my $minInstallAge = '7';

# Where do we send updates to?
my $publishURL = 'http://www.seekbrain.com/legacy/publish.cgi';

# Should this machine be tracked? Uses hostname then IP
my $trackMachine = 1;

# Verbose, you want to disable this if you're using legacycheck through a cron
my $verbose = 1;

# We like strict
use strict;

# We need Date::Calc
use Date::Calc qw(:all);

# Get rpm list
my @legacyRPMS = `rpm -qa | grep legacy`;

# Get current time
my $currentTime = time;

my %updateHash;

my ($hostname);
if($trackMachine > 0) {
$hostname = `hostname`;
} else {
$hostname = 'private';
}

chomp($hostname);

foreach my $line (@legacyRPMS) {
	chomp($line);
	my @rpmOutput = `rpm -qi $line`;
	my($rpmName,$rpmVersion,$rpmRelease,$rpmInstalled);
	foreach my $rpmLine (@rpmOutput) {
		if($rpmLine =~ /^Name/) {
			$rpmLine =~ s/^Name\s*\: (\S+)\s+.*/$1/;
			chomp($rpmLine);
			$rpmName = $rpmLine;
#			print $rpmLine . " ";
		} elsif($rpmLine =~ /^Version/) {
			chomp($rpmLine);
			$rpmLine =~ s/^Version\s*\: (\S+)\s+.*/$1/;
			$rpmVersion = $rpmLine;
#			print $rpmLine . " ";
		} elsif($rpmLine =~ /^Release/) {
			chomp($rpmLine);
			$rpmLine =~ s/^Release\s*\: (\S+)\s+.*/$1/;
			$rpmRelease = $rpmLine;
#			print $rpmLine . " ";
		} elsif($rpmLine =~ /^Install Date/) {
			chomp($rpmLine);
			$rpmLine =~ s/^Install Date\s*\: (.*)\s\s+.*/$1/;
			$rpmLine =~ s/(\s\s+)//gi;
			chomp($rpmLine);
			$rpmInstalled = $rpmLine;
#			print $rpmLine . " ";
		}
	}

	# We have an installed date by this time
	# now we cut out the first 4 characters (we don't need day of the week)
	$rpmInstalled =~ s/\S\S\S\s(.*)/$1/;

	# Now we convert the month to a digit
	$rpmInstalled =~ s/Jan/01/;
	$rpmInstalled =~ s/Feb/02/;
	$rpmInstalled =~ s/Mar/03/;
	$rpmInstalled =~ s/Apr/04/;
	$rpmInstalled =~ s/May/05/;
	$rpmInstalled =~ s/Jun/06/;
	$rpmInstalled =~ s/Jul/07/;
	$rpmInstalled =~ s/Aug/08/;
	$rpmInstalled =~ s/Sep/09/;
	$rpmInstalled =~ s/Oct/10/;
	$rpmInstalled =~ s/Nov/11/;
	$rpmInstalled =~ s/Dec/12/;

	my($trash,$myDay,$myMonth,$myYear,$myHour,$myMinute,$mySecond) = split(/^(\d\d) (\d\d) (\d\d\d\d) (\d\d)\:(\d\d)\:(\d\d).*/, $rpmInstalled, 7); 
	#print "Day: $myDay Month: $myMonth Year: $myYear Hour: $myHour Minute: $myMinute\n";
	$rpmInstalled = Mktime($myYear, $myMonth, $myDay, $myHour, $myMinute, $mySecond);
	if($verbose > 0) {
		print "We have $rpmName-$rpmVersion-$rpmRelease installed " . localtime($rpmInstalled) . "\n";
	}

	my $installDifference = $currentTime - $rpmInstalled;
	if($verbose > 0) {
		print "\tInstall difference (seconds) is: $installDifference\n";
	}	
	if(($installDifference > ($minInstallAge*86400)) && ($installDifference < ($maxInstallAge*86400))) {
		if($verbose > 0) {
			print "\t$rpmName is going to be reported\n";
		}
		$updateHash{$rpmName}{'installDifference'} = $installDifference;
		$updateHash{$rpmName}{'rpmVersion'} = $rpmVersion;
		$updateHash{$rpmName}{'rpmRelease'} = $rpmRelease;
		print "/usr/bin/curl \"$publishURL?action=post&hostname=$hostname&packagename=$rpmName&packageversion=$rpmVersion&packagerelease=$rpmRelease\" $publishURL\n";
		system("/usr/bin/curl \"$publishURL?action=post&hostname=$hostname&packagename=$rpmName&packageversion=$rpmVersion&packagerelease=$rpmRelease\"");
	}
}


