package Shellex::Shellex;
use strict;
use warnings;
use Log::Log4perl qw(:easy);
use Exporter qw(import);
our @EXPORT_OK = qw(shellex findBin);

sub shellex {

my $cmd = shift;
my $logger = shift;
if ( defined $logger && $logger ne '' ) {
$logger->info("Running: $cmd 2>&1");
}

my $output = `$cmd 2>&1`;
chomp $output;
my $rc = $?;
if ( defined $logger && $logger ne '' ) {
$logger->info("Returned: $rc");
}

if ( $rc != 0 ) {
if ( defined $logger && $logger ne '' ) {
$logger->error("$cmd returned non-zero: $rc");
}

print "$cmd returned non-zero: $rc\n";
exit 1;
}

return $output;

}

sub findBin {

my $cmd = shift;
my $logger = shift;
my $binPath = shellex("which $cmd",$logger);
my $rc = $?;
chomp $binPath;
if ( $rc != 0 ) {
if ( defined $logger && $logger ne '' ) {
$logger->error("Couldn't find a path for $cmd, exiting...");
}
exit 1;
} else {
return $binPath;
}

}
1;