make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / vifiles
blobe1551b8c67dbf10d8ca9a0689c32729bdfec69ae
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 vifiles - Edit multiple files as one
10 =head1 CAVEATS
12 If a LF char at the end of any files is missing, it'll be added after edit.
14 =head1 SEE ALSO
16 vidir(1) from moreutils
18 =cut
20 EOF
24 Recursive=no
25 Force=no
26 shopt -s nullglob
28 cleanup()
30 rm "$tmpfile"
33 prepcat()
35 dir_depth=$[dir_depth + 1]
36 for file in "$@"
38 if [ -d "$file" ]
39 then
40 if [ $dir_depth -le 1 -o $Recursive = yes ]
41 then
42 prepcat "$file"/*
44 else
45 cat "$file" | filename=$file perl -pe 's/^/$ENV{filename}:/; s/\n?$/\n/'
46 nfiles=$[nfiles + 1]
48 done
51 tmpfile=`mktemp -t vifiles.tmp-XXXXXX`
52 trap cleanup EXIT
53 set -e
55 while [ $# -gt 0 ]
57 case "$1" in
58 -R|-r|--recursive)
59 Recursive=yes;;
60 -f|--force)
61 Force=yes;;
62 -h|--help)
63 echo "vifiles - edit multiple files at once
65 Usage: vifiles [-R] [FILES]
66 Options:
67 -R, -r, --recursive open all files within the directories specified in parameters
68 -f, --force don't ask confirmation to save changes (provided that the editor exited without error code)
70 vifiles opens up your text editor (search list: \$EDITOR, \$VISUAL, vi) to edit a file with the content of all of the files specified in parameters, each line prefixed with the file name from which it's coming from. Save changes back to the original files."
71 exit;;
72 --) shift
73 break;;
74 -*) echo "Unknown option: $1" >&2
75 false;;
76 *) break;;
77 esac
78 shift
79 done
81 nfiles=0
82 dir_depth=0
83 prepcat "$@" >"$tmpfile"
85 echo "Opening $nfiles files to edit ..." >&2
87 command ${EDITOR:-${VISUAL:-vi}} "$tmpfile"
88 # 'errexit' is set
90 read -e -p "Apply changes? [y/n] " -i n
91 if [ "${REPLY^^}" != Y ]
92 then
93 exit
96 # create directories if any new added in the file paths
97 cat "$tmpfile" | cut -s -f1 -d: | sed -e 's@\(.\+\)/[^/]\+@\1@' | sort -u | xargs --verbose -d "\n" mkdir -v -p
99 echo "Saving edited files ..." >&2
101 cat "$tmpfile" | perl -ne '
102 if(($path, $line) = /([^:]+):(.*)/s)
104 if(not exists $fhs{$path})
106 if(not open $fhs{$path}, ">", $path)
108 warn "vifiles: $path: $!\n";
109 $fhs{$path} = undef;
112 if(defined $fhs{$path})
114 print {$fhs{$path}} $line or warn sprintf "vifiles: %s while writing %d bytes to %s\n", $!, length $line, $path;