#!/usr/bin/ruby

require 'rubygems'
require 'date'

#REMOTE_REPOS="http://planet.openstreetmap.org/"
REMOTE_DIR="/data/planet/"
LOCAL_REPOS="/home/harrywood/public_html/waychains/"
MAX_TRIES=10
WAIT_TIME=15*60


def diff_name(d)
  "daily/" + (d-1).strftime("%Y%m%d") + "-" + d.strftime("%Y%m%d") + ".osc.gz"
end

def retry_until(max, wait)
  success = false
  max.times do
    success = yield
    break if success
    puts "Retrying"
    sleep wait
  end
  raise Exception.new("Retry limit exceeded.") unless success
end

today = Date.today

# download today's diff
file = diff_name(today)
#puts "Downloading today's diff #{REMOTE_REPOS + file}"
puts "Copying today's diff #{REMOTE_DIR + file}"
retry_until(MAX_TRIES, WAIT_TIME) do
	system("cp '#{REMOTE_DIR + file}' '#{LOCAL_REPOS + "todays-diff.osc.gz"}'")
	#system("wget -c -q -O '#{LOCAL_REPOS + "todays-diff.osc.gz"}' '#{REMOTE_REPOS + file}'")
end


