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

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 hfaxd service.
Options:
    -H <hostname> [--verbose]
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

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

    print "hfaxd.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 $host = $options{"opt_H"};

    if ((!$host) || ($host eq "")) {
        print "\nHost name not specified.\n";
        &print_usage();
        return $ERRORS{'OK'};
    }

    my $faxstat = "/s/hylafax/bin/faxstat";
    my $scheduler = "scheduler not running";
    my $status = "CRITICAL";
    my $modem = "modem not running";
    my $output = "";

    open CMD, "$faxstat -h $host 2>&1 |";

    while ( <CMD> ) {
        chomp;
        s/[^\w]*$//g;
        $output .= "$_; ";
        if ( /scheduler.*$host.*: \w*/ ) {
            ($scheduler) = /scheduler.*$host.*: ([\w ]*)/;
        } elsif ( /Modem.*: \w*/ ) {
            ($modem) = /Modem.*: ([\w ]*)/;
        }
    }

    print "scheduler: $scheduler - modem: $modem\n" if $options{"verbose"};

    if (($scheduler =~ /Running/) &&
            (($modem =~ /Running/) || ($modem =~ /Sending/))) {
        $status = "OK";
    } elsif ($scheduler =~ /Running/) {
        $status = "WARNING";
    }

    print "$status: $output\n";

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

    return $ERRORS{$status};
}

