updated git and svn scripts
[xrzperl.git] / htmlconv_filecase
blobc84fd45569c10645fa02837145a7128a0460a452
1 #!/usr/bin/perl -w
2 ###APPNAME: htmlconv_filecase
3 ###APPAUTHOR: duel
4 ###APPDATE: 2009-04-17 01:08:39
5 ###APPVER: 0.1
6 ###APPDESC: htmlconv_filecase
7 ###APPUSAGE: [-u] directory
8 ###APPEXAMPLE: htmlconv_filecase chm_directory
9 ###APPOPTION: -u:Convert to upper case
10 use strict;
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
14 #OR nothing will work
15 use lib $ENV{XR_PERL_MODULE_DIR};
17 use MyPlace::Script::Usage qw/help_required help_even_empty/;
18 #exit 0 if(help_required($0,@ARGV));
19 exit 0 if(help_even_empty($0,@ARGV));
21 my $UPPER;
22 my @FOLDERS;
24 foreach(@ARGV) {
25 if($_ eq "-u") {
26 $UPPER=1;
28 elsif(-d $_) {
29 push @FOLDERS,$_;
31 else {
32 die("\"$_\" isn't a valid option,neither a directory\n");
35 push @FOLDERS,"." unless(@FOLDERS);
37 sub slurp {
38 my $file = shift;
39 local ($/,*FH);
40 open FH,"<",$file;
41 my $result = <FH>;
42 close FH;
43 return $result;
46 sub do_case {
47 my $old = shift;
48 my $new = $UPPER ? uc($old) : lc($old);
49 unless($new eq $old) {
50 print STDERR "Converting $old -> $new ... ";
51 rename $old,$new or die("$!\n");
52 print STDERR "\n";
54 return $new;
56 use File::Spec::Functions;
57 sub dirtree {
58 my @result;
59 my $dir = &do_case(shift);
60 print STDERR "dirtree(): processing \"$dir\"\n";
61 opendir DIR,$dir or die("$!\n");
62 my @subdir;
63 if($dir eq ".") {
64 foreach(readdir(DIR)) {
65 next if($_ eq '.' or $_ eq '..');
66 $_ = do_case($_);
67 push @result,$_;
68 push @subdir,$_ if(-d $_);
71 else {
72 foreach(readdir(DIR)) {
73 next if($_ eq '.' or $_ eq '..');
74 my $path = do_case(catfile($dir,$_));
75 push @result,$path;
76 push @subdir,$path if(-d $path);
79 close DIR;
80 push @result,&dirtree($_) foreach(@subdir);
81 return @result;
84 my $FILE_EXP = '\.\w{1,8}';
85 my $EXP = qr/((?:=\s*[^'"\s\n\r]+$FILE_EXP[\s\>\<]|"[^"\n\r]+$FILE_EXP"|'[^'\n\r]+$FILE_EXP'))/i;
86 my $REPLACE = $UPPER ? '\U$1\E' : '\L$1\E';
88 #test exp
89 #while(<STDIN>) {
90 # s/$EXP/\L$1\E/g;
91 # print $_;
93 #exit 0;
96 foreach(@FOLDERS) {
97 print STDERR "Processing $_ ...\n";
98 my @tree = dirtree($_);
99 unless(@tree) {
100 print STDERR "\tNothing to do\n";
101 next;
103 # my @exp = build_exp(\@tree);
104 # use Data::Dumper;die(Dumper(\@tree));
105 # use Data::Dumper;die(Dumper(\@exp));
106 foreach(@tree) {
107 if(/\.(?:html?|css|js)$/ and -f $_) {
108 print STDERR "Processing $_ ...\n";
109 my $text = slurp($_);
110 my $newtext = $text;
111 $UPPER ? $newtext =~ s/$EXP/\U$1\E/go : $newtext =~ s/$EXP/\L$1\E/go;
112 unless($newtext eq $text) {
113 print STDERR "Writing $_ ... ";
114 open FO,">:raw",$_ or die("$!\n");
115 print FO $newtext;
116 close FO;
117 print STDERR "\n";