#!/usr/bin/perl

###############################################
# prog Name: Uploadlstjpg.pl                  #
# Author   : MSUmar ,msumar@rediffmail.com    #
# Purpose  : Upload lstfiles,checks each lst  #
#  file in a folder P:\umar\newslogjpg and    #
#  checks for file existence in the server    #
#  and if found then checks for the size and  #
#  then compares the size of the local file   #
#  and server file and if that is found to be #
# same it deletes the local file              #
# Date     : February 26,2004                 #
###############################################


use Net::FTP;
use File::Copy;
use DBI;

#####Open the Directory and put the files  in the array#############
#move("/home/websites/arabdemog_com/lstfilestvart/*fav*","/home/websites/arabdemog_com/lstfilessyn/");
#$syscmd="mv /home/websites/arabdemog_com/lstfilessyn/J*fav* /home/websites/arabdemog_com/lstfilessynold/";
#system($syscmd)||print("unable to move");
my $dbh=DBI->connect('dbi:mysql:arabdemog_com:arabiandemographics.com','arabdemog_com','p2a9r7cd') || die('cannot conect to database');


#my $dirtoget="/usr/local/apache/htdocs/umar/uploads/";
my $dirtoget="/home/websites/arabdemog_com/lstfilessynpress/";
chdir($dirtoget);

opendir(IMD,$dirtoget) || die("Unable to open directory");
@thelocfiles = readdir(IMD);
closedir(IMD);

####################################################################

#my $ftp = Net::FTP->new('www.arabiandemographics.com');
#$ftp->login('arabdemog_com','p2a9r7cd');
#$ftp->cwd('umar/uploads');

#$ftp->binary();

foo: foreach $DATFILE(@thelocfiles)
  {
	 open(DATFILE) or die("Could not open log file.");
	foo1:foreach $line (<DATFILE>) 
		{ 		
		$record=substr($line,1,length($line)-2);
		#print "Record=$record\n";
	
		#fn,syn,fav1,fav2,fav3,fav4,fav5,fav6,fav7,fav8,fav9,fav10,fav11,fav12,fav13,fav14,fav15,fav16,fav17,fav18,fav19,Source,Who,Where,When,What,Why
		#my($fn,$syn,$fav1,$fav2,$fav3,$fav4,$fav5,$fav6,$fav7,$fav8,$fav9,$fav10,$fav11,$fav12,$fav13,$fav14,$fav15,$fav16,$fav17,$fav18,$fav19,$ssrc,$swho,$swhere,$swhen,$swhat,$swhy)=split(/\"\,\"/,"$line");
		
		my($fn,$syn,$fav1,$fav2,$fav3,$fav4,$fav5,$fav6,$fav7,$fav8,$swhat,$swho,$pro,$anti_pro,$event,$rift,$complaint,$swhen,$swhere,$swhy,$ssrc)=split(/\"\,\"/,"$line");

		print "Src=$ssrc\n";
		print "Swho=$swho\n";
		print "Swhere=$swhere\n";
		print "Swhen=$swhen\n";
		print "Swhat=$swhat\n";
		print "Swhy=$swhy\n";

		if(length($fn)<5)
			{
			  next foo1;
			}
			if(length($syn))
			{
				$syn =~ s/\'//g;
				$syn =~ s/\"//g;
				#$syn =~ s/\n//g;
			}
			if(length($swho))
			{
			 $swho =~ s/\'//g;
			 $swho =~ s/\"//g;
			 #$swho =~ s/\n//g;
			}
			if(length($swhen))
			{
			 $swhen =~ s/\'//g;
			 $swhen =~ s/\"//g;
			 #$swhen =~ s/n//g;
			}
			if(length($swhere))
			{
			 $swhere =~ s/\'//g;
			 $swhere =~ s/\"//g;
			 #$swhere =~ s/\n//g;
			}
			if(length($swhat))
			{
			$swhat =~ s/\'//g;
			$swhat =~ s/\"//g;
			#$swhat =~ s/\n//g;
			}
			if(length($swhy))
			{
				$swhy =~ s/\'//g;
				$swhy =~ s/\"//g;
				#$swhy =~ s/\n//g;
			}
			if(length($ssrc))
			{
			 $ssrc =~ s/\'//g;
			 $ssrc =~ s/\"//g;
			 #$ssrc =~ s/\n//g;
			}
		 $fn =~ s/\'//g;
		 $fn =~ s/\"//g;

		print "Src=$ssrc\n";
		print "Swho=$swho\n";
		print "Swhere=$swhere\n";
		print "Swhen=$swhen\n";
		print "Swhat=$swhat\n";
		print "Swhy=$swhy\n";

		#my $sql="insert into tbl_fav_syn_bts (fn,syn,fav1,fav2,fav3,fav4,fav5,fav6,fav7,fav8,fav9,fav10,fav11,fav12,fav13,fav14,fav15,fav16,fav17,fav18,fav19,swho,swhen,swhere,swhat,swhy,ssrc,sdatupl)values(\'$fn\',\'$syn\',\'$fav1\',\'$fav2\',\'$fav3\',\'$fav4\',\'$fav5\',\'$fav6\',\'$fav7\',\'$fav8\',\'$fav9\',\'$fav10\',\'$fav11\',\'$fav12\',\'$fav13\',\'$fav14\',\'$fav15\',\'$fav16\',\'$fav17\',\'$fav18\',\'$fav19\',\'$swho\',\'$swhen\',\'$swhere\',\'$swhat\',\'$swhy\',\'$ssrc\',now\(\))";
		my $sql="insert into tbl_fav_syn_bts(fn,syn,fav1,fav2,fav3,fav4,fav5,fav6,fav7,fav8,swho,swhen,swhere,swhat,swhy,ssrc,pro,anti_pro,event,rift,complaint,sdatupl) values(\'$fn\',\'$syn\',\'$fav1\',\'$fav2\',\'$fav3\',\'$fav4\',\'$fav5\',\'$fav6\',\'$fav7\',\'$fav8\',\'$swho\',\'$swhen\',\'$swhere\',\'$swhat\',\'$swhy\',\'$ssrc\',\'$pro\',\'$anti_pro\',\'$event\',\'$rift\',\'$complaint\',now())";

		print "\nSQL=$sql\n";
		$sthE=$dbh->prepare($sql)|| print("unable to prepare $sql  $dbh->errstr $!");
		$rv=$sthE->execute || print("Record Already Entered   $sql $sthE->errstr");
		}
		close(DATFILE);

}
	


$syscmd="mv /home/websites/arabdemog_com/lstfilessynpress/* /home/websites/arabdemog_com/lstfilessynpressold/";
system($syscmd)||print("unable to move");


