#!/usr/local/bin/perl -s

if ( $r )
{
    foreach (<*.dist>)
    {
	s/.dist$//;
	unlink $_;
    }
    exit(0);
}

$config_file = "eternity.conf";
$config_cache = "conf.cache";
$disclaimer = "disclaimer.html";

unlink $config_cache;

# list of vars which can be set in config file
%config_var = 
    ( "eternity_dir", 1, "eternity_url", 1, "eternity_cgi", 1,
     "perl5_exe", 1, "pgp_exe", 1, "sha1_exe", 1, "echo_exe", 1, 
     "admin_name", 1, "admin_email", 1, 
     "nntp_server", 1, "news_spool", 1, 
     "altavista_sever", 1, "dejanews_server", 1,
     "cache", 1, 
     "cache_size", 1, "cache_dir", 1, 
     "directory_service", 1,
     "newsgroups_file", 1, "lock_dir", 1, "log_file", 1, 
     "cgi_update", 1, "cgi_update_interval", 1 );

parse_conf_file( $config_file, $config_cache );

do( $config_cache );

open( DISCLAIM, $disclaimer ) || die( "can't access \"$disclaimer\"\n" );
{
    @disclaim = <DISCLAIM>;
    $disclaim = join( '', @disclaim );
    close( DISCLAIM );
}

while ( <*.dist> )
{
    open( DIST, $_ ) || die( "can't access \"$_\"\n" );
    $mode = mode( $_ );
    s/\.dist$//;
    $new = $_;
    open( NEW, ">$new" ) || die( "can't create \"$new\"\n" );
    while ( <DIST> )
    {
	s/\@perl5\@/$perl5_exe/g;
	s/\@echo\@/$echo_exe/g;
	s/\@cgi\@/$eternity_cgi/g;
	s/\@url\@/$eternity_url/g;
	s/\@dir\@/$eternity_dir/g;
	s/\@disclaimer\@/$disclaim/g;
	print NEW $_;
    }
    close( NEW );
    close( DIST );
    chmod( $mode, $new ) || die( "chmod $mode $new failed: $!\n" );
}

######################################################################
# parse config file

sub parse_conf_file
{
    local( $file, $out ) = @_;
    local( $var, $val );

    if ( ! open( CFG, $file ) )
    {
	print STDERR "can not read config file $file\n";
	return;
    }

    if ( ! open( CACHE, ">$out" ) )
    {
	close( CFG );
	print STDERR "can not create config cache $out\n";
	return;
    }

    print CACHE<<EOM;
# This file is automatically created by the eternity program.
# Make changes to $file
EOM

    while ( <CFG> )
    {
	next if /^\s*#/;
	chomp;
	s/\s*#.*$//;
	next if $_ eq "";
	($var,$val) = /^\s*(\w+)\s*=\s*(.+)\s*$/;
	$var = "\L$var";
	if ( defined( $config_var{ $var } ) ) # only allowed variables
	{
	    $val =~ s/^off$/0/;	    $val =~ s/^no$/0/;
	    $val =~ s/^on$/1/; 	    $val =~ s/^yes$/1/;
	    $val =~ s/^encrypt$/2/; $val =~ s/^encrypted$/2/;
	    if ( ! ( $val =~ m/^\d$/ ) )
	    {
		$val =~ s/secs$//;
		$val =~ s/mins$// && ($val *= 60);
		$val =~ s/hours$// && ($val *= 3600);
		$val =~ s/Kb$//;
		$val =~ s/kb$//;
		$val =~ s/Mb$// && ($val *= 1024);
		$val =~ s/mb$// && ($val *= 1024);
		$val =~ s/@/\\@/g;
		$val =~ s/`//g;	           # no shell escapes!
            }
	    print CACHE '$'. "$var = $val;\n";
        }
    }
    close( CACHE );
    close( CFG );
}

sub mod_time
{
# modify time is # 9
    return (stat($_[0]))[9];
}

sub mode
{
# chmod permissions is # 2
    return (stat($_[0]))[2] & 07777; 
# & 07777 because linux gives us big numbers which chmod doesn't like
}
