#!/usr/bin/perl -w
# -*- perl -*-

#
# Author: Slaven Rezic
#
# Copyright (C) 2009,2012,2014 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: slaven@rezic.de
# WWW:  http://www.rezic.de/eserte/
#

use strict;
use FindBin;
use lib ("$FindBin::RealBin/..",
	 "$FindBin::RealBin/../lib",
	);

use Getopt::Long;

use Geography::Berlin_DE;
use PLZ;

# Manually created using data from
# http://www.statistik-berlin.de/berl/regional/plz_bez2004.pdf
my @raw_data =
    ([qw(Mitte
	 10115 10117 10119 10178 10179 10435 10551 10553 10555 10557
	 10559 10623 10785 10787 10963 10969 13347 13349 13351 13353
	 13355 13357 13359 13405 13407 13409)],
     [qw(Friedrichshain-Kreuzberg
	 10179 10243 10245 10247 10249 10367 10407 10785 10961 10963
	 10965 10967 10969 10997 10999)],
     [qw(Pankow
	 10119 10247 10249 10405 10407 10409 10435 10437 10439 13053
	 13086 13088 13089 13125 13127 13129 13156 13158 13159 13187
	 13189
	 13051
       )], # note: 13051 was additionally introduced, because of Blankenburger Pflasterweg
     [qw(Charlottenburg-Wilmersdorf
	 10553 10585 10587 10589 10623 10625 10627 10629 10707 10709
	 10711 10713 10715 10717 10719 10777 10779 10787 10789 10825
	 13353 13597 13627 13629 14050 14052 14053 14055 14057 14059
	 14193 14195 14197 14199)],
     [qw(Spandau
	 13581 13583 13585 13587 13589 13591 13593 13595 13597 13599
	 13627 13629 14052 14089)],
     [qw(Steglitz-Zehlendorf
	 12157 12161 12163 12165 12167 12169 12203 12205 12207 12209
	 12247 12249 12277 12279 14109 14129 14163 14165 14167 14169
	 14193 14195 14197 14199)],
     [qw(Tempelhof-Schneberg
	 10777 10779 10781 10783 10785 10787 10789 10823 10825 10827
	 10829 10965 12099 12101 12103 12105 12107 12109 12157 12159
	 12161 12163 12169 12249 12277 12279 12305 12307 12309 12347
	 14197)],
     [qw(Neuklln
	 10965 10967 12043 12045 12047 12049 12051 12053 12055 12057
	 12059 12099 12107 12305 12347 12349 12351 12353 12355 12357
	 12359)],
     [qw(Treptow-Kpenick
	 12435 12437 12439 12459 12487 12489 12524 12526 12527 12555
	 12557 12559 12587 12589 12623)],
     [qw(Marzahn-Hellersdorf
	 12555 12619 12621 12623 12627 12629 12679 12681 12683 12685
	 12687 12689)],
     [qw(Lichtenberg
         10315 10317 10318 10319 10365 10367 10369 13051 13053 13055
	 13057 13059)],
     [qw(Reinickendorf
	 13403 13405 13407 13409 13435 13437 13439 13465 13467 13469
	 13503 13505 13507 13509 13599 13629)],
    );
my %bezirk_to_plz;
for my $bezirk_def (@raw_data) {
    my $bezirk = shift @$bezirk_def;
    $bezirk_to_plz{$bezirk} = { map { ($_,1) } @$bezirk_def };
}

my $single_check;
GetOptions("single-check=s" => \$single_check)
    or die "usage: $0 [--single-check 'Street|Citypart|Zip']\n";

my @errors;
my @warnings;

if ($single_check) {
    my $rec = [ split /\|/, $single_check ];
    do_check($rec);
} else {
    my $plz = PLZ->new;
    $plz->load;
    for my $rec (@{ $plz->{Data} }) {
	do_check($rec);
    }
}

require Data::Dumper; print STDERR "Line " . __LINE__ . ", File: " . __FILE__ . "\n" . Data::Dumper->new([\@warnings, \@errors],[qw(warnings errors)])->Indent(1)->Useqq(1)->Dump; # XXX

exit(@errors == 0 ? 0 : 1);

sub do_check {
    my $rec = shift;

    my $citypart = $rec->[PLZ::FILE_CITYPART]
	or do { push @warnings, "No citypart for $rec->[PLZ::FILE_NAME]"; return };
    my $plz = $rec->[PLZ::FILE_ZIP]
	or do { push @warnings, "No plz for $rec->[PLZ::FILE_NAME]"; return };
    my $super_citypart = Geography::Berlin_DE->get_supercitypart_for_any($citypart)
	or do { push @warnings, "Cannot find super citypart for $citypart"; return };
    if (!exists $bezirk_to_plz{$super_citypart}->{$plz}) {
	push @errors, "Unexpected plz <$plz> for super citypart <$super_citypart>, street <$rec->[PLZ::FILE_NAME]>";
    }
}

__END__

=head1 NAME

check_berlin_plz - checks ZIPs against citypart list

=head1 DESCRIPTION

Anhand einer Tabelle von statistik-berlin.de wird geprft, ob alle
Bezirk-PLZ-Kombinationen in Berlin.coords.data korrekt sind.

Mit der Option --single-check kann die berprfung fr eine einzelne
Zeile gemacht werden:

    check_berlin_plz --single-check='Priesterweg|Schneberg|12157'

=cut
