# -*- perl -*-
#
# $Revision: 1.2 $
package cricket;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check cricket functionality on local host.
Options:
    [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.2 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "csl_cricket.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $status = "OK";
    my $output;

    my $tmpdir = "/tmp";
    my $filename = "strivia-subtree-";

    opendir(DIR, $tmpdir) || die "Must have valid tmpdir!\nCan't opendir $tmpdir: $!";
    my @TMPFILES = grep { /^$filename/ } readdir(DIR); # Get all relevant files
    closedir DIR;

# For each file in the cricket tmpdir...
    foreach my $file (sort @TMPFILES) {

# If the log file is older than 30 min, warn, Warn, WARN
        my $modification = -M "$tmpdir/$file";
        $modification = ( int ($modification * 24 * 60 *10) / 10 );
        if ($modification > 30) {
            $status = "WARNING" if ($status eq "OK");
            $output = "rm $tmpdir/$file ($modification min) # $output ";
        }
        my ($printfile) = $file =~ /$filename(.*)$/;
        $output .= "$printfile: $modification min # ";
    }

    ($output) || ($output = "No $tmpdir/$filename* files");

    print "$status: $output\n";

    print "Exit status: $status\n" if $options{"verbose"};

    return $ERRORS{$status};
}

