#!perl -w
  
   use SOAP::Lite;
   use Getopt::Long;
   
 my ($mode_use,$time,$user,$name,$host,$event,$descrizione,$tipo,$output,$file,$save_db,$save_db_mode,$save_db_n,$help);
GetOptions("mode=s" => \$mode_use, 
	"date=s" => \$time,
	"user=s"=> \$user,
	"name=s"=> \$name,
	"host=s"=> \$host,
	"event=s"=> \$event,
	"det=s"=> \$descrizione,
	"type=s"=> \$tipo,
	"output=s" => \$output,
	"out-file=s"=> \$file,
	"save-db=s"=> \$save_db,
	"save-db-mode=s"=> \$save_db_mode,
	"save-db-n=s"=> \$save_db_n,
        "help" => \$help);

if (!defined($time)) {
        $time_tmp = `date +\%s`;
	($time)=($time_tmp=~ /(\d+)/);
}

if (!defined($user)) {

	$user=`whoami`;
}

if (!defined($host)) {

$host_tmp=`hostname`;
($host)=($host_tmp=~ /(.+)\n/);
}

if (!defined($save_db))
{
$save_db="off";
}

if (!defined($mode_use))
{
$mode_use="new";
}

if (!defined($save_db_mode))
{
$save_db_mode="line";
}

if (!defined($output))
{
$output="off";
}

if (!defined($tipo))
{
$tipo="blob";
}

#$job_id=$ENV{'EDG_WL_JOBID'};

$job_id=$ENV{'EDG_WL_JOBID'};

if(defined($help)) {
	        print "usage: send.pl  --name=<classID> --event=<event> --det=<content of event> --type=<tipo> \n";
		        exit 0;
		}
#print "MODE: $mode_use";

my $ext_event=$event; 
my $ext_descr=$descrizione; 

my $ext_name=$name;

if($mode_use=~ /init/){

my $nome_var1="CE";
my $var_cont1=$ENV{'EDG_WL_LOG_DESTINATION'};

#print "nome: $nome_var1  \n valore: $var_cont1\n \n";

   SOAP::Lite
	        -> uri('http://webcms.ba.infn.it:50000/Demo')
	        -> proxy('http://webcms.ba.infn.it:50000')
        -> char($time,$name,$host,$job_id,$nome_var1,$var_cont1)
        -> result;


$nome_var1="PWD";
$var_cont1=$ENV{'PWD'};

#print "nome: $nome_var1  \n valore: $var_cont1\n \n";
    SOAP::Lite                                             
        -> uri('http://webcms.ba.infn.it:50000/Demo')                                             
        -> proxy('http://webcms.ba.infn.it:50000')
        -> char($time,$name,$host,$job_id,$nome_var1,$var_cont1)
        -> result;


$nome_var1="USER";
$var_cont1=$ENV{'USER'};

#print "nome: $nome_var1  \n valore: $var_cont1\n \n";
    SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
	-> char($time,$name,$host,$job_id,$nome_var1,$var_cont1)
        -> result;

$nome_var1="HOME";
$var_cont1=$ENV{'HOME'};

#print "nome: $nome_var1  \n valore: $var_cont1\n \n";
    SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
	-> char($time,$name,$host,$job_id,$nome_var1,$var_cont1)
        -> result;

$nome_var1="status";
$var_cont1="Running";

#print "nome: $nome_var1  \n valore: $var_cont1\n \n";
 SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
	-> char($time,$name,$host,$job_id,$nome_var1,$var_cont1)
        -> result;


}

if($mode_use=~ /new/){

        $time_tmp = `date +\%s`;
        ($time)=($time_tmp=~ /(\d+)/);
	

 print  SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
	#-> char($time,$name,10,$host,20,$job_id,$nome_var,20,$var_cont,200,$host,20)
	-> char($time,$name,$host,$job_id,$ext_event,$ext_descr)
        -> result;

}

if($mode_use=~ /end/){
        $time_tmp = `date +\%s`;
        ($time)=($time_tmp=~ /(\d+)/);

	$nome_var2="status";
	$var_cont2="Done";

	  print SOAP::Lite
         -> uri('http://webcms.ba.infn.it:50000/Demo')
         -> proxy('http://webcms.ba.infn.it:50000')
         -> char($time,$name,$host,$job_id,$nome_var2,$var_cont2)
         -> result;

if($output=~ /save/)
{
        $time_tmp = `date +\%s`;
        ($time)=($time_tmp=~ /(\d+)/);

system("mkdir /tmp/$time/");
system("cp $file /tmp/$time/");

$nome_var="file saved";
$var_cont="/tmp/$time/$file";

    SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
        -> char($time,$name,$host,$job_id,$nome_var,$var_cont)
        -> result;

}
if($save_db=~ /on/)
{
if($save_db_mode=~ /byte/)
{
$var_cont=`tail -c $save_db_n $file`;
$nome_var="imported file";

    SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
	-> char($time,$name,$host,$job_id,$nome_var,$var_cont)
        -> result;


}
if($save_db_mode=~ /line/)
{

$var_cont=`tail -n $save_db_n $file`;
$nome_var="imported file";

    SOAP::Lite
        -> uri('http://webcms.ba.infn.it:50000/Demo')
        -> proxy('http://webcms.ba.infn.it:50000')
        -> char($time,$name,$host,$job_id,$nome_var,$var_cont)
        -> result;

}


}

}
