#!/usr/bin/perl 
#
# Little wrapper around the usefull Geo::Coordinates::RDNAP cause the Dutch
# Cadaster Organisation decided to take down their usefull one.
#
# License: BSDLike - http://rickvanderzwet.nl/LICENSE
#
# Rick van der Zwet <info@rickvanderzwet.nl>
use lib 'lib';
use strict;
use Geo::Coordinates::RDNAP qw/from_rd to_rd dms/;

print "Content-type: text/plain\n\n";
my @uri = split /\//, $ENV{'REQUEST_URI'};
if ($#uri lt 5) {
  print "ERROR in parameters size (example /rdnap/rd2etrs/150.000/480.000/0)\n";
  exit 1;
}
shift(@uri);
my ($script, $command, $raw1, $raw2, $raw3) = @uri;
$raw1 =~ tr/_//d;
$raw2 =~ tr/_//d;

my ($res1, $res2, $res3) = 0;

if ($command eq "rd2etrs") {
  ($res1, $res2, $res3) = from_rd($raw1, $raw2, $raw3);
  $res1 = sprintf("%.5f", $res1);
  $res2 = sprintf("%.5f", $res2);
  $res3 = sprintf("%.5f", $res3);
} elsif( $command eq "etrs2rd") {
  ($res1, $res2, $res3) = to_rd($raw1, $raw2, $raw3);
  $res1 = sprintf("%.0f", $res1);
  $res2 = sprintf("%.0f", $res2);
  $res3 = sprintf("%.2f", $res3);
} else {
  print "ERROR in parameters size (example /rdnap/rd2etrs/150.000/480.000/0)\n";
  exit 1;
}

print join("/",($res1, $res2, $res3));
print "\n";
