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

@twoDArray = ( 
   [ 'stuff', '0_1'], 
   [ 'stiff','1_1' ] 
); 

$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=8192;
$yBigTileSize=8192;
$xNoBigTiles=2;
$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));
$yExtraLevels=ceil(log($yNoBigTiles)/log(2.0));
$extraLevels=max($xExtraLevels,$yExtraLevels);
print "$xExtraLevels $yExtraLevels $extraLevels\n";
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.".jpeg";
             $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";

    }


$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=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;
    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="zoomify/";
   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 ");
       }
   }
