2 # anime_rename: A script to rename arbitrary anime chapters (it can rename any
3 # kind of files of course, but that is the reason why i code it)
4 # by Abel `00z' Camarillo
5 # Released under BSD 3-clause license.
7 # See README for documentation.
14 $options{'-h --help'}= "Shows help and exit.";
15 $options{'-p --prefix'}="Sets the prefix of each file.";
16 $options{'-d --debug'}="Show debug information.";
17 $options{'-s --simulate'}="Do nothing just print what it will do (implies -d)";
19 = "Number of digits that have the representative number.Default: 2";
24 my $string = join '', @_;
25 print "$string" if $debug_eval;
26 eval "$string" unless $simulate;
32 my ($option, $description, $format_top, $format);
34 $format_top="format STDERR_TOP =\n"
36 join(' | ', map {s/^[[:space:]]*([^[:space:]]+).*$/$1/g;$_} sort keys %options)
37 . " <files to rename>"."\n"
42 $format = "format STDERR =\n"
43 . "~~^". '<' x 15 . " ^" . '<' x 65 . "\n"
44 . '$option, $description'. "\n"
49 write STDERR while ( ($option, $description ) = each %options);
53 #####################################################################
55 #####################################################################
57 our ($debug_eval, $simulate);
59 my ($prefix, $digits)=('',2);
61 GetOptions ( "help|h" => \&show_help
62 , 'prefix|p=s'=>\$prefix
63 , 'debug|d' => \$debug_eval
64 , 'simulate|s' => sub{ ($simulate,$debug_eval)=(1,1)}
65 , 'num|n:2' => \$digits
70 print STDERR "$0 needs an argument!\n"
71 . "$0 --help for usage\n"
77 die "ERROR: I need a -p option (see $0 -h for usage)\n" unless $prefix;
79 foreach my $file (@ARGV)
81 my ($basedir, $numeric_id, $extension) =
82 ( $file =~ m|^(.*/)?[^[:digit:]]*\W?([[:digit:]]{$digits})\W?.*\.(.*)$|);
84 $basedir = './' unless $basedir;
85 $numeric_id = '' unless $numeric_id;
86 $extension = '' unless $extension;
88 debug_eval "rename '$file', '$basedir${prefix}_${numeric_id}.$extension' \n"
89 if -f $file and $basedir and $numeric_id or $extension ;