#!/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/lstoldfiles/";
my $outfile="/home/websites/arabdemog_com/out.sql";
chdir($dirtoget);

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

####################################################################
open(OUT,">$outfile") || die("Unable to open directory");
#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.");
	 $first_line=0;
	foo1:foreach $line (<DATFILE>) 
		{ 	
		  if($first_line ==0)
			{
			  $first_line =1;	#skip first line
			  next foo1;
			}
		$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($cl,$sucl,$sdat,$med,$bcode,$pg,$nbpg,$siz,$phsz,$pgpos,$col,$pgdet,$sub,$ser,$fn,$art,$mt,$lan,$val,$spc,$grp1,$grp2,$grp3,$grp4)=split(/\"\,\"/,"$line");
		$cl =~ s/\"//;
		$grp4 =~ s/\"//;
		#print "Line=$line";

		#print "$cl,$sucl,$sdat,$med\n";
		#print "$grp1,$grp2,$grp3,$grp4\n\n";
		$dval=substr($val,0,6);
		#if(substr($fn,0,2)=='05' and $dval!='000000')
			{
			print OUT "update tbl_artothtree set val=\'$dval\' where fn=\'$fn\' \n";
			$sql= "update tbl_artothtree set val=\'$dval\' where fn=\'$fn\' \n";

			}

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

}
			close(OUTFILE);




