# SMArT
#
# Configuration file code
#
# Copyright 2001 Wilmer van der Gaast (lintux@lintux.cx)
#
# Updated for marker-aware config writing.

my( @info, @conf, @rawconf, $l );

$info[1] = 'Volume';
$info[2] = 'Server name';
$info[3] = 'Internal network';
$info[4] = 'IPX device';
$info[5] = 'Device flags';
$info[6] = 'Version spoofing';
$info[7] = 'Password handling';
$info[8] = 'Login/security flags';
$info[9] = 'creat() modes';
$info[10] = 'Guest group';
$info[11] = 'Guest user';
$info[12] = 'Supervisor login';
$info[13] = 'User login';
$info[15] = 'Automatic login mapping';
$info[16] = 'Startup tests';
$info[17] = 'Bindery/user flags';
$info[18] = 'Queue flags';
$info[21] = 'Print queue';
$info[22] = 'Print server';
$info[30] = 'Burst mode';
$info[40] = 'Volume cache';
$info[41] = 'Share/lock files';
$info[42] = 'Spool dir';
$info[45] = 'Bindery files';
$info[46] = 'Attribute files';
$info[47] = 'Trustee files';
$info[50] = 'Conversion tables';
$info[60] = 'MAX_CONNECTIONS';
$info[61] = 'MAX_NW_VOLS';
$info[62] = 'Reserved';
$info[63] = 'MAX_DIR_BASE_ENTRIES';
$info[68] = 'USE_MMAP';
$info[69] = 'HANDLE_ALL_SAP_TYPS';
$info[70] = 'NETWORK_SERIAL_NMBR';
$info[71] = 'NETWORK_APPL_NMBR';
$info[80] = 'max_dir_search_handles';
$info[100] = 'Debug IPX kernel';
$info[101] = 'Debug NWSERV';
$info[102] = 'Debug NCPSERV';
$info[103] = 'Debug NWCONN';
$info[104] = 'Debug NWCLIENT';
$info[105] = 'Debug NWBIND';
$info[106] = 'Debug NWROUTED';
$info[200] = 'Daemon mode';
$info[201] = 'Log file';
$info[202] = 'Log mode';
$info[210] = 'Shutdown timing';
$info[211] = 'Shutdown warnings';
$info[300] = 'Routing info';
$info[301] = 'Routing log';
$info[302] = 'Routing log flags';
$info[310] = 'Watchdogs';
$info[400] = 'nwserv.stations file';
$info[401] = 'Reply to nearest server requests';
$info[402] = 'Reply to connect requests';

open( CONF, '<' . $mars_config ) or die "Could not open $mars_config: $!";
@conf = ();
@rawconf = ();

while( $l = <CONF> )
{
    push( @rawconf, $l );

    my $x = $l;
    $x =~ s/[\r\n]//g;
    $x =~ s/[\t ]+/ /g;
    $x =~ s/#.*//;
    $x =~ s/^[\t ]*//;
    $x =~ s/[\t ]*$//;

    if( $x ne '' )
    {
	push( @conf, $x );
    }
}
close( CONF );

sortconfig();

sub sortconfig()
{
    @conf = sort( { $a cmp $b } @conf );
    @conf = sort( { ( split( ' ', $a ) )[0] <=> ( split( ' ', $b ) )[0] } @conf );
}

sub getconfigline( $ )
{
    my( @c, $c );

    @c = getconfig( @_ );
    $c = $c[0];
    $c =~ s/^[0-9]* //;
    return( $c );
}

sub getconfig( $ )
{
    my( @c );

    @c = grep( /^$_[0] /i, @conf );
    return( @c );
}

sub addconfigline( $ )
{
    unshift( @conf, $_[0] );
}

sub delconfigline( $ )
{
    @conf = grep( !/^$_[0] /i, grep( !/^$_[0]$/i, @conf ) );
}

sub normalize_line( $ )
{
    my $x = $_[0];

    $x =~ s/[\r\n]//g;
    $x =~ s/[\t ]+/ /g;
    $x =~ s/#.*//;
    $x =~ s/^[\t ]*//;
    $x =~ s/[\t ]*$//;

    return( $x );
}

sub section_of_line( $ )
{
    my $x = normalize_line( $_[0] );

    if( $x =~ /^([0-9]+)\b/ )
    {
	return( $1 );
    }

    return( '' );
}

sub grouped_section_key( $ )
{
    my $sec = $_[0];

    if( $sec >= 100 && $sec <= 106 ) { return '100-106'; }
    if( $sec >= 200 && $sec <= 202 ) { return '200-202'; }
    if( $sec >= 210 && $sec <= 211 ) { return '210-211'; }
    if( $sec >= 300 && $sec <= 302 ) { return '300-302'; }

    return $sec;
}

sub build_marker_map()
{
    my( %map, $line, $sec, $key );

    foreach $line ( @conf )
    {
	$sec = section_of_line( $line );
	next if $sec eq '';

	$key = grouped_section_key( $sec );
	push( @{ $map{$key} }, $line );
    }

    # Keep SYS as first entry in section 1
    if( defined( $map{'1'} ) )
    {
	my @sys = grep( /^1 SYS /, @{ $map{'1'} } );
	my @rest = grep( !/^1 SYS /, @{ $map{'1'} } );
	$map{'1'} = [ @sys, @rest ];
    }

    return %map;
}

sub writeconfig_compact()
{
    my( $i, $l );

    sortconfig();

    open( CONF, '>' . $mars_config ) or die "Could not write $mars_config: $!";

    $l = ( getconfig( '1 SYS' ) )[0];
    delconfigline( '1 SYS' );
    addconfigline( $l );

    foreach $i ( @conf )
    {
	$l = $i;
	$l =~ s/ .*//;
	printf( CONF "%-50s    # %s\n", $i, $info[$l] );
    }

    close( CONF );
}

sub writeconfig_markers()
{
    my( %secmap, %emitted );
    my( $line, $active_key, $inside_active );

    %secmap = build_marker_map();
    $inside_active = '';

    open( CONF, '>' . $mars_config ) or die "Could not write $mars_config: $!";

    foreach $line ( @rawconf )
    {
	if( $line =~ /^\s*#\s*>>>\s*SMARTHOOK\s+SECTION\s+([0-9]+(?:-[0-9]+)?)\s+ACTIVE\s+BEGIN/i )
	{
	    $active_key = $1;
	    $inside_active = $active_key;

	    print CONF $line;

	    if( defined( $secmap{$active_key} ) )
	    {
		foreach my $entry ( @{ $secmap{$active_key} } )
		{
		    print CONF $entry . "\n";
		}
	    }

	    $emitted{$active_key} = 1;
	    next;
	}

	if( $line =~ /^\s*#\s*<<<\s*SMARTHOOK\s+SECTION\s+([0-9]+(?:-[0-9]+)?)\s+ACTIVE\s+END/i )
	{
	    $inside_active = '';
	    print CONF $line;
	    next;
	}

	if( $inside_active ne '' )
	{
	    # Skip old content inside ACTIVE blocks completely.
	    next;
	}

	print CONF $line;
    }

    close( CONF );
}

sub writeconfig_preserve_layout()
{
    my( %secmap, %written, @sections, $sec, $line, $sysline );
    my( $heading_sec );

    sortconfig();

    $sysline = ( grep( /^1 SYS /, @conf ) )[0];
    if( defined( $sysline ) )
    {
	@conf = grep( $_ ne $sysline, @conf );
	unshift( @conf, $sysline );
    }

    foreach $line ( @conf )
    {
	$sec = section_of_line( $line );
	if( $sec ne '' )
	{
	    push( @{ $secmap{$sec} }, $line );
	}
    }

    open( CONF, '>' . $mars_config ) or die "Could not write $mars_config: $!";

    foreach $line ( @rawconf )
    {
	if( $line =~ /^\s*#.*Section\s+([0-9]+)\b/i )
	{
	    $heading_sec = $1;

	    foreach $sec ( sort { $a <=> $b } keys( %secmap ) )
	    {
		next if $written{$sec};
		next if $sec > $heading_sec;

		if( defined( $secmap{$sec} ) )
		{
		    foreach my $entry ( @{ $secmap{$sec} } )
		    {
			print CONF $entry . "\n";
		    }
		}
		$written{$sec} = 1;
	    }
	}

	$sec = section_of_line( $line );

	if( $sec eq '' )
	{
	    print CONF $line;
	    next;
	}

	if( ! $written{$sec} )
	{
	    if( defined( $secmap{$sec} ) )
	    {
		foreach my $entry ( @{ $secmap{$sec} } )
		{
		    print CONF $entry . "\n";
		}
	    }
	    $written{$sec} = 1;
	}
    }

    @sections = sort { $a <=> $b } keys( %secmap );
    foreach $sec ( @sections )
    {
	next if $written{$sec};

	print CONF "\n";
	foreach my $entry ( @{ $secmap{$sec} } )
	{
	    print CONF $entry . "\n";
	}
	$written{$sec} = 1;
    }

    close( CONF );
}

sub config_has_smart_markers()
{
    foreach my $line ( @rawconf )
    {
	if( $line =~ /SMARTHOOK\s+SECTION/i )
	{
	    return 1;
	}
    }
    return 0;
}

sub writeconfig()
{
    if( defined( $smart_compact_nwservconf ) && $smart_compact_nwservconf )
    {
	writeconfig_compact();
    }
    elsif( config_has_smart_markers() )
    {
	writeconfig_markers();
    }
    else
    {
	writeconfig_preserve_layout();
    }
}
