packaging-add-macos-bits-20020826
[openafs.git] / src / packaging / MacOS / csrvdbmerge.pl
1 #!/usr/bin/perl
2 # CellServDB merging script
3 # only tested on darwin systems
4
5 use File::Copy;
6 use IO::File;
7 use Fcntl;
8 use strict;
9
10
11 sub doit {
12   my ($cur,$in,$last,$new);
13   my ($line, $oline, $cell, $pos, $which);
14   my %cellstat;
15
16   $cur=new IO::File '<CellServDB';
17   $last=new IO::File '<CellServDB.master.last';
18   
19   while (defined($line=<$cur>)) {
20     if ($line =~ /^>([-a-zA-Z0-9\._]+)\s/) {
21       if ($cell) {
22         $oline=<$last>;
23         if ($oline && $oline !~ /^>/) { # fewer servers in user's file than master
24           $cellstat{$cell} = 1;
25         }
26       }
27       $cell=$1;
28       $cellstat{$cell}=2; 
29       # start at the beginning of the old file, and find $cell
30       $last->seek(0,SEEK_SET);
31       while (<$last>) {
32         if (/>$cell\s/) { # note that we don't compare the cell comments
33           $cellstat{$cell}=0; 
34           last;
35         }
36       }
37       next;
38     }
39     if (! $cell) {
40       die "First CellServDB line isn't a cell\n";
41     }
42     next if ($cellstat{$cell} == 2); # cell only in local CellServDB
43     next if ($cellstat{$cell} == 1); # already found a local change
44     $oline=<$last>;
45     if ($oline =~ /^>/) { # more servers in user's file than master
46       $last->setpos($pos);
47       $cellstat{$cell} = 1;
48       next;
49     }
50     next if ($line eq $oline);
51     $cellstat{$cell} = 1;
52   }
53   close($last);
54   $cur->seek(0,SEEK_SET);
55   $cur=new IO::File '<CellServDB' or die "No CellServDB: $!\n";
56   $in=new IO::File '<CellServDB.master' or die "No CellServDB.master: $!\n";
57   $new=new IO::File '>CellServDB.NEW' or die "Cannot create output CellServDB: $!\n";
58   while (defined($line=<$cur>)) {
59     if ($line =~ /^>([-a-zA-Z0-9\._]+)\s/) {
60       $cell=$1;
61       $which=$cellstat{$cell};
62       unless ($which) {
63         $in->seek(0,SEEK_SET);
64         while (<$in>) {
65           if (/>$cell\s/) {
66             last;
67           }
68         }
69         if (defined($_)) {
70           print $new $_;
71           while (defined($line=<$in>) && $line !~ /^>/) {
72             print $new $line;
73           }
74         } 
75       }
76     }
77     if (! $cell) {
78       die "First CellServDB line isn't a cell\n";
79     }
80     if ($which) {
81       print $new $line;
82     }
83   }
84   close($in);
85   close($cur);
86   close($new);
87   rename('CellServDB.NEW', 'CellServDB');
88   copy('CellServDB.master', 'CellServDB.master.last');
89 }
90
91 doit;