#!/usr/local/bin/perl
use POSIX qw(ceil floor);
use List::Util qw[min max];

@twoDArray = ( 
   [ 'gps0', '0_0'], 
   [ 'gps1','1_0' ],
   [ 'gps2','2_0' ],
   [ 'gps3','3_0' ],
   [ 'gps4','4_0' ],
   [ 'gps5','5_0' ],
   [ 'gps6','6_0' ],
   [ 'gps7','7_0' ],
   [ 'gps8','8_0' ],
   [ 'gps9','9_0' ],
   [ 'gps10','10_0' ],
   [ 'gps11','11_0' ],
   [ 'gps12','12_0' ],
   [ 'gps13','13_0' ],
   [ 'gps14','14_0' ],
   [ 'gps15','15_0' ],
   [ 'gps16','16_0' ],
   [ 'gps17','17_0' ],
   [ 'gps18','18_0' ],
   [ 'gps19','19_0' ],
   [ 'gps20','20_0' ],
   [ 'gps21','21_0' ],
   [ 'gps22','22_0' ],
   [ 'gps23','23_0' ],
   [ 'gps24','24_0' ],
   [ 'gps25','25_0' ]
); 

$count=0;
@fileNames;
print "$twoDArray[0][0]\n";
print "$twoDArray[1][0]\n";
print "$twoDArray[0][1]\n";
print "$twoDArray[1][1]\n";
print "running\n";
$xBigTileSize=16384;
$yBigTileSize=$xBigTileSize;
$xNoBigTiles=26;
$yNoBigTiles=1;
#@bigTileName=("stuff","stiff");
$xTotSize=$xNoBigTiles*$xBigTileSize;
$yTotSize=$yNoBigTiles*$yBigTileSize;
$tileSize=256;
$xTileMakerLevels=1+log($xBigTileSize/$tileSize)/log(2.0);
$yTileMakerLevels=1+log($yBigTileSize/$tileSize)/log(2.0);
$xNoTile=$xBigTileSize/$tileSize;
$yNoTile=$yBigTileSize/$tileSize;
print "$xTotSize $yTotSize $xTileMakerLevels\n";

$xExtraLevels=ceil(log($xNoBigTiles)/log(2.0));
#$xExtraLevels=4;
$yExtraLevels=ceil(log($yNoBigTiles)/log(2.0));
$extraLevels=max($xExtraLevels,$yExtraLevels);
print "$xExtraLevels $yExtraLevels $extraLevels\n";
#exit;
#print "$bigTileName[0] : $bigTileName[1]\n";
for ($j=0;$j<$yNoBigTiles;$j++){
    for ($i=0;$i<$xNoBigTiles;$i++){
	print "$i $j \n";
        for ($l=0;$l<$xTileMakerLevels;$l++){
	    $noTiles=2**($l);
#print "$twoDArray[$i][$j] :: $l :: $noTiles\n";
        for ($n=0;$n<$noTiles;$n++){
	    for ($m=0;$m<$noTiles;$m++) {
#             print "$twoDArray[$i][$j] :: $l :: $m :: $n\n";
             $tileFile=$twoDArray[$i][$j]."-".$l."-".$m."-".$n.".jpg";
             $newLevel=$l+$extraLevels;
             $newM=$m+($i*2**$l);
	     $newN=$n+($j*2**$l);
	     $newFile=$newLevel."-".$newM."-".$newN.".jpg";
             if (-e $tileFile){
                 $score=sprintf("%05d",$newLevel).sprintf("%08d",$newN).sprintf("%08d",$newM);
                 $fileNames[$count]=$newFile;
                 $lxy{$score}=$newFile;
                 $lookUp{$newFile}=$tileFile;
		 print "$tileFile yes $newFile $score : $lxy{$score} \n";
 
		 $count++;
	     } else {
#		 print "$tileFile no\n";
	     }

             #$newLevel=$l;
	    }
	}
	    
	}


    }


}
#print "$fileNames[0]\n";
    $noFiles=@fileNames;
    print "$noFiles\n";

   for ($i=0;$i<@fileNames;$i++){
#	print "$fileNames[$i] : $lxy{$fileNames[$i]}\n";

    }
#exit;

$xExtraSize=$xNoBigTiles*$tileSize;
$yExtraSize=$yNoBigTiles*$tileSize;

$xNo=$xNoBigTiles;
$yNo=$yNoBigTiles;

$xSize=$xExtraSize/2;
$ySize=$yExtraSize/2;
print ceil(3.9);
print ceil(3);
$level=$extraLevels-1;
print "\n $xSize $ySize \n";
while ( ($xSize >= $tileSize || $ySize >= $tileSize) || $xSteps > 1 || $ySteps > 1){
    $xSteps=max(1,ceil($xSize/$tileSize));
    $ySteps=max(1,ceil($ySize/$tileSize));
    print "x/y steps $xSteps $ySteps $xSize $ySize\n";
$ySize=$ySize/2;
#$xSize=$xSize/2;
    $xSize=($xsteps*$tileSize/2)+($xSize-$tileSize*$xsteps)/2;
    print "$xSize :::\n";
    for ($j=0;$j<$ySteps;$j++){
	
	    for ($i=0;$i<$xSteps;$i++){
	#	$xSize=$xSize/2;
             print "$level $i $j \n"; 
                 $score=sprintf("%05d",$level).sprintf("%08d",$j).sprintf("%08d",$i);
	     $extraFile=$level."-".$i."-".$j.".jpg";
                 $fileNames[$count]=$extraFile;
                 $lxy{$score}=$extraFile;
                 $lookUp{$extraFile}="null";
	    }
    }
  
$level--;   
}
              

    @listScore=keys(%lxy);
    @sortScore=sort  @listScore;
$tileDir=0;
$fileCount=1;
$prefix="zoom4/";
   for ($i=0;$i<@sortScore;$i++){
#       $fileCount++;
       if ($fileCount > 256){
	   $fileCount=1;
           $tileDir++;
       }
       $fileCount++;
       $currentName= $lookUp{$lxy{$sortScore[$i]}};
       $groupDir=$prefix."TileGroup".$tileDir;
       unless ( -e $groupDir){
       mkdir ($groupDir,0777);
   }
       print "$lxy{$sortScore[$i]} $sortScore[$i] $i $currentName $groupDir\n";
       if (-e $currentName){
       $fullNew=$groupDir."/".$lxy{$sortScore[$i]};
#      system ("cp $currentName $fullNew ");
       }
   }
