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

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

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

    print "samba.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 $smbclient = "/usr/bin/smbclient";
    my $status = "CRITICAL";
    my $output = "";

    open CMD, "$smbclient -N -L $host 2>&1 |";

    while ( <CMD> ) {
        chomp;
        s/[^\w]*$//g;
        $output .= "$_; ";
        if ( /IPC\$\s+IPC\s+IPC.Service/ ) {
	  $status = "OK";
        }
    }

    if ($status eq "OK") {
      print "$status: Samba connection ok\n";
    } else {
      print "$status: Samba connection to $host failed\n";
    }

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

    return $ERRORS{$status};
}

