Sindbad~EG File Manager

Current Path : /usr/src/nrpe-2.15/
Upload File :
Current File : //usr/src/nrpe-2.15/subst

#!/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" => '/usr/local/nagios'},
	"exec_prefix" => { "value" => '${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";

}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists