-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
lowercase
executable file
·160 lines (145 loc) · 3.89 KB
/
lowercase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/perl
#
# Copyright (c) 2019 Martin Storsjo
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
require File::Spec::Unix;
my $do_symlink = 0;
my $map_files = 0;
my $map_paths = 0;
my %mapping;
sub do_rename($$$) {
my $src = shift;
my $destdir = shift;
my $destname = shift;
my $dest = "$destdir/$destname";
if ($do_symlink) {
# Ignore errors, which occur on case insensitive file systems
symlink(File::Spec::Unix->abs2rel($src, $destdir), "$dest");
} else {
rename("$src", "$dest") || die("Rename: $!\n");
}
}
sub remapName($) {
my $path = shift;
$path =~ s/\/$//; # Trim the trailing slash, for directories
my $name = $path;
$name =~ s/.*\///;
$name = lc($name);
if ($map_paths) {
if (exists $mapping{lc($path)}) { return $mapping{lc($path)}; }
} elsif ($map_files) {
if (exists $mapping{$name}) { return $mapping{$name}; }
}
return $name;
}
sub mergedir($$) {
my $src = shift;
my $dest = shift;
opendir(IN, $src) || die("$src: $!\n");
my @dir = readdir IN;
closedir IN;
foreach my $i (@dir) {
next if($i eq ".");
next if($i eq "..");
if(-d "$src/$i") {
if(-e "$dest/$i") {
mergedir("$src/$i", "$dest/$i");
} else {
do_rename("$src/$i", $dest, $i);
}
} else {
do_rename("$src/$i", $dest, $i);
}
}
rmdir($src) if (!$do_symlink);
return 1;
}
sub dodir($$) {
my $dir = shift;
my $relpath = shift;
opendir(IN, $dir) || die("$dir: $!\n");
my @dir = readdir IN;
closedir IN;
foreach my $i (@dir) {
next if($i eq ".");
next if($i eq "..");
my $relname = "$relpath$i";
if(-d "$dir/$i") {
dodir("$dir/$i", "$relname/");
} else {
my $new = remapName($relname);
if($i ne $new) {
#print "FILE: $i -> $new\n";
do_rename("$dir/$i", $dir, $new);
}
}
}
my @dirs = split('/', $dir);
my $ldir = pop(@dirs);
my $newname = remapName($relpath);
$newname = lc($ldir) if ($relpath eq "");
if($ldir ne $newname) {
my $ndir = join('/', @dirs);
my $parent = $ndir;
if($ndir) {
$ndir .= "/" . $newname;
} else {
$parent = ".";
$ndir = $newname;
}
#print "DIR: $dir -> $ndir\n";
if(-d $ndir) {
mergedir($dir, $ndir) || die("Merge: $!\n");
} else {
do_rename("$dir", $parent, $newname);
}
}
}
sub readMapping($) {
my $file = shift;
open FILE, $file;
foreach my $line (<FILE>) {
chomp $line;
my $name = $line;
$name =~ s/.*\///;
next if (exists $mapping{lc($line)});
$mapping{lc($line)} = $name;
}
close FILE;
}
my @paths;
for (my $i = 0; $i < @ARGV; $i++) {
my $arg = $ARGV[$i];
if ($arg eq "-symlink") {
$do_symlink = 1;
} elsif ($arg eq "-map_paths") {
if ($i + 1 < @ARGV) { readMapping($ARGV[$i+1]); }
$map_paths = 1;
$i += 1;
} elsif ($arg eq "-map_files") {
if ($i + 1 < @ARGV) { readMapping($ARGV[$i+1]); }
$map_files = 1;
$i += 1;
} elsif ($arg eq "-map_winsdk") {
$map_paths = 1;
# Keep the GL header directory in upper case, as that's the canonical
# cross platform spelling of that directory.
$mapping{lc("GL")} = "GL";
} else {
push @paths, $arg;
}
}
die("Usage: lowercase dir\n") if(@paths != 1);
dodir($paths[0], "");