#!/usr/bin/perl

#--------------------------------------------------------------------------------------
# Project	: hcl
# File		: hclupload
# Author 	: Keerthan Muthurasa <kmuthurasa at mandriva dom com>
# Created On	: Mon Aug 27 11:15:03 2007
# Purpose	: Collect data about hardware ,gather them in a compress directory
# 		  in order to send it to a remote server
#-------------------------------------------------------------------------------------

use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use File::Basename;

#
# WARNING : Don't modify the output messages or else the hclGUI will not work 
# correctly.
#



my $server="http://192.168.100.95";

#Testing arguments count
my $argcount=scalar(@ARGV);

#One and only one argument ( email ) is allowed.
if($argcount != 1)
{	
	print "<Client: Usage : hclupload [email]\n";
	exit(-1);
}

# email format is not checked because already done in the 
# script calling hclupload.pl
my $email=$ARGV[0];

#Getting information about the current time
#my($sec,$min,$hour,$mday,$mon,$year,$wday,$day,$isdst) = localtime(time);

#Date setted to the american date format
#my $date = "[ ".($mon+1)."/".$mday."/".(1900+$year).":".$hour."-".$min."-".$sec." ] ";

#Opening log file in order to write output.
#open(LOG,">>/var/log/hcllog");


#Checking if the network is working
sub network_enabled
{	
	#Output in the screen
	print "Checking network...\n";
	
	#Output in the log file
	#print LOG "$date:< Client : Checking network...\n"; 
	
	#Trying to reach server
	my $network_Up = gethostbyname("qa.mandriva.com") ? 1 : 0;
        $network_Up;
}

#if network is working
if( network_enabled() )
{
	print "Network enabled.\n";
	#print LOG "$date:< Client : Network enabled.\n";
	print "Collecting hardware information...\n";
	#print LOG "$date:< Client : Collecting hardware information...\n";

	

# Collecting hardware information using the report file
	my $regene=system("/usr/sbin/hclcollector");
	if($regene ne "-1")
	{		
		print "Information collecting successed.\n";
		#print LOG "$date:< Client : Information collecting successed.\n";

		my $extension;
		my $file;

		print "Checking report file format...\n";
		#print LOG "$date:< Client : Checking report file format...\n";
		
		#Checking the file extension
		if(-e "/tmp/report.tar.bz2" )
		{
			$file = "report.tar.bz2";
		}
		elsif(-e "/tmp/report.tar.gz" )
		{
			$file = "report.tar.gz";	
		}
		else
		{
			$file = "report.zip";
		}
				
		# execute "file" commande in order to find out the real
		# format of report file 
		open(EXT,"file /tmp/$file|");
		while(<EXT>)
		{
			$extension=$_;
		}
		close(EXT);

		#Only bzip2 , gzip and Zip format are allowed
		if($extension =~ m/bzip2 compressed data/ || $extension =~ m/gzip compressed data/ || $extension =~ m/Zip archive data/)
		{
			print "Valid file format($file).\n";
			#print LOG "$date:< Client : Valid file format($file).\n";
			print "Sending information to server...\n";
			#print LOG "$date:< Client : Sending information to server...\n";
			
			# Sending information to the client using a UserAgent
			my $ua  = LWP::UserAgent->new();
			my $req = POST "$server/cgi-bin/hcldatacollector.cgi", Content_Type => 'form-data',
			Content      => [
					submit => 1,
					file => [ "/tmp/$file","$file"],
					email => $email
					];
			
	     		my $response = $ua->request($req);
			
			# succeful answer from server
			if ($response->is_success()) 
			{
 				if($response->content !~ m/exit error/)
				{
				
				
					#($sec,$min,$hour,$mday,$mon,$year,$wday,$day,$isdst) = localtime(time);
					#$date = "[ ".($mon+1)."/".$mday."/".(1900+$year).":".$hour."-".$min."-".$sec." ] ";
					print  "File uploading successed.\n";
					#print LOG $date.":".$response->content;
				}
				else
				{
					print "File uploading failed --exit error.\n";
					#	print LOG $date.":".$response->content;
				}
				system("rm -rf /tmp/$file");		
			}
			else # something wrong...
			{
				print "File uploading failed --exit error.\n";
				#print LOG $date.":".$response->as_string."\n\n";
				system("rm -rf /tmp/$file");
				exit(-1);		
			}
		}
		else # File format is not good...
		{
			print "Bad report file format --exit error.\n";
			#print LOG "$date: < Client : Bad report file format (exit error).\n\n";
			system("rm -rf /tmp/$file");
			exit(-1);
		}		
	}	
	else #Probleme when collecting hardware data
	{
		print "Fail to collect information --exit error.\n";
		#print LOG "$date: < Client : Fail to collect information (exit error) \n\n";
		exit(-1);
	}
}
else # Network problem or server unreacheble
{

	print "Server unreacheble --exit error.\n";
	#print LOG "$date: < Client : Server unreacheble (exit error) \n\n";
	exit(-1);
}
#print LOG "\n";
#close(LOG);
