summaryrefslogtreecommitdiff
path: root/rename-macro.pl
blob: 487857e360c59efdf0719a2d0ad81b8da785ef3b (plain)
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
#! /usr/bin/env perl
# rename-macro
# Rename an autoconf-archive macro, obsoleting the old name
# Usage: rename-macro OLD_PREFIX NEW_PREFIX FROM TO
#   If there is no prefix to remove, make the prefix some
#   string that does not occur in the files being modified.


use strict;
use warnings;

my ($old_prefix, $new_prefix, $old_file, $new_file) = @ARGV;

# Extract names from file names
my $old_name = $old_file;
$old_name =~ s/\..*$//;
my $uc_old_name = uc($old_name);
$old_name = lc($old_name);
my $new_name = $new_file;
$new_name =~ s/\..*$//;
my $uc_new_name = uc($new_name);
$new_name = lc($new_name);

# Read file
open IN, $old_file or die "could not read `$old_file'\n";
my $text = do { local $/, <IN> };

# Make new macro
my $new_text = $text;
$new_text =~ s/$old_name/$new_name/g; # Change name (lower case)
$new_text =~ s/$uc_old_name/uc($new_name)/ge; # Change name (upper case)
$new_text =~ s/$old_prefix/$new_prefix/g; # Change other references to prefix (upper case)
my $lc_old_prefix = lc($old_prefix);
$new_text =~ s/$lc_old_prefix/lc($new_prefix)/ge; # Change other references to prefix (lower case)
open OUTFILE, ">$new_file" or die "could not read `$new_file'";
print OUTFILE $new_text;
system "git add $new_file";

# Obsolete the old macro
my $insertion = <<END;
# OBSOLETE MACRO
#
#   Renamed to $uc_new_name
#
END
chomp $insertion;
my $old_text = $text;
$old_text =~ s/^\# SYNOPSIS/"$insertion\n\# SYNOPSIS"/me;
open OUTFILE, ">$old_file" or die "could not write `$old_file'";
print OUTFILE $old_text;