#!/usr/bin/perl
# tl --- this / last preprocessor
# Author: Dan Jacobson https://www.jidanni.org/
# Copyright: https://www.gnu.org/licenses/gpl.html
# Created: 2024-05-26T23:32:17+0000
# Last-Updated: 2024-06-22T07:36:40+0000
#     Update #: 76
#

use strict;
use warnings q!all!;
use utf8;
use HTML::Entities;
use URI::Escape;
use XML::LibXML;
my $Usage = "Usage: $0 <--preprocess|--postprocess>";
my $opt   = shift || die $Usage;
my $oo    = "https://www.openstreetmap.org";
my $mag   = 20;
use Data::Dumper;
if ( $opt eq "--preprocess" ) {
    my %h;
    my @order = reverse 0 .. 1;
    while (<>) {
        chomp;
        @{ $h{this} } = split /,/;
        if ( $h{last} ) {
            my @o;
            for (qw/last this/) {
                push @o, @{ $h{$_} }[@order];
            }
            for (qw/last this/) {
                push @o, @{ $h{$_} }[-1];
            }
            print "@o$/";
        }
        @{ $h{last} } = @{ $h{this} };
    }
}

elsif ( $opt eq "--postprocess" ) {
    my $doc   = XML::LibXML::Document->new( '1.0', "UTF-8" );
    my $head  = XML::LibXML::Element->new('head');
    my $title = XML::LibXML::Element->new('title');
    $head->appendChild($title);
    my $meta = XML::LibXML::Element->new('meta');
    $meta->setAttribute( 'http-equiv', 'Content-Type' );
    $meta->setAttribute( 'content',    'text/html; charset=UTF-8' );
    $head->appendChild($meta);
    $title->appendText('ND / SD border monuments');
    $meta = XML::LibXML::Element->new('meta');
    $meta->setAttribute( 'name',    'viewport' );
    $meta->setAttribute( 'content', 'width=device-width' );
    $head->appendChild($meta);
    my $html = XML::LibXML::Element->new('html');
    $html->appendChild($head);
    my $body = XML::LibXML::Element->new('body');
    $html->appendChild($body);
    my $p0 = XML::LibXML::Element->new('p');
    $p0->appendText(
        "N/S Dakota border monuments: iD editor links to approximate predicted locations. "
    );
    my $a0 = XML::LibXML::Element->new('a');
    $a0->setAttribute( "href", "osm.html" );
    $a0->appendText("Explanation");
    $p0->appendChild($a0);
    $p0->appendText(".");
    $body->appendChild($p0);
    my $p = XML::LibXML::Element->new('p');
##  $p->appendText( scalar localtime );
    $body->appendChild($p);
    my $outdoc = XML::LibXML::Document->new( '1.0', "UTF-8" );
    $outdoc->setDocumentElement($html);
    $outdoc->createInternalSubset( "html", (undef) x 2 );
    $body->appendChild($p);

    while (<>) {
        my %h;
        chomp;
        (
            @{ $h{west}{ll} }[ 0 .. 1 ],
            @{ $h{east}{ll} }[ 0 .. 1 ],
            $h{west}{azimuth},    #from west
            $h{east}{azimuth},    #from east
            $h{distance},
            $h{west}{name},
            $h{east}{name}
        ) = split;
        my $a = XML::LibXML::Element->new('a');
        $p->appendChild($a);
        $a->setAttribute( "href", map_link( @{ $h{west}{ll} } ) );
        $a->appendText( $h{west}{name} );
        {
            $h{west}{name} =~ /.$/;
            if ( $& eq 0 ) {
                $p->appendChild( XML::LibXML::Element->new('br') );
            }
            elsif ( $& =~ /\d/ ) { $p->appendText(" | ") }
            else                 { $p->appendText(" ") }
        }
    }
    $outdoc->toStringHTML();
    print $outdoc->toString($p);
}
else { die $Usage }

sub map_link {
    return sprintf "%s/edit#map=%d/%s/%s", $oo, $mag, @_;
}
