package l3config;

use utf8;
use Exporter;
use vars qw(@ISA @EXPORT $VERSION);
use Getopt::Long;

@ISA = ('Exporter');
@EXPORT = qw(%Config &init_config &init_config_without_command_line_processing);

our $System_Config_File = "/etc/lilalo.conf";
our $User_Config_File = "$ENV{HOME}/.l3rc";
$ENV{HOME} ||= "/tmp";


our %Config = (
    "skip_empty"        => "yes",
    "skip_interrupted"  => "no",
    "skip_wrong"        => "no",
    "editors"           => ["vi", "pico", "ee", "vim", "nano"],
    "pagers"            => ["more", "less", "zmore", "zless", "info", 
                            "man", "mc", "trafshow", "screen", "cfdisk",
                            "trafshow-bsd", "yes", "lynx", "links", "centericq"
                            ],
    "full_output_commands" => ["cat"],
    "terminal"          => ["mc"],
    "suppress_editors"  => "yes",
    "suppress_pagers"   => "yes",
    "suppress_terminal" => "yes",

    "terminal_width"    => 400,
    "terminal_height"   => 1000,
    "verbose"           => "yes",

    "head_lines"        => 10,
    "tail_lines"        => 10,
    "cache_head_lines"  => 2500,
    "cache_tail_lines"  => 2500,
    "skip_text"         => "...",
    "show_time"         => "yes",
    "show_diffs"        => "yes",
    "show_screenshots"  => "yes",
    "show_comments"     => "yes",
    "show_notes"        => "yes",

    "input"             => "$ENV{HOME}/.lilalo",
    "diffs"             => "",
    "input_mask"        => "*.script",
    "encoding"          => "utf-8",

    "cache"             => "$ENV{HOME}/.lilalo/report.xml",
    "cache_sqlite"      => "$ENV{HOME}/.lilalo/report.sqlite",
    "cache_stat"        => "$ENV{HOME}/.lilalo/.report.dat",

    "output"            => "/tmp/report.html",
    "output_mask"       => "INDEX",
    "output_format"     => "html",
    "cgi_path"          => "/l3",
    "frontend_files"    => "/l3files",
    "frontend_css"      => "/l3files/l3.css",
    "l3shot_path"       => "/l3shot/",
    "l3shot_suffix"       => ".png",
    "frontend_google_ico"   => "/l3/google.ico",
    "frontend_linux_ico"    => "/l3/linux.ico",
    "frontend_freebsd_ico"  => "/l3/freebsd.ico",
    "frontend_opennet_ico"  => "/l3/opennet.ico",
    "frontend_local_ico"    => "/l3/freebsd.ico",

    "mywi_server"       => "127.0.0.1",
    "mywi_port"         => "19801",

    "stat_inactivity_interval"  =>  "1800",

    "signature"         => "#lm:",
    "from"              => "",
    "to"                => "",
    "lab"               => "",
    "keywords"          => "linux command",
    "files_keywords"    => "linux file",

    comment_width       => "300",
    note_width          => "500",
    time_width          => "6em",

    "mode"              => "daemon",        # daemon | normal
    "daemon_sleep_interval" => "10",
    "detach"            => "yes",
    "agent_pidfile"     => "$ENV{HOME}/.lilalo/l3-agent.pid",

    "backend_address"   => "xgu.ru",
    "backend_port"      => "18030",
    "backend_pidfile"   => "/tmp/l3-backend.pid",
    "backend_datafile"  => "/var/lilalo/lablogs-xml/backend.xml",
    "backend_datadir"  => "/var/lilalo/lablogs-xml/",
    "upload_dir"        => "/var/www/l3shot",

    "l3-agent"          => "l3-agent", 
    "l3-backend"        => "l3-backend", 

    "course-name"       => "", 
    "course-code"       => "", 
    "course-date"       => "",
    "course-center"     => "", 
    "course-trainer"    => "", 
    "course-student"    => "", 

    "filter"            => "",
#lm
    "show_host"         => "no",

    "l3cd"              => "",          # Текущий контекст перехваченных команд
                                        # Возможные варианты:
                                        #   КУРС/ДАТА-НАЧАЛА/МАШИНА/ПОЛЬЗОВАТЕЛЬ
                                        #   ДАТА-НАЧАЛА/КУРС/МАШИНА/ПОЛЬЗОВАТЕЛЬ
                                        #   УНИКАЛЬНЫЙ_ПОЛЬЗОВАТЕЛЬ/КУРС/МАШИНА/ПОЛЬЗОВАТЕЛЬ

# Вспомогательные программы
#"l3-report"    => "./lm-report",
    "l3-report"         => "./l3-report",

# Каталоги
    "path_lilalo"       => "/var/lilalo/",
    "path_classes"      => "/var/lilalo/classes/",
    "path_lablogs"      => "/var/lilalo/lablogs/",
    "courses_path"      => "/var/lilalo/courses/",
    "outpath"           => "/var/lilalo/out/",
    "path_web"          => "/var/www/l3",   # Путь к web-отчётам
    "path_share"        => "./share/",      # Путь к web-отчётам

# Файлы
    "runfile"           => "lm.run", 
    "logfile"           => "lm.log", 

    "class"             => "class",         # Имя файла класса
    "class_suffix"      => ".xml",      # Cуффикс файла класса
    "classfile"         => "",

    "sshkey"            => "$ENV{HOME}/.ssh/id_dsa.pub",
    "lmssh"             => "./lm-ssh",
    "lminstall"         => "./lm-install",
    "ssh_user"          => "root",

    "l3scripts"         => "l3scripts",


    "cgi_path_info"     => "",
    "cgi2file"          => "",

    "year"              =>  "2006",
    "commands_to_show_at_a_go" => "100",
    "start_from_command" => "0",
);

sub read_config_file
{
    my $config = $_[0];
    my $filename = $_[1];
    open(CONFIG, "$filename")
        or return;
    while (<CONFIG>) {
        chomp;
        s/#.*//;
        next if /^\s*$/;
        my ($var, $val) =  split /\s*=\s*/, $_, 2;
        $var =~ s/\s*//;
        $config->{$var} = $val;
    }
    close(CONFIG);
}


sub init_config
{
    my %argv_config;
    my %file_config;
    read_config_file(\%file_config, $System_Config_File);
    read_config_file(\%file_config, $User_Config_File);
    GetOptions(\%argv_config, map "$_=s", keys %Config);
    %Config = (%Config, %file_config, %argv_config);
    for my $key (keys %Config) {
        utf8::decode($Config{$key});
    }
}

sub init_config_without_command_line_processing
{
    my %argv_config;
    my %file_config;
    read_config_file(\%file_config, $System_Config_File);
    read_config_file(\%file_config, $User_Config_File);
    %Config = (%Config, %file_config);
    for my $key (keys %Config) {
        utf8::decode($Config{$key});
    }
}

