Modified README to reflects COPYING changes.
[anime_rename.git] / anime_rename
blob86cb902758abf5d675a435bda560eb55dc025deb
1 #!/bin/env perl
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.
9 use warnings;
10 use Getopt::Long;
12 our %options;
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)";
18 $options{'-n --num'}
19   = "Number of digits that have the representative number.Default: 2";
21 sub
22 debug_eval
24   my $string = join '', @_;
25   print "$string" if $debug_eval;
26   eval "$string" unless $simulate;
29 sub
30 show_help
32   my ($option, $description, $format_top, $format);
34   $format_top="format STDERR_TOP =\n"
35     . "$0 ". 
36     join(' | ', map {s/^[[:space:]]*([^[:space:]]+).*$/$1/g;$_} sort keys %options) 
37     . " <files to rename>"."\n"
38     . ".\n";
40   eval "$format_top";
42   $format = "format STDERR =\n"
43     . "~~^". '<' x 15 . " ^" . '<' x 65 . "\n"
44     . '$option, $description'. "\n"
45     . ".\n";
47   eval "$format";
49   write STDERR while ( ($option, $description ) = each %options);
50   exit 1
53 #####################################################################
54 # MAIN                                                              #
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
66     );
68 unless (@ARGV)
70   print STDERR "$0 needs an argument!\n"
71     . "$0 --help for usage\n"
72     ;
74   exit 1
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 ;