1.12.39
[gnumeric.git] / tools / grand-rename
blobb067250d8ff146b8fdd2694a53d4e3168514963b
1 #!/usr/bin/perl -w
2 # -----------------------------------------------------------------------------
4 use strict;
6 die "$0: must be run from top-level directory.\n"
7 unless (-r "configure.ac" &&
8 -r 'ChangeLog' &&
9 -r 'po/POTFILES.in');
11 my $test = 1;
12 my $doit = 1;
14 while (@ARGV > 0) {
15 if ($ARGV[0] =~ /^--doit/) {
16 # Nothing -- this is now the default
17 } elsif ($ARGV[0] =~ /^--test/) {
18 $doit = 0;
19 } else {
20 last;
25 my $oldname = $ARGV[0];
26 my $newname = $ARGV[1];
27 if (defined ($oldname) && !defined ($newname)) {
28 $newname = $oldname;
29 if ($oldname =~ /^_?[A-Z].*[a-z]/) {
30 $newname =~ s/(_*)/$1Gnm/;
31 } elsif ($oldname =~ /^[a-z0-9_]+$/) {
32 $newname =~ s/(_*)/$1gnm_/;
33 } elsif ($oldname =~ /^[A-Z0-9_]+$/) {
34 $newname =~ s/(_*)/$1GNM_/;
37 die "Usage $0 oldname newname\n" unless $oldname && $newname;
38 die "$0: oldname and newname must be different\n" if $oldname eq $newname;
40 my @files = &find_files ();
42 if ($test) {
43 &do_files (\@files, 1);
45 if ($doit) {
46 print STDERR "Renaming $oldname to $newname\n";
47 &do_files (\@files, 0);
50 # -----------------------------------------------------------------------------
52 sub find_files {
53 my @files = ();
55 my %generated =
56 ('stamp.h' => 1,
57 'gnumeric-config.h' => 1,
58 'src/parser.c' => 1,
59 'src/dialogs/embedded-ui.c' => 1,
62 local (*FIL);
63 open (*FIL, "find . '(' -name '*.[chy]' -type f -print ')' -o " .
64 "'(' -type d '(' -name intl -o -name macros -o -name .git -o -name win32 ')' -prune ')' |") or
65 die "$0: Cannot run find command.\n";
66 while (<FIL>) {
67 chomp;
68 s|^\./||;
70 next if $generated{$_};
72 push @files, $_;
74 close (*FIL);
76 return @files;
79 # -----------------------------------------------------------------------------
81 sub do_files {
82 my ($pfiles,$test) = @_;
84 foreach my $filename (@$pfiles) {
85 local (*SRC);
86 local (*DST);
88 # print STDERR "Scanning $filename...\n";
89 my $newfilename = "$filename.new";
91 open (*SRC, "<$filename") || die "$0: Cannot read $filename: $!\n";
92 if (!$test) {
93 open (*DST, ">$newfilename") || die "$0: Cannot write $newfilename: $!\n";
96 my $didit = 0;
98 if ($test) {
99 my $lineno = 1;
100 foreach my $line (<SRC>) {
101 my $linecopy = $line;
102 $line =~ s/\b$oldname\b/$newname/g;
103 $line =~ s/\b$newname\b/$oldname/g;
104 if ($line ne $linecopy) {
105 chomp ($linecopy);
106 die "$0: Rename clash in $filename, line $lineno: $linecopy <-> $line\n";
108 $lineno++;
110 } else {
111 foreach my $line (<SRC>) {
112 $didit = 1 if $line =~ s/\b$oldname\b/$newname/g;
113 print DST $line;
116 close (*SRC);
118 if (!$test) {
119 close (*DST);
120 if ($didit) {
121 rename $newfilename, $filename;
122 } else {
123 unlink $newfilename;
129 # -----------------------------------------------------------------------------