1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | # use diagnostics; |
---|
6 | |
---|
7 | # -------------------------------------------------------------------------------- |
---|
8 | |
---|
9 | my $ARBHOME = $ENV{ARBHOME}; |
---|
10 | if (not defined $ARBHOME) { die "Environmentvariable ARBHOME has be defined"; } |
---|
11 | if (not -d $ARBHOME) { die "ARBHOME ('$ARBHOME') does not point to a valid directory"; } |
---|
12 | |
---|
13 | # -------------------------------------------------------------------------------- |
---|
14 | |
---|
15 | my %allowDup = ( 'MACH' => 1 ); |
---|
16 | |
---|
17 | my %old = (); # key = var, value = line |
---|
18 | |
---|
19 | # -------------------------------------------------------------------------------- |
---|
20 | |
---|
21 | sub analyze_line($) { |
---|
22 | my ($line) = @_; |
---|
23 | my ($commented, $var, $value,$comment) = ( 0, undef, undef ); |
---|
24 | |
---|
25 | my $rest; |
---|
26 | |
---|
27 | if ($line =~ /^\s*([\#]*)\s*([A-Z][A-Z_0-9]+)\s*\:\=\s*([a-zA-Z_0-9]+)\s*/o) { |
---|
28 | ($var,$value) = ($2,$3); |
---|
29 | if ($1 eq '#') { $commented = 1; } |
---|
30 | $rest = $'; |
---|
31 | } |
---|
32 | else { |
---|
33 | $rest = $line; |
---|
34 | } |
---|
35 | |
---|
36 | if ($rest =~ /\s*\#\s*/o) { |
---|
37 | $comment = $'; |
---|
38 | } |
---|
39 | |
---|
40 | if (0 and defined $var) { |
---|
41 | print "----------------------------------------\n"; |
---|
42 | print "line ='$line'\n"; |
---|
43 | print "commented='$commented'\n"; |
---|
44 | print "var ='$var'\n"; |
---|
45 | print "value ='$value'\n"; |
---|
46 | if (defined $comment) { print "comment ='$comment'\n"; } |
---|
47 | } |
---|
48 | |
---|
49 | return ($commented, $var, $value,$comment); |
---|
50 | } |
---|
51 | |
---|
52 | sub loadOldConfig($) { |
---|
53 | my ($cfg) = @_; |
---|
54 | |
---|
55 | open(CFG,'<'.$cfg) || die "can't read '$cfg' (Reason: $!)"; |
---|
56 | foreach my $line (<CFG>) { |
---|
57 | chomp($line); |
---|
58 | my ($commented, $var, $value,$comment) = analyze_line($line); |
---|
59 | if (defined $var) { |
---|
60 | if (defined $allowDup{$var}) { |
---|
61 | my $dupId = $var.'#'.$value; |
---|
62 | if (defined $old{$dupId}) { die "Duplicated entry for '$var=$value' in '$cfg'"; } |
---|
63 | $old{$dupId} = $line; |
---|
64 | } |
---|
65 | else { |
---|
66 | if (defined $old{$var}) { die "Duplicated entry for '$var' in '$cfg'"; } |
---|
67 | $old{$var} = $line; |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | close(CFG); |
---|
72 | } |
---|
73 | |
---|
74 | sub gen_line($$$$) { |
---|
75 | my ($commented, $var, $value, $comment) = @_; |
---|
76 | my $line; |
---|
77 | if (defined $var) { |
---|
78 | defined $value || die "var without value"; |
---|
79 | $line = ($commented==1 ? "# ": "").$var.' := '.$value; |
---|
80 | if (defined $comment) { |
---|
81 | $line .= '# '.$comment; |
---|
82 | } |
---|
83 | } |
---|
84 | else { |
---|
85 | if (defined $comment) { |
---|
86 | $line = '# '.$comment; |
---|
87 | } |
---|
88 | else { |
---|
89 | $line = ''; |
---|
90 | } |
---|
91 | } |
---|
92 | return $line; |
---|
93 | } |
---|
94 | |
---|
95 | sub updateConfig($$) { |
---|
96 | my ($src,$dest) = @_; |
---|
97 | |
---|
98 | # die "test"; |
---|
99 | |
---|
100 | open(IN,'<'.$src) || die "can't read '$src' (Reason: $!)"; |
---|
101 | open(OUT,'>'.$dest) || die "can't write '$dest' (Reason: $!)"; |
---|
102 | |
---|
103 | foreach my $line (<IN>) { |
---|
104 | chomp($line); |
---|
105 | my ($commented, $var, $value, $comment) = analyze_line($line); |
---|
106 | |
---|
107 | if ($var) { |
---|
108 | my $oline = undef; |
---|
109 | if (defined $allowDup{$var}) { |
---|
110 | my $dupId = $var.'#'.$value; |
---|
111 | $oline = $old{$dupId}; |
---|
112 | } |
---|
113 | else { |
---|
114 | $oline = $old{$var}; |
---|
115 | } |
---|
116 | |
---|
117 | if (defined $oline) { |
---|
118 | my ($commented_old, $var_old, $value_old, $comment_old) = analyze_line($oline); |
---|
119 | |
---|
120 | print OUT gen_line($commented_old, $var_old, $value_old, $comment); # old settings, new comment |
---|
121 | } |
---|
122 | else { |
---|
123 | print OUT gen_line($commented, $var, $value, $comment); # line was not defined in old cfg |
---|
124 | } |
---|
125 | } |
---|
126 | else { |
---|
127 | print OUT gen_line($commented, $var, $value, $comment); # non-var line |
---|
128 | } |
---|
129 | print OUT "\n"; |
---|
130 | } |
---|
131 | |
---|
132 | close(OUT); |
---|
133 | close(IN); |
---|
134 | } |
---|
135 | |
---|
136 | sub main() { |
---|
137 | my $dest = $ARBHOME.'/config.makefile'; |
---|
138 | my $src = $dest.'.template'; |
---|
139 | |
---|
140 | if (not -f $dest) { |
---|
141 | my $cmd = "cp -p '$src' '$dest'"; |
---|
142 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
143 | } |
---|
144 | else { |
---|
145 | loadOldConfig($dest); |
---|
146 | |
---|
147 | my $backup = $dest.'.bak'; |
---|
148 | my $cmd = "cp -p '$dest' '$backup'"; |
---|
149 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
150 | |
---|
151 | my $new = $dest.'.new'; |
---|
152 | updateConfig($src,$new); |
---|
153 | |
---|
154 | $cmd = "cp -p '$new' '$dest'"; |
---|
155 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
156 | |
---|
157 | unlink($new); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | main(); |
---|