$end_time = $start_time + 60 * $param['timeout'];
$prefixes = $db->GetAll("select * from gridprefix order by rand();");
foreach ($prefixes as $idx => $prefix) {
    //sleep until calm if we've specified a load average
    if ($param['load'] < 100) {
        while (get_loadavg() > $param['load']) {
            sleep($param['sleep']);
            if (time() > $end_time) {
                exit;
            }
        }
    }
    //mysql might of closed the connection in the meantime if we reuse the same object
    $mosaic = new GeographMapMosaic();
    print "Starting {$prefix['prefix']}...\n";
    flush();
    $minx = $prefix['origin_x'];
    $maxx = $prefix['origin_x'] + $prefix['width'] - 1;
    $miny = $prefix['origin_y'];
    $maxy = $prefix['origin_y'] + $prefix['height'] - 1;
    $crit = "map_x between {$minx} and {$maxx} and " . "map_y between {$miny} and {$maxy} and " . "pixels_per_km >= 40 and " . "((map_x-{$prefix['origin_x']}) mod 5) != 0 and " . "((map_y-{$prefix['origin_y']}) mod 5) != 0";
    $count = $mosaic->deleteBySql($crit, $param['dryrun'], $param['base']);
    print "Deleted {$count}\n";
    $total += $count;
    if (time() > $end_time) {
        //well come to the end of the scripts useful life
        exit;
    }
}
print "Total: {$total}\n";
exit;
    foreach ($prefixes as $idx => $prefix) {
        list($usec, $sec) = explode(' ', microtime());
        $endtime = (double) $usec + (double) $sec;
        $timetaken = $endtime - $STARTTIME;
        if ($timetaken > 15) {
            //mysql might of closed the connection in the meantime
            unset($mosaic->db);
        }
        print "<h3>{$prefix['prefix']}</h3>";
        flush();
        $minx = $prefix['origin_x'];
        $maxx = $prefix['origin_x'] + $prefix['width'] - 1;
        $miny = $prefix['origin_y'];
        $maxy = $prefix['origin_y'] + $prefix['height'] - 1;
        $crit = "map_x between {$minx} and {$maxx} and " . "map_y between {$miny} and {$maxy} and " . "pixels_per_km >= 40 and " . "((map_x-{$prefix['origin_x']}) mod 5) != 0 and " . "((map_y-{$prefix['origin_y']}) mod 5) != 0";
        $count = $mosaic->deleteBySql($crit, $dummy, $basemap);
        print "Deleted {$count}<br>";
        flush();
        $total += $count;
        list($usec, $sec) = explode(' ', microtime());
        $GLOBALS['STARTTIME'] = (double) $usec + (double) $sec;
    }
    print "<h2>Total: {$total}</h2>";
    exit;
} elseif (isset($_POST['inv'])) {
    $square = new GridSquare();
    require_once 'geograph/mapmosaic.class.php';
    $mosaic = new GeographMapMosaic();
    $smarty->display('_std_begin.tpl');
    echo "<h3><a href=\"recreatemaps.php\">&lt;&lt;</a> Invalidating Maps...</h3>";
    flush();