#!/usr/bin/perl -w # NAME: cmpacdirs.pl # AIM: VERY SPECIFIC - Compare two aircraft folder. Report missing and differences use strict; use warnings; use File::Basename; # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] ) use File::stat; use Time::gmtime; use Cwd; my $os = $^O; my $perl_dir = '/home/geoff/bin'; my $PATH_SEP = '/'; my $temp_dir = '/tmp'; if ($os =~ /win/i) { $perl_dir = 'C:\GTools\perl'; $temp_dir = $perl_dir; $PATH_SEP = "\\"; } unshift(@INC, $perl_dir); require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl' Check paths in \@INC...\n"; # log file stuff our ($LF); my $pgmname = $0; if ($pgmname =~ /(\\|\/)/) { my @tmpsp = split(/(\\|\/)/,$pgmname); $pgmname = $tmpsp[-1]; } my $outfile = $temp_dir.$PATH_SEP."temp.$pgmname.txt"; open_log($outfile); # user variables my $VERS = "0.0.5 2015-01-09"; my $load_log = 0; my $in_file = ''; my $verbosity = 0; my $out_file = ''; my $in_dir1 = 'C:\OSGeo4W\apache\htdocs\map-test2\ac'; my $in_dir2 = 'F:\FGx\fgx-aircraft\data'; my $out_file1 = $temp_dir.$PATH_SEP."tempcopy.bat"; my $out_file2 = $temp_dir.$PATH_SEP."tempdiff.bat"; my $out_file3 = $temp_dir.$PATH_SEP."tempcopy2.bat"; # ### DEBUG ### my $debug_on = 0; my $def_file = 'def_file'; ### program variables my @warnings = (); my $cwd = cwd(); sub VERB1() { return $verbosity >= 1; } sub VERB2() { return $verbosity >= 2; } sub VERB5() { return $verbosity >= 5; } sub VERB9() { return $verbosity >= 9; } 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); close_log($outfile,$load_log); exit($val); } sub prtw($) { my ($tx) = shift; $tx =~ s/\n$//; prt("$tx\n"); push(@warnings,$tx); } sub process_in_file($) { my ($inf) = @_; if (! open INF, "<$inf") { pgm_exit(1,"ERROR: Unable to open file [$inf]\n"); } my @lines = ; close INF; my $lncnt = scalar @lines; prt("Processing $lncnt lines, from [$inf]...\n"); my ($line,$inc,$lnn); $lnn = 0; foreach $line (@lines) { chomp $line; $lnn++; if ($line =~ /\s*#\s*include\s+(.+)$/) { $inc = $1; prt("$lnn: $inc\n"); } } } sub process_in_dir1($) { my ($dir) = @_; if ( !opendir( DIR, $dir ) ) { prtw("WARNING: Unable to open dir $dir!\n"); return; } my @files = readdir(DIR); closedir DIR; my ($itm,$ff,$sb,$sz,$cfg,$tm); ut_fix_directory(\$dir); my @jsfiles = (); ##$total_dirs++; foreach $itm (@files) { next if ($itm eq '.'); next if ($itm eq '..'); next if (!($itm =~ /\.js$/)); $ff = $dir.$itm; if (-f $ff) { $sz = 0; $tm = 0; if ($sb = stat($ff)) { $sz = $sb->size; $tm = $sb->mtime; } push(@jsfiles,[$itm,$ff,$sz,$tm]); #$total_files++; } elsif (-d $ff) { ##$cfg = $ff.$PATH_SEP.".git".$PATH_SEP."config"; } } $itm = scalar @jsfiles; prt("Got $itm js file from $dir...\n") if (VERB9()); return \@jsfiles; } sub process_in_dir2($) { my ($dir) = @_; if ( !opendir( DIR, $dir ) ) { prtw("WARNING: Unable to open dir $dir!\n"); return; } my @files = readdir(DIR); closedir DIR; my ($itm,$ff,$sb,$sz,$cfg,$tm); ut_fix_directory(\$dir); my @jsfiles = (); ##$total_dirs++; foreach $itm (@files) { next if ($itm eq '.'); next if ($itm eq '..'); ###next if (!($itm =~ /\.js$/)); $ff = $dir.$itm; if (-f $ff) { next if (!($itm =~ /\.js$/)); $sz = 0; $tm = 0; if ($sb = stat($ff)) { $sz = $sb->size; $tm = $sb->mtime; } push(@jsfiles,[$itm,$ff,$sz,$tm]); #$total_files++; } elsif (-d $ff) { ##$cfg = $ff.$PATH_SEP.".git".$PATH_SEP."config"; my $refarr = process_in_dir1($ff); foreach $cfg (@{$refarr}) { push(@jsfiles,$cfg); } } } $itm = scalar @jsfiles; prt("Got $itm js file from $dir...\n") if (VERB9()); return \@jsfiles; } sub process_dirs() { my ($cnt1,$cnt2,$ra1,$ra2); my $refa1 = process_in_dir1($in_dir1); $cnt1 = scalar @{$refa1}; prt("Got $cnt1 js file from $in_dir1...\n"); my $refa2 = process_in_dir2($in_dir2); $cnt2 = scalar @{$refa2}; prt("Got $cnt2 js file from $in_dir2...\n"); my ($fil1,$ff1,$sz1,$tm1); my ($fil2,$ff2,$sz2,$tm2); my ($fnd,$cpy,$dif,$cpy2); my (@arr,$dtxt,$len,$cnt3); $cpy = ''; $dif = ''; $cpy2 = ''; $cnt1 = 0; $cnt2 = 0; $cnt3 = 0; foreach $ra2 (@{$refa2}) { $fil2 = ${$ra2}[0]; $ff2 = ${$ra2}[1]; $sz2 = ${$ra2}[2]; $tm2 = ${$ra2}[3]; $fnd = 0; foreach $ra1 (@{$refa1}) { $fil1 = ${$ra1}[0]; $ff1 = ${$ra1}[1]; $sz1 = ${$ra1}[2]; $tm1 = ${$ra1}[3]; if ($fil2 eq $fil1) { $fnd = 1; last; } } if ($fnd) { $dif .= "diff -u $ff1 $ff2\n"; if (open (DIFF, "diff -u $ff1 $ff2|")) { @arr = ; close DIFF; $dtxt = join("",@arr); $len = length($dtxt); if ($len) { $cpy2 .= "copy $ff2 $in_dir1\n"; $cnt3++; } else { prt("NOTE: File $fil2 are the SAME...\n"); } } else { prt("ERROR: FAILED to get DIFF text!\n"); pgm_exit(1, "PREMATURE ERROR EXIT!\n"); } $cnt2++; } else { prt("$fil2 not found in dir1\n"); $cpy .= "copy $ff2 $in_dir1\n"; $cnt1++; } } write2file($cpy,$out_file1); prt("Written $cnt1 to $out_file1\n"); write2file($dif,$out_file2); prt("Written $cnt2 to $out_file2\n"); write2file($cpy2,$out_file3); prt("Written $cnt3 to $out_file3\n"); } ######################################### ### MAIN ### #parse_args(@ARGV); #process_in_file($in_file); process_dirs(); pgm_exit(0,""); ######################################## sub need_arg { my ($arg,@av) = @_; pgm_exit(1,"ERROR: [$arg] must have a following argument!\n") if (!@av); } sub parse_args { my (@av) = @_; my ($arg,$sarg); my $verb = VERB2(); while (@av) { $arg = $av[0]; if ($arg =~ /^-/) { $sarg = substr($arg,1); $sarg = substr($sarg,1) while ($sarg =~ /^-/); if (($sarg =~ /^h/i)||($sarg eq '?')) { give_help(); pgm_exit(0,"Help exit(0)"); } elsif ($sarg =~ /^v/) { if ($sarg =~ /^v.*(\d+)$/) { $verbosity = $1; } else { while ($sarg =~ /^v/) { $verbosity++; $sarg = substr($sarg,1); } } $verb = VERB2(); prt("Verbosity = $verbosity\n") if ($verb); } elsif ($sarg =~ /^l/) { if ($sarg =~ /^ll/) { $load_log = 2; } else { $load_log = 1; } prt("Set to load log at end. ($load_log)\n") if ($verb); } elsif ($sarg =~ /^o/) { need_arg(@av); shift @av; $sarg = $av[0]; $out_file = $sarg; prt("Set out file to [$out_file].\n") if ($verb); } else { pgm_exit(1,"ERROR: Invalid argument [$arg]! Try -?\n"); } } else { $in_file = $arg; prt("Set input to [$in_file]\n") if ($verb); } shift @av; } if ($debug_on) { prtw("WARNING: DEBUG is ON!\n"); if (length($in_file) == 0) { $in_file = $def_file; prt("Set DEFAULT input to [$in_file]\n"); } } if (length($in_file) == 0) { pgm_exit(1,"ERROR: No input files found in command!\n"); } if (! -f $in_file) { pgm_exit(1,"ERROR: Unable to find in file [$in_file]! Check name, location...\n"); } } sub give_help { prt("$pgmname: version $VERS\n"); prt("Usage: $pgmname [options] in-file\n"); prt("Options:\n"); prt(" --help (-h or -?) = This help, and exit 0.\n"); prt(" --verb[n] (-v) = Bump [or set] verbosity. def=$verbosity\n"); prt(" --load (-l) = Load LOG at end. ($outfile)\n"); prt(" --out (-o) = Write output to this file.\n"); } # eof - template.pl