#!/usr/bin/perl
# 
# Copyright (c) 2007 Nick Burch
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#                   NPE Maps API Mirroring
#                   ----------------------
#
# This script handles downloading the latest NPEMaps dataset export (but
#  not the latest FreeThePostcode export), and importing it into a local
#  copy of the NPE Maps database.
# This allows you to run your own copy of the NPE Maps api, and have it
#  loaded with the latest data nightly / weekly
# (You will almost certainly want to schedule a run of the
#  freethepostcode importer too)
#
# Warning - you should not be tracking bad postcodes if you use this
#  script, as it will zap everything it has previously imported between
#   runs!

# Find our private perl libraries
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../../perllib";
use NPEMap;
use strict;

# Download the npemap file
my $npemap_url = "http://www.npemap.org.uk/data/currentlist";
print `wget -O /tmp/npemap-currentlist -q $npemap_url`;
if($?) { exit $?; }

# Our entry in the sources table
my $source = 2;

# Clear out the current entries
my $dbh = setup_dbh();
my $del = $dbh->prepare("DELETE FROM postcodes WHERE source = ?");
$del->execute($source);

# Load up the new ones
my $ins = $dbh->prepare("INSERT INTO postcodes (outward,inward,raw_postcode_outward,raw_postcode_inward,easting,northing,source) VALUES (?,?,?,?,?,?,$source)");

open(POSTCODES, "</tmp/npemap-currentlist");
while(my $line = <POSTCODES>) {
	chomp $line;
	if($line =~ /^\#/) { next };
	my ($outward,$inward,$e,$n) = 
		($line =~ /^([\w\d]+),([\w\d]*),(\d+),(\-?\d+),/);
	if($outward) {
		$ins->execute($outward,$inward,$outward,$inward,$e,$n);
	} else {
		warn("Invalid line '$line'\n");
	}
}

# Close our connection
$dbh->disconnect;
