#!/usr/bin/perl
# Повышение контрастности карт с google maps

use File::Spec::Functions;
use File::Basename;
use File::Path;
use Cwd;
use utf8;

########################################

sub mogrifymap ($$$) # Разделить
{
    my ($path, $oldpref, $newpref) = @_;
    my @namelist; # Список имен обрабатываемых файлов
    my ($proj, $fname, $res, $xs, $ys, $lon1, $lat1, $x1, $y1, $lon2, $lat2, $x2, $y2, $newtxt);

    if ( -f catfile($path, 'maps.txt') ) {
	open(TXT, catfile($path, 'maps.txt') );
	while (<TXT>) {
	    chomp;
	    s/^\s+//;
	    s/\s/ /g;
	    if ($_ and not m/^\#/) {
		($proj, $fname, $res, $xs, $ys, $lon1, $lat1, $x1, $y1, $lon2, $lat2, $x2, $y2) = split(' ', $_);
		$fname = catfile($path, $fname); 
		print "processing $fname...\n";
		`mogrify -gamma 0.6 +contrast $fname`;
	    } else {$newtxt .= "$_\n"};
	};
	close(TXT);
    } else { print("There is no maps.txt file in the specified path.\n") };
};


# Начало
($path) = @ARGV;

    if ( -f catfile($path, 'maps.txt')) {
	mogrifymap($path, $oldpref, $newpref);
	print "Finished\n";
	exit;
    } else { print "Usage: path\n"};
