Source

packaging / rpmbuild / SOURCES / dl-generic-0.1.0 / conf.pl

#!/usr/bin/perl -w
#
# This script should be called by the configure shell script.  If you are using GNU autoconf,
# or some other mechanism to configure your tree, just use that, and ignore this script.
# the bootstrap will keep re-creating it, as an essential tool, but you can ignore that,
# and/or never check it in, because it will stay in shared/packaging in the build-tools
# repository.
#
#----------------------------------------------------------------------------
use strict;
use Data::Dumper;

sub Parser($);
sub RawSubst($);
sub SubstHostnameShort();
sub SubstLogDir();
sub SubstPrefix();
sub SubstLocalStateDir();
sub ProcessArgs();
sub ProcessArg($$);
sub CheckArgs();
#----------------------------------------------------------------------------
my %Params = (
	'prefix' => { arg_mandatory => 1, val_mandatory => 1, val_default => '/opt', val_numeric => undef }
);
#----------------------------------------------------------------------------
ProcessArgs();

foreach my $line ( <STDIN> ) {
	$line = Parser($line);
	print $line;
}

exit 0;
#----------------------------------------------------------------------------
sub Parser($)
{
	my $Line = $_[0];
	if ( $Line =~ m/\@(.*)\@/ ) {
		my $variable = $1;
		my $parsedVar = RawSubst($variable);
		if ( defined($parsedVar) ) {
			my $idx = index($Line, '@' . $variable . '@');
			substr($Line, $idx, length($variable)+2, $parsedVar);
		}
	}
	return $Line;
}
#----------------------------------------------------------------------------
sub RawSubst($)
{
	my %substTable = (
		'HOSTNAME_SHORT' => \&SubstHostnameShort,
		'LOG_DIR' => \&SubstLogDir,
		'PREFIX' => \&SubstPrefix,
		'LOCAL_STATE_DIR' => \&SubstLocalStateDir
	);

	my $Variable = $_[0];
	die unless ( defined($Variable) );
	unless ( defined($substTable{$Variable}) ) {
		return ''; # Undefined variable
	}

	return $substTable{$Variable}->(); # Call the substitution function
}
#----------------------------------------------------------------------------
sub SubstHostnameShort()
{
	my $hn = `hostname -s`;
	chomp($hn);
	return $hn;
}
#----------------------------------------------------------------------------
sub SubstLogDir()
{
	return '';
}
#----------------------------------------------------------------------------
sub SubstPrefix()
{
	my $Param = 'prefix';
	if ( $Params{$Param}->{is_set} ) {
		return $Params{$Param}->{value};
	} else {
		return '';
	}
}
#----------------------------------------------------------------------------
sub SubstLocalStateDir()
{
	return '/var';
}
#----------------------------------------------------------------------------
sub ProcessArgs()
{
	foreach my $a ( @ARGV ) {
		if ( index($a, '--') == 0 ) {
			my $argName = substr($a, 2);
			next unless length($argName);

			my @paramParts = split('=', $argName);
			if ( scalar(@paramParts) >= 2 ) {
				ProcessArg($paramParts[0], $paramParts[1]);
			} else {
				ProcessArg($argName, undef);
			}
		}
	}

	CheckArgs();
}
#----------------------------------------------------------------------------
sub ProcessArg($$)
{
	my ( $ArgName, $ArgVal ) = @_;

	#printf("Called with %s equals %s\n", $ArgName, ($ArgVal) ? ($ArgVal) : ('(undef)'));
	#print Dumper \%Params;

	die unless( defined($ArgName) && length($ArgName) );
	unless ( defined($Params{$ArgName}) ) {
		printf(STDERR "Sorry, --%s is not a recognised parameter\n", $ArgName);
		exit 1;
	}

	# Check for mandatory values for arguments
	if ( !defined($ArgVal) && $Params{$ArgName}->{val_mandatory} ) {
		my $optional = ' and the argument must be numeric';
		printf(
                  STDERR "Sorry, --%s requires a value in the form --%s=(value)%s\n",
		  $ArgName, $ArgName, ($Params{$ArgName}->{val_numeric}) ? ($optional) : ('')
		);
		exit 1;
	}

	# Check for values which, if specified must be numeric
	if ( defined($ArgVal) && $Params{$ArgName}->{val_numeric} ) {
		unless ( $ArgVal =~ m/^[-]?\d+$/ ) {
			printf(STDERR "Sorry, for argument --%s=%s, %s must be numeric\n", $ArgName, $ArgName, $ArgVal);
			exit 1;
		}
	}

	$Params{$ArgName}->{value} = $ArgVal;
	$Params{$ArgName}->{is_set} = 1;
	return;
}
#----------------------------------------------------------------------------
sub CheckArgs()
{
	foreach my $a ( keys(%Params) ) {
		unless ( $Params{$a}->{is_set} ) { # For all arguments with no value set...
			if ( $Params{$a}->{arg_mandatory} ) {
				printf(STDERR "--%s is a mandatory argument\n", $a);
				exit 1;
			}
			if ( $Params{$a}->{val_mandatory} ) {
				if ( exists($Params{$a}->{val_default}) ) {
					$Params{$a}->{value} = $Params{$a}->{val_default};
					$Params{$a}->{is_set} = 1;
				} else {
					printf(
					  STDERR "--%s requires an argument (no default available)\n",
					  $a
					);
				}
			}
		}
	}
}
#----------------------------------------------------------------------------