#!/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;

#####Open the Directory and put the files  in the array#############
$syscmd="/var/www/vhosts/arabdemog.com/httpdocs/newslog/u2lower.sh";

	if (!defined($kidpid = fork())) 
		{				 # fork returned undef, so failed
			die "cannot fork: $!";
		} elsif ($kidpid == 0) 
		{         # fork returned 0, so this branch is the child
		exec($syscmd);         # if the exec fails, fall through to the next statement
		die "can't exec date: $!";
		} else 
		{     # fork returned neither 0 nor undef,        # so this branch is the parent
		 waitpid($kidpid, 0);
		} 

#my $dirtoget="/usr/local/apache/htdocs/umar/uploads/";
my $dirtoget="/var/www/vhosts/arabdemog.com/httpdocs/newslog/images/";
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 $file(@thelocfiles)
  {
   unless ( ($file eq ".") || ($file eq "..") )
   {
     my $file_mdtm=0;
     $fname=$dirtoget . $file;

     if(rindex($file,".jpg") < 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';
	 }

	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);
   
   }
 }




