]> git.nbdom.net Git - nb.git/commitdiff
bin/shell-replace
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 18:42:18 +0000 (19:42 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Sat, 10 Dec 2022 18:42:18 +0000 (19:42 +0100)
bin/shell-replace

index c2a76a692faa5daa9634aba81ec0148d1a463e89..3c78cfb2de4084d7ecb556857744db0f55f2c83b 100755 (executable)
@@ -40,6 +40,7 @@ $main::_DATA_ = undef;
 #
 #################################################################################
 use Data::Dumper;
+use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END);
 use File::Find;
 if (!@ARGV and !-t STDIN) {
        die "$NAME: option require file/path as arguments!\n" if $Opt{find};
@@ -72,18 +73,22 @@ my $file;
 for $file (@ARGV) {
 
        if ($file eq "-") {
-               $hfile = <>;
-               $hfile = \*ARGV;
+               #$hfile = \*ARGV;
+               $hfile = \*STDIN;
        } else {
                next if myself($file);
                open($hfile,"<",$file);
        }
+       my @original = <$hfile>;
+       seek($hfile,0,SEEK_SET);
 
-       verbose(1,">Processing: $file\n");
+       verbose(1,"Processing: $file\n");
 
        my @lines = &shell_replace($hfile);
        print map { $_ } @lines;
+
        close $hfile;
+       verbose(1,"Changed: $file\n") if join('',@original) ne join('',@lines);
 }
 
 #################################################################################
@@ -100,9 +105,11 @@ exit 0;
 #################################################################################
 sub verbose {
        my $level = shift @_;
-       return 0 if $VERBOSE <= $level;
+       return 0 if $VERBOSE < $level;
        print STDERR join(" ",@_)."\n";
+       return 1;
 }
+
 sub myself {
        my $file = shift @_;
        if ($file eq $0 or $file =~ /(^|\/)$NAME$/) {
@@ -126,7 +133,7 @@ sub wanted {
 
                next unless /^.{1,3}<SHELL_REPLACE/;
 
-               verbose(1,">Found: $file\n");
+               verbose(1,"Found: $file\n");
                push @WANTED_FILES, $file;
                last;
        }
@@ -134,6 +141,7 @@ sub wanted {
 }
 
 sub shell_replace {
+# Search tags and execute command, return new lines
 my $ident;
 my $i;
 my $change;