# -*- perl -*-
#
# $Revision: 1.1 $
package cricket_compile;

BEGIN {
    eval "require '/s/cricket-1.0.5/etc/cricket-conf.pl'"
        unless $Common::global::gInstallRoot;
    $Common::global::gInstallRoot ||= $programdir;
}


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;

use lib "$Common::global::gInstallRoot/lib";

use Common::Version;
use Common::global;
use Common::Log;
use Common::Options;
use Common::Util;
use Common::Map;
use Common::HandleTarget;
use ConfigTree::Cache;

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

sub print_usage () {

    print "
Usage:  Check cricket for an up-to-date compile of the conf
Options:
    [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.1 $';
    $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 = "Config tree is up to date.";

    $Common::global::gCT ||= new ConfigTree::Cache;
    my $gCT = $Common::global::gCT;
    $gCT->Base($Common::global::gConfigRoot);
    $gCT->Warn(\&Warn);

    if (! $gCT->init()) {
        Die("Failed to open compiled config tree from " .
            "$Common::global::gConfigRoot/config.db: $!");
    }

    my($recomp, $why) = $gCT->needsRecompile();
    if ($recomp) {
        $output = "Config tree needs to be recompiled: $why";
        $status = "CRITICAL";
    }

    print "$status: $output\n";

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

    return $ERRORS{$status};
}
