Initial Commit
[packages] / xemacs-packages / bbdb / utils / bbdb-areacode-split.pl
1 #!/usr/bin/perl
2
3 # Looks for phone numbers in your .bbdb with a particular area code
4 # and one of a set of exchanges and changes the area code.  The old
5 # and new area codes are specified on the command line, as is the
6 # location of a file that contains the exchanges that are being
7 # changed.  (The format of that file is very loose.  Every three digit
8 # sequence will be used.)
9
10 # Seth Golub <seth@cs.wustl.edu>
11 # 15 Aug 1997
12
13 sub Usage
14 {
15     $0 =~ s@.*/@@;
16     die "Usage: \n  $0 <old-code> <new-code> <exchanges-file> [bbdb]\n";
17 }
18
19 $old_area_code = shift || Usage();
20 $new_area_code = shift || Usage();
21 $exchange_list_file = shift || Usage();
22
23 $bbdb_file = $ENV{'BBDB'} || shift || $ENV{'HOME'} . '/.bbdb';
24 $bbdb_dir = `dirname $bbdb_file`;  chomp $bbdb_dir;
25 $tmp_file = "$bbdb_dir/bbdb.new-$$";
26
27 open( LIST, "<$exchange_list_file" ) 
28     || die "Failed to open $exchange_list_file\n";
29
30 while (<LIST>)
31 {
32     while ( /(\d\d\d)/g )
33     {
34         push( @exchanges, $1 );
35     }
36 }
37
38 close( LIST );
39
40 $exchanges = join( '|', @exchanges );
41
42 open( BBDB_IN, "<$bbdb_file" ) || die "Failed to open $bbdb_file\n";
43 open( BBDB_OUT, ">$tmp_file" ) || die "Failed to open $tmp_file\n";
44
45 while (<BBDB_IN>)
46 {
47     next unless /^\[/;
48     s/(\[\".*?\") $old_area_code (($exchanges) \d+ \d+\])/$1 $new_area_code $2/og;
49 } continue {
50     print BBDB_OUT;
51 }
52
53 close( BBDB_IN );
54 close( BBDB_OUT );
55
56 unlink( "$bbdb_file.bak" );
57 rename( $bbdb_file, "$bbdb_file.bak" );
58 rename( $tmp_file, $bbdb_file );
59
60 print STDERR "Old bbdb moved to $bbdb_file.bak\n";
61
62 __END__