#!/usr/bin/perl 
#Author:
#cleber.campanel
#30/03/2015
#version 1.0
use strict;
use Getopt::Long;
use POSIX;
use File::Basename;
use Filesys::SmbClient;
use Data::Dumper;
# Global variables
our $name = basename($0, ".pl");

our ($oHelp, $oVerbose, $oWarn, $oCrit, $perf, $username, $password, $workgroup, $share);
#--------------------------------------------------------------------------------------
sub main {

	getoption();
 	my $msg_share = $share;
 	$share =~ tr/\\/\//;

	my $smb = new Filesys::SmbClient(username  => $username,
    	                               password  => $password,
        	                           workgroup => $workgroup,
            	                       debug     => 0);

   	my $fd = $smb->opendir("smb:".$share) or quit("CRITICAL - $!",2);
  
	$smb->close($fd);

	quit("OK Available - ".$msg_share,0);

}

#--------------------------------------------------------------------------------------
sub quit {
	my $mgs = shift;
	my $code = shift;
	print $mgs,"\n";
	exit($code);
}
#--------------------------------------------------------------------------------------
sub getoption  {
	Getopt::Long::Configure('bundling');
	GetOptions(
		'h|help' => \$oHelp,
		'u|username=s' => \$username, 
		'p|password=s' => \$password, 
		'w|workgroup=s' => \$workgroup, 
		's|share=s' => \$share
        );

	if ( ($oHelp) or (!$username) or (!$password) or (!$share)){
		printUsage();
		exit(1);
	}
}

#--------------------------------------------------------------------------------------
sub printUsage {
       print <<EOB
Checks that the share is accessible
Usage: $name.pl [OPTION]...
-u, --username
-p, --password
-W, --workgroup
-s, --share
$name.pl -u \'workgroup\\username\' -p \'password\' -s '\\\\ip address ou hostname\\share'
$name.pl -u \'username\' -w  workgroup -p \'password\' -s '\\\\ip address ou hostname\\share'
EOB
}
#--------------------------------------------------------------------------------------
&main;

