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-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 "line ='$line'\n"; |
---|
42 | print "commented='$commented'\n"; |
---|
43 | print "var ='$var'\n"; |
---|
44 | print "value ='$value'\n"; |
---|
45 | if (defined $comment) { print "comment ='$comment'\n"; } |
---|
46 | } |
---|
47 | |
---|
48 | return ($commented, $var, $value,$comment); |
---|
49 | } |
---|
50 | |
---|
51 | sub loadOldConfig($) { |
---|
52 | my ($cfg) = @_; |
---|
53 | |
---|
54 | open(CFG,'<'.$cfg) || die "can't read '$cfg' (Reason: $!)"; |
---|
55 | foreach my $line (<CFG>) { |
---|
56 | chomp($line); |
---|
57 | my ($commented, $var, $value,$comment) = analyze_line($line); |
---|
58 | if (defined $var) { |
---|
59 | if (defined $allowDup{$var}) { |
---|
60 | my $dupId = $var.'#'.$value; |
---|
61 | if (defined $old{$dupId}) { die "Duplicated entry for '$var=$value' in '$cfg'"; } |
---|
62 | $old{$dupId} = $line; |
---|
63 | } |
---|
64 | else { |
---|
65 | if (defined $old{$var}) { die "Duplicated entry for '$var' in '$cfg'"; } |
---|
66 | $old{$var} = $line; |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | close(CFG); |
---|
71 | } |
---|
72 | |
---|
73 | sub gen_line($$$$) { |
---|
74 | my ($commented, $var, $value, $comment) = @_; |
---|
75 | my $line; |
---|
76 | if (defined $var) { |
---|
77 | defined $value || die "var without value"; |
---|
78 | $line = ($commented==1 ? "# ": "").$var.' := '.$value; |
---|
79 | if (defined $comment) { |
---|
80 | $line .= '# '.$comment; |
---|
81 | } |
---|
82 | } |
---|
83 | else { |
---|
84 | if (defined $comment) { |
---|
85 | $line = '# '.$comment; |
---|
86 | } |
---|
87 | else { |
---|
88 | $line = ''; |
---|
89 | } |
---|
90 | } |
---|
91 | return $line; |
---|
92 | } |
---|
93 | |
---|
94 | sub updateConfig($$) { |
---|
95 | my ($src,$dest) = @_; |
---|
96 | |
---|
97 | # die "test"; |
---|
98 | |
---|
99 | open(IN,'<'.$src) || die "can't read '$src' (Reason: $!)"; |
---|
100 | open(OUT,'>'.$dest) || die "can't write '$dest' (Reason: $!)"; |
---|
101 | |
---|
102 | foreach my $line (<IN>) { |
---|
103 | chomp($line); |
---|
104 | my ($commented, $var, $value, $comment) = analyze_line($line); |
---|
105 | |
---|
106 | if ($var) { |
---|
107 | my $oline = undef; |
---|
108 | if (defined $allowDup{$var}) { |
---|
109 | my $dupId = $var.'#'.$value; |
---|
110 | $oline = $old{$dupId}; |
---|
111 | } |
---|
112 | else { |
---|
113 | $oline = $old{$var}; |
---|
114 | } |
---|
115 | |
---|
116 | if (defined $oline) { |
---|
117 | my ($commented_old, $var_old, $value_old, $comment_old) = analyze_line($oline); |
---|
118 | |
---|
119 | print OUT gen_line($commented_old, $var_old, $value_old, $comment); # old settings, new comment |
---|
120 | } |
---|
121 | else { |
---|
122 | print OUT gen_line($commented, $var, $value, $comment); # line was not defined in old cfg |
---|
123 | } |
---|
124 | } |
---|
125 | else { |
---|
126 | print OUT gen_line($commented, $var, $value, $comment); # non-var line |
---|
127 | } |
---|
128 | print OUT "\n"; |
---|
129 | } |
---|
130 | |
---|
131 | close(OUT); |
---|
132 | close(IN); |
---|
133 | } |
---|
134 | |
---|
135 | sub main() { |
---|
136 | my $dest = $ARBHOME.'/config.makefile'; |
---|
137 | my $src = $dest.'.template'; |
---|
138 | |
---|
139 | if (not -f $dest) { |
---|
140 | my $cmd = "cp -p '$src' '$dest'"; |
---|
141 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
142 | } |
---|
143 | else { |
---|
144 | loadOldConfig($dest); |
---|
145 | |
---|
146 | my $backup = $dest.'.bak'; |
---|
147 | my $cmd = "cp -p '$dest' '$backup'"; |
---|
148 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
149 | |
---|
150 | my $new = $dest.'.new'; |
---|
151 | updateConfig($src,$new); |
---|
152 | |
---|
153 | $cmd = "cp -p '$new' '$dest'"; |
---|
154 | system($cmd)==0 || die "Failed to execute '$cmd' (result=$?)"; |
---|
155 | |
---|
156 | unlink($new); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | main(); |
---|