updated git and svn scripts
[xrzperl.git] / rename_by_firstline
blob1fe0dcb328659c58ecad044af89d74c039d358a9
1 #!/usr/bin/perl -w
2 # $Id$
3 use strict;
4 require v5.10.0;
5 our $VERSION = 'v0.1';
7 BEGIN
9 my $PROGRAM_DIR = $0;
10 $PROGRAM_DIR =~ s/[^\/\\]+$//;
11 $PROGRAM_DIR = "./" unless($PROGRAM_DIR);
12 unshift @INC,
13 map "$PROGRAM_DIR$_",qw{modules lib ../modules ..lib};
16 my %OPTS;
17 my @OPTIONS = qw/help|h|? version|ver edit-me manual|man/;
19 if(@ARGV)
21 require Getopt::Long;
22 require MyPlace::Usage;
23 Getopt::Long::GetOptions(\%OPTS,@OPTIONS);
24 MyPlace::Usage::Process(\%OPTS,$VERSION);
26 else
28 require MyPlace::Usage;
29 MyPlace::Usage::PrintHelp();
32 die("Invalid Usage\n") unless(@ARGV);
33 foreach my $org_filename (@ARGV)
35 if(-f $org_filename)
37 if(open FI,"<",$org_filename)
39 my $new_basename;
40 while(<FI>)
42 s/^[\s\n\r]+//;
43 s/[\s\n\r]+$//;
44 next unless($_);
45 $new_basename = $_;
46 last;
48 close FI;
49 if($new_basename)
51 my $ext_name = "";
52 my $dirname = $org_filename;
53 if($dirname =~ m/^(.*)\.([^\.]+)$/)
55 $dirname = $1;
56 $ext_name = ".$2";
58 if($dirname =~ /^(.+)\/[^\/]*$/) {
59 $dirname = "$1/";
61 else {
62 $dirname = "";
64 my $new_name = "$dirname$new_basename$ext_name";
65 if($new_name eq $org_filename)
67 print STDERR "$org_filename: New name is the same as orginal name\n";
68 next;
70 else
72 print STDERR "Rename $org_filename to $new_name\n";
73 system("mv","-v","--",$org_filename,$new_name) unless($OPTS{'--test'});
76 else
78 print STDERR "$org_filename MAYBE NOT contains any NON-EMPTY line\n";
79 print STDERR "$org_filename Skipped.\n";
82 else
84 print STDERR "$!\n";
87 else {
88 print STDERR "File not accessible : $org_filename\n";
92 __END__
94 =pod
96 =head1 NAME
98 rename_by_firstline - rename files according to the FIRST NON-EMPTY LINE
100 =head1 SYNOPSIS
102 rename_by_firstline [options] file1 [file2...]
104 =head1 OPTIONS
106 =over 12
108 =item B<--version>
110 Print version infomation.
112 =item B<-h>,B<--help>
114 Print a brief help message and exits.
116 =item B<--manual>,B<--man>
118 View application manual
120 =item B<--edit-me>
122 Invoke 'editor' against the source
124 =item B<--test>
126 Do not perform renaming action
128 =back
130 =head1 DESCRIPTION
132 rename files according to the FIRST NON-EMPTY LINE
134 =head1 CHANGELOG
136 2010-06-08 xiaoranzzz <xiaoranzzz@myplace.hell>
138 * file created.
140 =head1 AUTHOR
142 xiaoranzzz <xiaoranzzz@myplace.hell>
144 =cut