3 #=======================================================================
5 # File ID: ba028ba2-2287-11e2-b260-00c0a8deee11
9 # ©opyleft 2012– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
25 'sumfile' => 'Files.sha1',
35 'sumfile' => $Std{'sumfile'},
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = '0.00';
46 Getopt
::Long
::Configure
('bundling');
49 'after|a' => \
$Opt{'after'},
50 'before|b' => \
$Opt{'before'},
51 'debug' => \
$Opt{'debug'},
52 'help|h' => \
$Opt{'help'},
53 'sumfile|s=s' => \
$Opt{'sumfile'},
54 'text|t=s' => \
$Opt{'text'},
55 'verbose|v+' => \
$Opt{'verbose'},
56 'version' => \
$Opt{'version'},
58 ) || die("$progname: Option error. Use -h for help.\n");
60 $Opt{'debug'} && ($Debug = 1);
61 $Opt{'help'} && usage
(0);
62 if ($Opt{'version'}) {
67 my $text = $Opt{'text'};
68 length($text) || die("$progname: Filename text not specified (-t option)\n");
71 $Opt{'after'} && $Opt{'before'} && die("$progname: Cannot combine --after and --before\n");
76 (-f
$_ || -l
$_) || die("$progname: $_: File not found, aborting. No files renamed.\n");
77 valid_filename
($_) || die("$progname: $_: Invalid file name, aborting. No files renamed.\n");
80 # Better safe than sorry, check that no files with the same name already
81 # exist before renaming.
82 for my $curr (@files) {
84 $new = new_name
($text, $new);
85 -e
$new && die("$progname: $new: File already exists, aborting. No files renamed.\n");
88 my ($use_sumfile, $sumfp, $sumdata);
90 if (-f
$Opt{'sumfile'}) {
92 open($sumfp, '+<', $Opt{'sumfile'})
93 or die("$progname: $Opt{'sumfile'}: Cannot open file for read+write: $!\n");
94 flock($sumfp, LOCK_EX
)
95 or die("$progname: $Opt{'sumfile'}: Cannot flock(): $!\n");
96 $sumdata = join('', <$sumfp>);
102 for my $curr (@files) {
104 $new = new_name
($text, $new);
105 $sumdata =~ s/$curr/$new/;
106 -e
$new || rename($curr, $new) || die("$progname: $curr: Cannot rename file to '$new': $!\n");
107 $Opt{'verbose'} && printf("%s renamed to %s\n", $curr, $new);
112 or die("$progname: $Opt{'sumfile'}: Cannot seek(): $!\n");
114 or warn("$progname: $Opt{'sumfile'}: Cannot truncate file: $!\n");
116 print($sumfp $sumdata);
117 $use_sumfile && close($sumfp);
121 # Check that source filenames are formatted properly {{{
122 my $filename = shift;
123 # Examples of valid file names:
124 # 20110313T231244Z.zoom0084.m4a
125 # 20110405T223726Z.zo010013.mov
126 # I.e. standard Zoom file names processed by datefn(1)
127 return($filename =~ /^
145 # Return new file name {{{
146 my ($text, $filename) = @_;
148 $filename =~ s/^(.*?Z)\.(.*?)\.([^\.]*?)$/$1.$2.$text.$3/;
149 } elsif ($Opt{'before'}) {
150 $filename =~ s/^(.*?Z)\.(.*?)\.([^\.]*?)$/$1.$text.$2.$3/;
152 $filename =~ s/^(.*?Z)\.(.*?)\.([^\.]*?)$/$1.$text.$3/;
159 # Print program version {{{
160 print("$progname v$VERSION\n");
166 # Send the help message to stdout {{{
169 if ($Opt{'verbose'}) {
175 Rename files created by the Zoom Q3HD to something useful.
177 Usage: $progname -t "Text for file names" [options] [files [...]]
182 Place text after existing text.
184 Place text before existing text.
188 Specify name of file containing file checksums.
189 Default: '$Std{'sumfile'}'.
191 Insert X into file name.
193 Increase level of verbosity. Can be repeated.
195 Print version information.
197 Print debugging messages.
205 # Print a status message to stderr based on verbosity level {{{
206 my ($verbose_level, $Txt) = @_;
208 if ($Opt{'verbose'} >= $verbose_level) {
209 print(STDERR
"$progname: $Txt\n");
216 # Print a debugging message {{{
218 my @call_info = caller;
219 chomp(my $Txt = shift);
220 my $File = $call_info[1];
222 $File =~ s
#^.*/(.*?)$#$1#;
223 print(STDERR
"$File:$call_info[2] $$ $Txt\n");
230 # Plain Old Documentation (POD) {{{
240 [options] [file [files [...]]]
250 =item B<-h>, B<--help>
252 Print a brief help summary.
254 =item B<-v>, B<--verbose>
256 Increase level of verbosity. Can be repeated.
260 Print version information.
264 Print debugging messages.
274 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
278 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
279 This is free software; see the file F<COPYING> for legalese stuff.
283 This program is free software: you can redistribute it and/or modify it
284 under the terms of the GNU General Public License as published by the
285 Free Software Foundation, either version 2 of the License, or (at your
286 option) any later version.
288 This program is distributed in the hope that it will be useful, but
289 WITHOUT ANY WARRANTY; without even the implied warranty of
290 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
291 See the GNU General Public License for more details.
293 You should have received a copy of the GNU General Public License along
295 If not, see L<http://www.gnu.org/licenses/>.
303 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :