#!/usr/bin/perl


print "Now moving the files!!!!!!!!!!!!!!!!!!!!\n";
#$FGDCDir="/prodmtdata001/geo/gpms/output/fgdc";
$FGDCDir="/prodmtdata003/geo/shpgen/tigerline/fe_2013";
$TargetDir="/home/m/mccre004/GPMSscripts/FileMoverDir";

print "\$FGDCDir = $FGDCDir\n";
print "after the determing the FGDCDir variable\n";
opendir DHFGDC, $FGDCDir or die "cannot open $FGDCDir: $!";
print " the directory is opened!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

$beginCounter=0;
#$beginCounter=5000;
#$beginCounter=10000;
#$beginCounter=15000;


$endCounter=$beginCounter+15000;
print "\$endCounter = $endCounter\n";
#$TargetFips="72109";
$found="no";
#$counter=$beginCounter;

print "pre loop\n";
foreach $dir (readdir DHFGDC){

if ($counter >=$beginCounter and $counter<=$endCounter and $dir ne  "." and $dir ne ".."){

print "----------------------------------------------\n";

$FGDCoutputFile = $FGDCDir . "/" . $dir; 
print "\$FGDCoutputFile = $FGDCoutputFile";

opendir FGDCFILE, "$FGDCoutputFile";

foreach $file (readdir FGDCFILE){
print "----------------------------------------------\n";
print "\$file = $file\n";

$firstdot=index($file,".")+1;
$extensionA=substr($file,$firstdot);

print "\$extensionA = $extensionA\n";


$FGDCoutputFileB = $FGDCoutputFile . "/" . $file;
print "\$FGDCoutputFileB = $FGDCoutputFileB\n";




if ($extensionA eq "ea.iso.xml"){
#2013_roads.ea.iso.xml
$postSecond=substr($file,5);
print "\$postSecond = $postSecond\n";
}else{
$postSecond=substr($file,8);
print "\$postSecond = $postSecond\n";
};

$UnderScoreLocation=index($postSecond,"_")+1;
$dotLocation=index($postSecond,".");
$Themelength=$dotLocation-$UnderScoreLocation;

$Theme=substr($postSecond,$UnderScoreLocation,$Themelength);
$fips=substr($postSecond,0,$UnderScoreLocation-1);
$statefips=substr($fips,0,2);

print "\$Theme = $Theme\n";
#print "\$fips = $fips\n";
#print "\$statefips = $statefips\n";

$ThemDir = $TargetDir ."/" . $Theme;
$ThemDirFile = $ThemDir . "/" . $file;

if (-e $ThemDir){

}else{
system "mkdir $ThemDir";
};


if (-e $ThemDirFile){

$AgeThemDirFile = -M $ThemDirFile;#already moved
$AgeFGDCoutputFile = -M $FGDCoutputFile;#to be moved

if (  $AgeFGDCoutputFile <$AgeThemDirFile){
#if ($TargetFips eq $fips){
print "now moving $FGDCoutputFileB\n";

if ( $extensionA eq "shp.iso.xml" or $extensionA eq "ea.iso.xml" or $extensionA eq "dbf.iso.xml"){
system "mv $FGDCoutputFileB $ThemDir";
$found="yes";
$moveCounter++;
};


#};
};

}else{
#f ($TargetFips eq $fips){
print "now moving $FGDCoutputFileB\n";

if ( $extensionA eq "shp.iso.xml" or $extensionA eq "ea.iso.xml" or $extensionA eq "dbf.iso.xml"){
system "cp $FGDCoutputFileB $ThemDir";
$moveCounter++;
$found="yes";
};

#}
};#if (-e $ThemDirFile){


};# foreach $file (readdir FGDCFILE){

$counter++;
}else{
#print "not reading directory\n";
$counter++;

};#if ($counter >=$beginCounter and $counter<=$endCounter and $dir ne  "." and $dir ne ".."){

#$counter++;
#}else{
#print "not reading directory\n";
#$counter++;
};# foreach $dir (readdir DHFGDC){a

if ($found eq "yes"){
print "$moveCounter directories under $FGDCDir have been opened and $moveCounter files have been moved to $TargetDir (\$endCounter = $endCounter)\n";
}; 
