#!/usr/bin/perl
use threads;
use thread::queue;
use LWP::Simple;
use DateTime;
use DateTime::Duration;

select STDOUT; $| = 1;
my $q = Thread::Queue->new();


my @planet = (
  "http://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/daily/",
  "http://ftp.heanet.ie/mirrors/openstreetmap.org/daily/",
  "http://planet.openstreetmap.org/daily/"
);

@localPlanets = <planet-*.pbf>;

my @updateQueue;

my $newestPlanet = '20100101';
foreach $file (@localPlanets) {
   $file =~ /(\d{8})/;
   if ($1 > $newestPlanet) {
      $newestPlanet = $1;
   }
}

# initialize diff date
$newestPlanet =~ /(\d{4})(\d{2})(\d{2})/;
my $diffDate = DateTime->new( year => $1, month => $2, day => $3 );

print STDERR "Updating planet ".$diffDate->ymd."\n";

while ($diffDate < DateTime->today) {
   # construct update filename
   $start = $diffDate->ymd('');
   $diffDate->add_duration(DateTime::Duration->new(days => 1));
   $end = $diffDate->ymd('');
   $updateFile = $start.'-'.$end.'.osc.gz';

   # try to download updateFile
   foreach $server (@planet) { 
      # skip already existing updates
      last if (-e $updateFile);
      
      print STDERR "downloading $server$updateFile ...";
      $res = getstore($server.$updateFile, $updateFile);
      if (is_error($res)) {
         print STDERR "failed: $res\n";
         unlink $updateFile;
      } else {
         print STDERR "done\n";
      }
   }

   unless (-e $updateFile) {
      print STDERR "update $updateFile could not be found on the servers. Trying to apply previous diffs.\n";
      last;
   }
   
   # add update to array of diffs to apply
   push(@updateQueue, $updateFile);
   $resultPlanetDate = $end; # we have the diff, so update the end date
} 

# do we have updates to apply?
if (@updateQueue == 0) {
   print STDERR "No further updates available.\n";
   exit;
}

$osmosis = "..\\osmosis-r24958\\bin\\osmosis.bat";
foreach $diff (@updateQueue) {
   $osmosis .= " --rxc file=$diff --bc bufferCapacity=1000";
}
if (@updateQueue > 1) {
   $osmosis .= " --append-change sourceCount=".@updateQueue." --bc bufferCapacity=15000 --sort-change --bc  bufferCapacity=15000 --simplify-change --bc bufferCapacity=15000";
}
$osmosis .= " --read-pbf file=planet-$newestPlanet.pbf --buffer bufferCapacity=15000 --apply-change --buffer bufferCapacity=15000";

# additional extracts?
$osmosis .= " --tee outputCount=4";
$osmosis .= " --bounding-polygon file=..\\polygon_thailand_49865_simply500.poly --buffer bufferCapacity=15000 --write-xml file=thailand-$resultPlanetDate.osm";
$osmosis .= " --bounding-box left=9.39 right=25.35 top=33.34 bottom=19.5 --buffer bufferCapacity=15000 --write-xml file=libya-$resultPlanetDate.osm";
$osmosis .= " --bounding-box left=44.062 right=63.331 top=39.781 bottom=24.879 --buffer bufferCapacity=15000 --write-xml file=iran-$resultPlanetDate.osm";
# write new planet
$osmosis .= " --buffer bufferCapacity=15000 --write-pbf file=planet-$resultPlanetDate.pbf";

if (length($osmosis) > 8100) {
   print STDERR "command line too long to execute. It's ".length($osmosis)." characters long.\n";
   exit 1;
}

print "spawning: \n$osmosis\n";
system($osmosis);
