#!/usr/bin/perl -w
###############################################################################
##Copyleft Pascal DANEK 2005
##Copyleft Goneri Le Bouder 2006
##Copyleft FusionInventory Project 2010-2011
##Web : http://www.FusionInventory.org
##
##This code is open source and may be copied and modified as long as the source
##code is always made freely available.
##Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
################################################################################
use Fcntl qw/:flock/;
use LWP::UserAgent;
use XML::Simple;
use Compress::Zlib;
use Getopt::Long;
use strict;
my $help;
my $directory;
my $file;
my $url;
my $useragent;
my $remove;
my $verbose;
my $stdin;

sub loadfile {
    $file = shift;

    unless ( -r $file ) {
        print STDERR "Can't read $file\n";
        return;
    }
    print "Loading $file..." if $verbose;

    unless ( open( FILE, "$file" ) && flock( FILE, LOCK_EX | LOCK_NB ) ) {
        print STDERR "Failed to access $file : $!";
        return;
    }

    local $/;
    my $content = <FILE>;
    close FILE or die "Can't close file $file: $!";
    
    sendContent($content);

}

sub loaddirectory {
    my $directory = shift;

    unless ( -r $directory ) {
        print STDERR "Can't read $directory: $!\n";
        return;
    }

    opendir( DIR, $directory ) || die "can't opendir $directory: $!";
    foreach ( readdir(DIR) ) {
        loadfile("$directory/$_") if (/\.ocs$/);
	}
	closedir DIR;

}

sub loadstdin {
    my $content;
    undef $/;
    $content = <STDIN>;
    sendContent($content);
}

sub sendContent {
    my $content = shift;

    my $ua = LWP::UserAgent->new;
    $ua->agent($useragent);
    my $request = HTTP::Request->new( POST => $url );
    $request->header(
        'Pragma' => 'no-cache',
        'Content-type', 'Application/x-compress'
    );
    if (uncompress($content)) {
        $content = uncompress($content);
    }
    $request->content(compress($content));
    my $res = $ua->request($request);

    if($res->is_success){
      print "OK\n" if $verbose;
      print STDERR "Can't remove $file: $!\n"
	      if ($remove && (!unlink $file));
    }else{
    	if($verbose){
	 		 print "ERROR: ";
	 		 print $res->status_line(), "\n";
			}
    }
}

sub usage {

    print STDERR <<EOF;
DESCRIPTION:
  A command line tools to import .ocs file in an OCS Inventry server.

  USAGE:
  -h --help	: this menu
  -d --directory: load every .ocs files from a directory
  -f --file	: load a speficic file
  -u --url	: ocsinventory backend URL
  -r --remove	: remove succesfuly injected files
  -v --verbose	: verbose mode
  --stdin       : read data from STDIN

You must specify a --file or a --directory or STDIN.

Example :
 \$export https_proxy=http://www-proxy:8080
 \$fusioninventory-injector -v -f /tmp/toto-2010-09-10-11-42-22.ocs --url https://login:pw\@yourserver/ocsinventory

This tool is part of the FusionInventory distribution.
EOF
    exit 1;
}

GetOptions(
    'h|help'		=> \$help,
    'd|directory=s'	=> \$directory,
    'f|file=s'		=> \$file,
    'u|url=s'		=> \$url,
    'useragent=s'	=> \$useragent,
    'r|remove'		=> \$remove,
    'v|verbose'		=> \$verbose,
    'stdin'		=> \$stdin,
);

# Default values
$useragent	= 'FusionInventory-Injector' unless $useragent;
###

$|=1;
usage() if $help;
if ($file && -f $file) {
    loadfile($file);
} elsif ($stdin) {
    loadstdin();
} elsif ($directory) {
   die("Directory does not exist. Abort.") unless -d $directory;
   loaddirectory($directory);
} else {
    usage();
}

__END__

=head1 NAME

fusioninventory-injector - A tool to push inventory in an OCS Inventory or compatible server.

=head1 DESCRIPTION

This tool can be used to test your server, do benchmark or push inventory from off-line machine.

=head1 SYNOPSIS

Please see: 

B<fusioninventory-injector> S<--help>

