#!/usr/bin/perl -w # NAME: genconfig.pl # AIM: Read a directory, and generate config info # 04/12/2012 - Add -t tile-out, -m map-out, -l layer-out - separate outputs # 09/11/2012 - Initial cut use strict; use warnings; use File::Basename; # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] ) use Cwd; my $os = $^O; my $PATH_SEP = '/'; if ($os =~ /win/i) { $PATH_SEP = "\\"; } my $pgmname = $0; if ($pgmname =~ /(\\|\/)/) { my @tmpsp = split(/(\\|\/)/,$pgmname); $pgmname = $tmpsp[-1]; } # user variables my $VERS = "0.0.2 2012-12-04"; #my $VERS = "0.0.1 2012-11-09"; my $in_dir = ''; my $verbosity = 0; my $out_file = ''; my $tile_file = ''; my $map_file = ''; my $layer_file = ''; my $recursive = 0; my @in_dirs = (); my @relief_arr = (); ### program variables my @warnings = (); my $cwd = cwd(); # ### DEBUG ### my $debug_on = 0; my $def_dir = 'C:\DTEMP'; my $tmp_out = $def_dir.'\tempout.txt'; sub VERB1() { return $verbosity >= 1; } sub VERB2() { return $verbosity >= 2; } sub VERB5() { return $verbosity >= 5; } sub VERB9() { return $verbosity >= 9; } sub prt($) { print shift; } sub show_warnings($) { my ($val) = @_; if (@warnings) { prt( "\nGot ".scalar @warnings." WARNINGS...\n" ); foreach my $itm (@warnings) { prt("$itm\n"); } prt("\n"); } else { prt( "\nNo warnings issued.\n\n" ) if (VERB9()); } } sub pgm_exit($$) { my ($val,$msg) = @_; if (length($msg)) { $msg .= "\n" if (!($msg =~ /\n$/)); prt($msg); } show_warnings($val); exit($val); } sub prtw($) { my ($tx) = shift; $tx =~ s/\n$//; prt("$tx\n"); push(@warnings,$tx); } sub append2file { my ($txt,$fil) = @_; open WOF, ">>$fil" or mydie("ERROR: Unable to open $fil! $!\n"); print WOF $txt; close WOF; } sub out_tilecache($) { my $name = shift; my $txt = < Relief gdal -1 tiff $path EOF prt("$txt\n"); if (length($map_file)) { append2file($txt,$map_file); } elsif (length($out_file)) { append2file($txt,$out_file) if (length($out_file)); } } sub fix_dir($) { my $rd = shift; if (length($rd) && (!(${$rd} =~ /(\\|\/)$/))) { ${$rd} .= $PATH_SEP; } } sub process_dir($); sub process_dir($) { my ($dir) = @_; if (! opendir(DIR,$dir)) { prtw("WARNING: Unable to open diectory $dir!\n"); return; } my @dirs = (); my @files = readdir(DIR); my $cnt = scalar @files; prt("Processing $cnt file items, from directory $dir...\n") if (VERB2()); my ($file,$n,$d,$e,$path,$ff); fix_dir(\$dir); foreach $file (@files) { next if ($file eq '.'); next if ($file eq '..'); $ff = $dir.$file; if ( -d $ff) { push(@dirs,$ff); next; } next if (!($file =~ /\.tif$/)); ($n,$d,$e) = fileparse($file , qr/\.[^.]*/ ); out_tilecache($n); $path = $dir.$file; out_mapnik($n,$path); push(@relief_arr,$n); } if ($recursive) { foreach $dir (@dirs) { process_dir($dir); } } } sub process_in_dirs($) { my ($ra) = @_; my $cnt = scalar @{$ra}; prt("Processing $cnt directories...\n"); my ($dir,$i); for ($i = 0; $i < $cnt; $i++) { $dir = ${$ra}[$i]; process_dir($dir); } } sub get_layer_txt1() { my $txt = < (-o) = Write output to this file.\n"); # 04/12/2012 - Add -t tile-out, -m map-out, -l layer-out prt(" --tile (-t) = Append tile-out to this file.\n"); prt(" --map (-m) = Append map-out to this file.\n"); prt(" --layer (-l) = Append layer-out to this file.\n"); } # eof - genconfig.pl