#!/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.
#
# Rename tiles, based on the position of the top left tile
#
# Like David's terrifying debian-only rename line

use strict;
my $tl_e = shift;
my $tl_n = shift;

# Warn unless we got suitable numbers
unless($tl_n) {
	print "Use:\n";
	print "\trename-tiles.pl <top left easting> <top left northing-1>\n";
	print "\n";
	print "Give the bottom left easting and northing of the top left most\n";
	print " tile, eg top right is 244,859 given 244,858\n";
	exit 1;
}

foreach my $f (<tile-*-*.jpg>) {
	my ($e,$n) = ($f =~ /^tile\-([0-9]+)\-([0-9]+).jpg$/);
	unless(length($n)) { warn("Invalid '$f'\n"); next; }

	my $easting = $tl_e + $e;
	my $northing = $tl_n - $n;
	my $nf = sprintf("tile-%03d-%03d.jpg",$easting,$northing);

	print "$f\n$easting $northing\n";
	rename($f,$nf);
}
