package handle_configs;

# $Id$

use diagnostics;
use strict;

use common;

sub searchstr {
    # Preceed all characters which are special characters in regexps with
    # a backslash, so that the returned string used in a regexp searches
    # a literal occurence of the original string. White space is replaced
    # by "\s+"
    # "quotemeta()" does not serve for this, as it also quotes some regular
    # characters, as the space
    my ($s) = @_;
    $s =~ s!([\\/\(\)\[\]\{\}\|\.\$\@\%\*\?#\+\-])!\\$1!g;
    return $s;
}

sub read_directives {
    # Read one or more occurences of a directive
    my ($lines_ptr, $directive) = @_;

    my $searchdirective = searchstr($directive);
    # do not use if_() below because it slow down printerdrake
    # to the point one can believe in process freeze:
    map { (/^\s*$searchdirective\s+(\S.*)$/ ? chomp_($1) : ()) } @$lines_ptr;
}

sub read_unique_directive {

    # Read a directive, if the directive appears more than once, use
    # the last occurence and remove all the others, if it does not
    # occur, return the default value

    my ($lines_ptr, $directive, $default) = @_;

    if ((my @d = read_directives($lines_ptr, $directive)) > 0) {
	my $value = $d[-1];
	set_directive($lines_ptr, "$directive $value");
	return $value;
    } else {
        return $default;
    }
}

sub insert_directive {

    # Insert a directive only if it is not already there

    my ($lines_ptr, $directive) = @_;

    my $searchdirective = searchstr($directive);
    (/^\s*$searchdirective$/ and return 0) foreach @$lines_ptr;
    push @$lines_ptr, "$directive\n";
    return 1;
}

sub remove_directive {

    # Remove a directive

    my ($lines_ptr, $directive) = @_;

    my $success = 0;
    my $searchdirective = searchstr($directive);
    (/^\s*$searchdirective/ and $_ = "" and $success = 1)
	foreach @$lines_ptr;
    return $success;
}

sub comment_directive {

    # Comment out a directive

    my ($lines_ptr, $directive, $exactmatch) = @_;

    my $success = 0;
    my $searchdirective = searchstr($directive);
    $searchdirective .= ".*" if !$exactmatch;
    (s/^\s*($searchdirective)$/#$1/ and $success = 1)
	foreach @$lines_ptr;
    return $success;
}

sub replace_directive {

    # Replace a directive, if it appears more than once, remove
    # the additional occurences.

    my ($lines_ptr, $olddirective, $newdirective) = @_;

    my $success = 0;
    $newdirective = "$newdirective\n";
    my $searcholddirective = searchstr($olddirective);
    (/^\s*$searcholddirective/ and $_ = $newdirective and 
     $success = 1 and $newdirective = "") foreach @$lines_ptr;
    return $success;
}


sub move_directive_to_version_commented_out {

    # If there is a version of the directive "commentedout" which is
    # commented out, the directive "directive" will be moved in its place.

    my ($lines_ptr, $commentedout, $directive, $exactmatch) = @_;

    my $success = 0;
    my $searchcommentedout = searchstr($commentedout);
    $searchcommentedout .= ".*" if !$exactmatch;
    (/^\s*#$searchcommentedout$/ and 
     $success = 1 and last) foreach @$lines_ptr;
    if ($success) {
	remove_directive($lines_ptr, $directive);
	(s/^\s*#($searchcommentedout)$/$directive/ and 
	 $success = 1 and last) foreach @$lines_ptr;
    }
    return $success;
}

sub set_directive {

    # Set a directive, replace the old definition or a commented definition

    my ($lines_ptr, $directive, $full_line) = @_;

    my $olddirective = $directive;
    if (!$full_line) {
	$olddirective =~ s/^\s*(\S+)\s+.*$/$1/s;
	$olddirective ||= $directive;
    }

    my $success = (replace_directive($lines_ptr, $olddirective,
				     $directive) ||
		   insert_directive($lines_ptr, $directive));
    if ($success) {
	move_directive_to_version_commented_out($lines_ptr, $directive, 
						$directive, 1);
    }
    return $success;
}

sub add_directive {

    # Add a directive, replace a commented definition

    my ($lines_ptr, $directive) = @_;

    my $success = insert_directive($lines_ptr, $directive);
    if ($success) {
	move_directive_to_version_commented_out($lines_ptr, $directive, 
						$directive, 1);
    }
    return $success;
}

1;
ion>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>Mageia Installer and base platform for many utilities</td><td class='sub right'>Thierry Vignaud [tv]</td></tr></table>
<table class='tabs'><tr><td>
<a href='/software/drakx/?h=V1_1_8_8mdk'>summary</a><a href='/software/drakx/refs/?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>refs</a><a href='/software/drakx/log/perl-install/share?h=V1_1_8_8mdk'>log</a><a href='/software/drakx/tree/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>tree</a><a class='active' href='/software/drakx/commit/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>commit</a><a href='/software/drakx/diff/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>diff</a><a href='/software/drakx/stats/perl-install/share?h=V1_1_8_8mdk'>stats</a></td><td class='form'><form class='right' method='get' action='/software/drakx/log/perl-install/share'>
<input type='hidden' name='h' value='V1_1_8_8mdk'/><input type='hidden' name='id' value='3b7011a5f27c1cdc645a5a9734391ab88544ba3c'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/software/drakx/commit/?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>root</a>/<a href='/software/drakx/commit/perl-install?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>perl-install</a>/<a href='/software/drakx/commit/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>share</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='V1_1_8_8mdk'/><input type='hidden' name='id' value='3b7011a5f27c1cdc645a5a9734391ab88544ba3c'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Pablo Saratxaga &lt;pablo@mandriva.com&gt;</td><td class='right'>2000-10-09 17:29:34 +0000</td></tr>
<tr><th>committer</th><td>Pablo Saratxaga &lt;pablo@mandriva.com&gt;</td><td class='right'>2000-10-09 17:29:34 +0000</td></tr>
<tr><th>commit</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>3b7011a5f27c1cdc645a5a9734391ab88544ba3c</a> (<a href='/software/drakx/patch/perl-install/share?id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='sha1'><a href='/software/drakx/tree/?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>8a123394709f99bb58969439feaa78244ad2bfb1</a> /<a href='/software/drakx/tree/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>perl-install/share</a></td></tr>
<tr><th>parent</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/perl-install/share?h=V1_1_8_8mdk&amp;id=c132e5d3fea1b2a45cea44784c0592319d91e0ad'>c132e5d3fea1b2a45cea44784c0592319d91e0ad</a> (<a href='/software/drakx/diff/perl-install/share?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c&amp;id2=c132e5d3fea1b2a45cea44784c0592319d91e0ad'>diff</a>)</td></tr><tr><th>download</th><td colspan='2' class='sha1'><a href='/software/drakx/snapshot/drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar'>drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar</a><br/><a href='/software/drakx/snapshot/drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.gz'>drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.gz</a><br/><a href='/software/drakx/snapshot/drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.bz2'>drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.bz2</a><br/><a href='/software/drakx/snapshot/drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.xz'>drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.tar.xz</a><br/><a href='/software/drakx/snapshot/drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.zip'>drakx-3b7011a5f27c1cdc645a5a9734391ab88544ba3c.zip</a><br/></td></tr></table>
<div class='commit-subject'>updated pot file</div><div class='commit-msg'></div><div class='diffstat-header'><a href='/software/drakx/diff/?h=V1_1_8_8mdk&amp;id=3b7011a5f27c1cdc645a5a9734391ab88544ba3c'>Diffstat</a> (limited to 'perl-install/share')</div><table summary='diffstat' class='diffstat'>