# CFG.pm
#
# Handle configs given from Perl, environment and call params
#
# Ralf Peine, 2004-05-26 Tue


package CFG;

use strict;

use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %CFG $envVarPattern);

use Exporter;

@ISA         = qw(Exporter);
@EXPORT      = qw(%CFG);
@EXPORT_OK   = qw();
%EXPORT_TAGS = (Variables  => [qw(%CFG)],
		);

use Log;

sub setEnvVarPattern {
    $envVarPattern = $_[0];
}

sub parseArgs {
}

sub addArgs { # arg: variable=value

    my $err = 0;
    my $str;
    my $var;
    my $val;

    while ($str = shift) {
	if ($str =~ /^(\w+)=(.*)/) {
	    $var = $1;
	    $val = $2;
	    $CFG{$var} = $val;
	    # LOG ("#+ ARG $var = '$CFG{$var}'\n");
	} else {
	    print "\nwrong syntax for arg data: '$str'\n";
	    print "correct: 'a1=123456' or 'a2=abcdefg'\n\n";
	    $err++;
	}
    }
    $err = '' if ($err == 0);
    return $err;
}

sub addEnv {

    my $var;
    my $venv;

    foreach $venv (keys (%ENV)) {
	if ($venv =~ /$envVarPattern(.*)/) {
	    $var = $1;
	    $CFG{$var} = $ENV{$venv};
	}
    }
    return;
}

sub sprintAll {

    my $var;
    my $val;
    my $erg = '';

    foreach $var (sort(keys (%CFG))) {
	$val = $CFG{$var};
	$val = '' unless $val;
	$erg .= "#- '$var'='$val'\n";
    }

    return $erg;
}

1;
