#!/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#############
#$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/lstfilesint/";
chdir($dirtoget);

#pendir(IMD,$dirtoget) || die("Unable to open directory");
#@thelocfiles = readdir(IMD);
#closedir(IMD);
@thelocfiles = `ls *fav*` ;
####################################################################

#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)-3);
		print "Record=$record\n";
		my($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,$fn)=split(/\"\,\"/,"$record");
		
		if(length($fn)<10)
			{
			  next foo1;
			}
		 $syn =~ s/\'//g;
		 $syn =~ s/\"//g;
		 $swho =~ s/\'//g;
		 $swho =~ s/\"//g;
		 $swhen =~ s/\'//g;
		 $swhen =~ s/\"//g;
		 $swhere =~ s/\'//g;
		 $swhere =~ s/\"//g;
		 $swhat =~ s/\'//g;
		 $swhat =~ s/\"//g;
		 $swhy =~ s/\'//g;
		 $swhy =~ s/\"//g;
		 $ssrc =~ s/\'//g;
		 $ssrc =~ s/\"//g;
		 $speech =~ s/\'//g;
		 $speech =~ s/\"//g;
		 $sdatinc =~ s/\'//g;
		 $sdatinc =~ s/\"//g;
		 $fn =~ s/\'//g;
		 $fn =~ s/\"//g;
		$sql="insert into tbl_fav_syn_int (fn,$syn,fav1,fav2,fav3,fav4,fav5,fav6,fav7,fav8,fav9,fav10,fav11,fav12,fav13,fav14,fav15,fav16,fav17,fav18,fav19,sdatupl,swho,swhen,swhere,swhat,swhy,ssrc) values(\'$fn\',\'$syn\',\'$fav1\',\'$fav2\',\'$fav3\',\'$fav4\',\'$fav5\',\'$fav6\',\'$fav7\',\'$fav8\',\'$fav9\',\'$fav10\',\'$fav11\',\'$fav12\',\'$fav13\',\'$fav14\',\'$fav15\',\'$fav16\',\'$fav17\',\'$fav18\',\'$fav19\',now(),\'$swho\',\'$swhen\',\'$swhere\',\'$swhat\',\'$swhy\',\'$ssrc\')";
		print "\n$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);

}
	
exit(-1);

my $dirtoget="/home/websites/arabdemog_com/umar/tvart/";
chdir($dirtoget);

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

foo: foreach $file(@thelocfiles)
  {
   unless ( ($file eq ".") || ($file eq "..") )
   {
     my $file_mdtm=0;
     $fname=$dirtoget . $file;

     if(rindex($file,".asf") < 20)
     {
      next foo;
     } 
   
     $position=index($file, "-");
     $month=substr($file,$position+2+1,2);
     $yr=substr($file,$position+4+1,4);     
     $cntry=substr($file,0,2);
	
	 if($cntry eq '01')
     {  
		 $cntryname='K';
	 }
	 elsif($cntry eq '02')
     {  
		 $cntryname='S';
	 }
	 elsif($cntry eq '03')
     {  
		 $cntryname='Q';
	 }
	 elsif($cntry eq '04')
     {  
		 $cntryname='B';
	 }
	 elsif($cntry eq '05')
     {  
		 $cntryname='U';
	 }
	 elsif($cntry eq '06')
     {  
		 $cntryname='L';
	 }
	 elsif($cntry eq '07')
     {  
		 $cntryname='J'
	 }
	 elsif($cntry eq '08')
     {  
		 $cntryname='P';
	 }
	 elsif($cntry eq '09')
     {  
		 $cntryname='O';
	 }
	 elsif($cntry eq '10')
     {  
		 $cntryname='E';
	 }
	 elsif($cntry eq '12')
         {  
		 $cntryname='R';
		 }
	 elsif($cntry eq '18')
         {  
		 $cntryname='I';
		 }

	if (!(-e $yr && -d $yr))
		{
			mkdir $yr;
		}
	if (!(-e ($yr ."/". $cntryname) && -d ($yr."/".$cntryname)))
		{
			mkdir $yr."/".$cntryname;
		}
	if (!(-e ($yr."/".$cntryname."/".$month) && -d ($yr."/".$cntryname."/".$month)))
		{
			mkdir $yr."/".$cntryname."/".$month || print('unable to create directory');
		}

	#print $yr."/".$cntryname."/".$month . "\n";
	
#	if($month eq '11')
#	   {
#		move ($fname,$yr."/".$cntryname."/".$month);
#	   }
		#move ($fname,$yr."/".$cntryname."/".$month);
	#move ($fname,$yr."/".$cntryname."/".$month);
#	print $fname ."           " .$yr."/".$cntryname."/".$month;
#	print $dirtoget."/".$yr."/".$cntryname."/".$month. "/" .$file,$dirtoget ."\n";
#	move ($dirtoget."/".$yr."/".$cntryname."/".$month. "/" .$file,$dirtoget);
   
   }
 }

$syscmd="mv /home/websites/arabdemog_com/lstfilessyn/*fav* /home/websites/arabdemog_com/lstfilessynold/";
system($syscmd)||print("unable to move");


