nagios-nrpe/subst.in

62 lines
1.7 KiB
Plaintext
Raw Normal View History

2016-06-24 12:21:25 +02:00
#!/usr/bin/perl -w
# This script finishes the job started by config.status by replacing the variables
# of the form ${...} which were inserted into the file(s) by config.status.
# Read all files with a single read statement
$/ = undef;
# List of variables to replace
my %configvars = (
"prefix" => { "value" => '@prefix@'},
"exec_prefix" => { "value" => '@exec_prefix@'},
);
sub replace_var {
my $filep = shift;
my $cvp = shift;
my $varname = shift;
return if( $cvp->{ $varname}->{ "replaced"});
if( defined( $cvp->{ $varname}->{ "dependency"})) {
if( !$cvp->{ $cvp->{ $varname}->{ "dependency"}}->{ "replaced"}) {
# If a dependency exists and it is not already replaced, replace it
replace_var( $filep, $cvp, $cvp->{ $varname}->{ "dependency"});
}
}
my $replacement = $cvp->{ $varname}->{ "value"};
$$filep =~ s/\${$varname}/$replacement/g;
$cvp->{ $varname}->{ "replaced"} = 1;
}
# Figure out the dependencies.
foreach my $cv ( keys %configvars ) {
if( $configvars{ $cv}->{ "value"} =~ /\${([^}]+)}/) {
my $dependency = $1;
if( exists( $configvars{ $dependency})) {
$configvars{ $dependency}->{ "dependency"} = $cv;
}
$configvars{ $cv}->{ "replaced"} = 0;
}
}
# Process each file
while ($f = shift @ARGV) {
# Read in the file
open( FILE, $f) || die "Unable to open $f for reading";
my $file = <FILE>;
close( FILE) || die "Unable to close $f after reading";
# Replace each of the variables we know about
foreach $cv ( keys %configvars ) {
replace_var( \$file, \%configvars, $cv);
}
# Write out the replacements
open( FILE, ">$f") || die "Unable to open $f for writing";
print FILE $file;
close( FILE) || die "Unable to close $f after writing";
}