-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixupguiddata
executable file
·68 lines (56 loc) · 1.54 KB
/
fixupguiddata
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
#!/usr/bin/perl -w
use strict;
use utf8;
use feature 'signatures';
no warnings 'experimental::signatures';
use open IO => ":encoding(utf-8)";
use open ':std';
use POSIX;
use File::Slurp;
use Data::Dumper;
use Getopt::Long::Descriptive;
use JSON;
my $guidmatch=qr/^(.*)([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})(.*)$/;
sub renameentries($dir, $opt, $guidmap) {
my $dh;
opendir($dh, $dir);
my @entries=readdir($dh);
closedir($dh);
foreach my $entry ( @entries ) {
if (my ($pre, $match, $post) = ($entry =~ $guidmatch)) {
if (defined($guidmap->{$match})) {
my $new=$pre . $guidmap->{$match}->{ObjectGUID} . $post;
if ($opt->rename) {
rename(sprintf("%s/%s", $dir, $entry), sprintf("%s/%s", $dir, $new));
}
printf("Rename\nFrom: %s/%s\nTo: %s/%s\n\n",
$dir, $entry, $dir, $new);
}
}
}
}
my ($opt, $usage) = describe_options(
'$Script %o <some-arg>',
[ 'datastore|d=s', "Datastore dir", { required => 1 } ],
[ 'guidmap|m=s', "Guidmap.json", { required => 1 } ],
[ 'rename', "Rename", { default => 0 } ],
[],
[ 'help', "print usage message and exit", { shortcircuit => 1 } ],
);
sub scanandrename($dir, $opt, $guidmap, $lvl=0) {
renameentries($dir, $opt, $guidmap);
# Recurse down
my $dh;
opendir($dh, $dir);
my @dirs=map {
$dir . "/" . $_;
} grep {
!/^\./ && -d "$dir/$_"
} readdir($dh);
closedir($dh);
foreach my $dir ( @dirs ) {
scanandrename($dir, $opt, $guidmap, $lvl+1);
}
}
my $guidmap=from_json(read_file($opt->guidmap));
scanandrename($opt->datastore, $opt, $guidmap);