#!/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 File::Temp qw( tempdir );
use File::Basename;
use lib qw(/usr/lib/libDrakX);
use common;
use hclcollector;

require_root_capability();

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://hcl.mandriva.com";

my $tempdir=tempdir(CLEANUP => 1);
my $base=basename($tempdir);
#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("hcl.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
	hclcollector::init($tempdir);
	if (-e "$tempdir/$base.tar.bz2" || -e "$tempdir/$base.tar.gz" || -e "$tempdir/$base.zip") {		
		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 "$tempdir/$base.tar.bz2") {
			$file = "$tempdir/$base.tar.bz2";
		} elsif (-e "$tempdir/$base.tar.gz") {
			$file = "$tempdir/$base.tar.gz";	
		} else {
			$file = "$tempdir/$base.zip";
		}
				
		# execute "file" commande in order to find out the real
		# format of report file 
		open(my $EXT, "file $file|");
		foreach (<$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/hcldatacollector.cgi", Content_Type => 'form-data',
			Content      => [
					submit => 1,
					file => [ $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 $file");		
			} else { # something wrong...
				print "File uploading failed --exit error.\n";
				#print LOG $date.":".$response->as_string."\n\n";
				system("rm -rf $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 $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);
