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

#
# Author: Slaven Rezic
#
# Copyright (C) 1998,2011,2013 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: eserte@cs.tu-berlin.de
# WWW:  http://user.cs.tu-berlin.de/~eserte/
#

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

my $datadir = "$FindBin::RealBin/../data";
@Strassen::datadirs = ($datadir);

my $datafile     = 'strassen';
my $contausnahme = "$datadir/str_cont_ausnahme";
my $show_unconsumed;

GetOptions("contausnahme=s"   => \$contausnahme,
	   "data=s"           => \$datafile,
	   "showunconsumed"   => \$show_unconsumed,
	  );

my %ausnahme;
my %ausnahme_unconsumed;
if ($contausnahme ne '' and open(AUSNAHME, $contausnahme)) {
    while(<AUSNAHME>) {
	chomp;
	next if /^#/;
	$ausnahme{$_}++;
	$ausnahme_unconsumed{$_}++ if $show_unconsumed;
    }
    close AUSNAHME;
} else {
    warn "WARN: Couldn't load $contausnahme: $!";
}

my $seen_header = 0;

$| = 1; # we have a mixture of STDOUT+STDERR printing below, so autoflush has to be turned on.

my $str = new Strassen $datafile;
my $lastname;
my $lastlastcoord;
$str->init;
while(1) {
    my $s = $str->next_obj;
    last if $s->is_empty;
    my $s_name = $s->name;
    if (defined $lastname and $lastname eq $s_name) {
	if ($show_unconsumed || !exists $ausnahme{$s_name}) {
	    my($x, $y) = @{$s->coord_as_list(0)};
	    if ($lastlastcoord->[0] != $x ||
		$lastlastcoord->[1] != $y) {
		if ($show_unconsumed && exists $ausnahme{$s_name}) {
		    delete $ausnahme_unconsumed{$s_name};
		} else {
		    if (!$seen_header) {
			print STDERR "*** The following streets maybe need an entry in str_cont_ausnahme\n";
			$seen_header++;
		    }
		    print $s_name . "\n";
		}
	    }
	}
    }
    $lastname = $s_name;
    my @coords = $s->coords_list;
    $lastlastcoord = $s->coord_as_list($#coords);
}

if ($show_unconsumed && %ausnahme_unconsumed) {
    print STDERR "*** The following streets might be removed from str_cont_ausnahme\n";
    print join("\n", sort keys %ausnahme_unconsumed), "\n";
}

__END__

=head1 NAME

check_cont - check if streets are continuous in file

=head1 SYNOPSIS

Normal usage:

    cd data
    perl ../miscsrc/check_cont -data .strassen.tmp

Checking for unconsumed entries in str_cont_ausnahme:

    cd data
    perl ../miscsrc/check_cont -data .strassen.tmp -showunconsumed

=head1 DESCRIPTION

berprft, ob bei fortgesetzten Straen das vorherige Stck
aufschliet. In str_cont_ausnahme stehen die Ausnahmen (z.B. weil es
zwei unabhngige Teilstcke sind oder die Strae von einem Platz
unterbrochen wird).

=head1 AUTHOR

Slaven Rezic

=cut

