#!/usr/bin/perl
#
# Copyright (c) 2006 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.
#
# Takes in two OGSB 8 fix grid references, and prints out the corners
#
# Like David's terrifying debian-only rename line

use strict;
my $ref_a = shift;
my $ref_b = shift;

# Did they give a grid ref?
my @c1 = convert($ref_a);
my @c2 = convert($ref_b);

# Warn unless we got suitable numbers
unless(@c2) {
	print "Use:\n";
	print "\tcorners.pl <XX#####> <XX#####>\n";
	print "\n";
    print "In grid mode:\n";
    print "  Give the 2 letter + 6 digit references for two opposite corners\n";
	exit 1;
}

my $min_e = min($c1[0], $c2[0]);
my $max_e = max($c1[0], $c2[0]);
my $min_n = min($c1[1], $c2[1]);
my $max_n = max($c1[1], $c2[1]);

print "\n";
print "Top Left:  $min_e $max_n\n";
print "Top Right: $max_e $max_n\n";
print "Bot Left:  $min_e $min_n\n";
print "Bot Right: $max_e $min_n\n";
print "\n";
print "Rename Tiles TL BL is:\n";
print "\t".($min_e)." ".($max_n-1)."\n";

sub convert {
    my $ref = shift;
    if($ref =~ /^([HNOST])([A-Z])(\d{3})(\d{3})$/) {
        my ($km100, $km10, $re, $rn) = ($1,$2,$3,$4);
        my ($e, $n);

        # The big bit
        if($km100 eq "S") {
            $e = 0;
            $n = 0;
        } elsif($km100 eq "N") {
            $e = 0;
            $n = 500;
        } elsif($km100 eq "H") {
            $e = 0;
            $n = 1000;
        } elsif($km100 eq "T") {
            $e = 500;
            $n = 0;
        } elsif($km100 eq "0") {
            $e = 500;
            $n = 500;
        } else {
            die("Unknown grid letter '$km100' for '$ref'\n");
        }
        print "$e - $n\n";

        # The little bit
        my $idx = ord($km10) - 65;
        if($idx >= 9) {
            $idx--;
        }
        my $e_offset = (($idx % 5) * 100);
        my $n_offset = (int($idx / 5) * 100);
        print "$idx => $e_offset + $n_offset\n";
        $e += $e_offset;
        $n += (400 - $n_offset);

        # The rest
        print "$e - $n\n";
        $e += int($re / 10);
        $n += int($rn / 10);

        print "Translated $ref into $e easting, $n northing\n";
        return ($e,$n);
    }
}
sub min {
    my @a = @_;
    my $r = $a[0];
    foreach my $v (@a) {
        if($v < $r) { $r = $v; }
    }
    return $r;
}
sub max {
    my @a = @_;
    my $r = $a[0];
    foreach my $v (@a) {
        if($v > $r) { $r = $v; }
    }
    return $r;
}
