#!/usr/bin/perl
# 
# Copyright (c) 2006-2007 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.
#
use strict;

# Path to the scale directory on each server
my %servers;
#$servers{dev} = "/usr/local/OSGB/";
#$servers{encore} = "/usr/local/maps/OSGB/";
$servers{urchin} = "/srv/www/tile.npemap.org.uk/html/";
#$servers{bitclean} = "/home/bitclean/public_html/";
#$servers{openstreetmap} = "/home/dsheldon/npetiles/";

# Allow restricting to just one server
if($ARGV[0] && $servers{$ARGV[0]}) {
	my $srv = shift @ARGV;
	print "Restricting to just server $srv\n";
	my %srvs;
	$srvs{$srv} = $servers{$srv};
	%servers = %srvs;
}

# Process the rest of the arguments
my $scale = shift @ARGV;
my @args = @ARGV;

unless($scale && $args[0]) {
	print "Use:\n";
	print "  upload.pl <scale> [easting] [easting] [easting....]\n";
	print "\n";
	print "Will upload the specified easting directories to all 3 servers\n";
	exit 1;
}
unless($scale == 1 || $scale == 3 || $scale == 6 || $scale eq 'm') {
	die("Scale must be the 1st argument, and one of 1, 3, 6 or m\n");
}

# Check they're in the right directory
my $curdir = `pwd`;
chomp $curdir;
print "$curdir\n";
if($scale eq "m") { $curdir =~ s/meta1$/scaledm/; }
unless($curdir =~ /scaled$scale$/) {
    die("Can't upload for scale $scale from directory $curdir\n");
}

# Do they want to upload to osgb, or osie?
my $remote_append = "";
if($curdir =~ /\/ni-maps\//) {
    print "Detected OS Northern Ireland tiles, uploading to osie\n\n";
    $remote_append = "osie/";
} else {
    $remote_append = "osgb/";
}

# Are they uploading the latest tiles, or ones for just one
#  decade?
if($curdir =~ /\/(19[0-6]0s)\//) {
    my $decade = $1;
    print "Decade is set to the $decade\n";
    $remote_append .= $decade."/";
} else {
    print "No decade restriction, uploading latest tiles\n";
    $remote_append .= "latest/";
}

# Report what we're going to do
print "\n";
print "About to upload ".join(" ",@args)." for scale $scale\n\n";

my $dir = "scaled$scale/";
if($scale eq 'm') {
	$dir = "meta1/";
}

# Upload each in turn
foreach my $server (reverse sort keys %servers) {
	my $path = $servers{$server} . $remote_append . $dir;

	my $cmd = "rsync -avc --bwlimit=96 ".join(" ",@args)." $server:$path";
	print $cmd."\n";
	print `$cmd 1>&2`;
}
print "\n\n";

# Fix permissions on urchin
my $cmd = "ssh urchin 'cd $servers{urchin}$remote_append$dir && chown -R .npemap ".join(" ",@args)."'"; 
print $cmd."\n";
`$cmd`;

# Done
print "\n\n";
