#!/usr/bin/perl

use strict;
use CGI qw(:standard);
use XML::Simple;

BEGIN {
	chdir("/home/devi/cvs/lilalo");
	require l3config;
};

my %filter;

for my $key (qw(login_from)) {
	$filter{$key} =  param($key) if param($key);
}

my %Trainings;
my $XMLTraining;
my %Machines;

sub load_training
{
        my $classfile = 
	$_[0]||
        $l3config::Config{"classfile"} || 
        $l3config::Config{"path_classes"}."/".$l3config::Config{"class"}.$l3config::Config{"class_suffix"};

        my $XMLTraining = XMLin($classfile , ForceArray => [ 'student' ] )  
                or die "Can't open file of the training ",$classfile,"\n";

        for my $student (@{$XMLTraining->{"student"}}) {
		$XMLTraining->{host}->{$student->{"host"}}=$student;
        }
	return $XMLTraining;
}

print header(
		-charset => "utf-8", 
		);


if ($ENV{PATH_INFO} eq "/index") {
	# Показываем индекс курсов
	my @training_files = glob($l3config::Config{"path_classes"}."/*".$l3config::Config{"class_suffix"});
	if (@training_files) {
		for my $training_file (@training_files) {
			my $training = load_training($training_file);
			$Trainings{$training->{"date"}}=$training;
		}
		print "<html>\n";
		print "<head>\n";
		print "<title>Журналы лабораторных работ</title>\n";
		print "</head>\n";
		print "<body>\n";
		print "<table>\n";
		for my $tdate (reverse sort keys %Trainings) {
			my $t = $Trainings{$tdate};
			print "<tr>";
			print "<td>".$t->{date}."</td>";
			print "<td>".$t->{course}."</td>";
			print "</tr>\n";
			print "<tr>";
			print "<td/>";
			print "<td><pre>";
			for my $host (sort keys %{$t->{host}}) {
				my $h = $t->{host}->{$host};
				print "$host";
				print " ".$h->{firstname}." ".$h->{surname}." ";
				print "<a href='/cgi-bin/l3/".$tdate."/".$host."/root'>root</a> ";
				print "<a href='/cgi-bin/l3/".$tdate."/".$host."/".$h->{user}."'>".$h->{user}."</a> ";
				print "\n";
			}
			print "</pre><td>";
			print "</tr>\n";
		}
		print "</table>\n";
		print "</body>\n";
		print "</html>\n";
	}
	else {
		print "No training-files found<br/>\n";
		print "Template to load files: ".$l3config::Config{"path_classes"}."*".$l3config::Config{"class_suffix"}."\n"
	}
}
elsif ($ENV{PATH_INFO} eq "/current/index" || $ENV{PATH_INFO} eq "") {
	my $t = load_training();

	print "<html>\n";
	print "<head>\n";
	print "<title>Журналы лабораторных работ</title>\n";
	print "<link rel='stylesheet' href='".$l3config::Config{frontend_css}."' type='text/css'/>\n";
	print "</head>\n";
	print "<body>\n";
	print "<h1>Журналы лабораторных работ</h1>\n";
	print "<table>\n";
	print "<tr class='table_header'>\n";
	print "<td>"."Имя"."</td>";
	print "<td>Хост</td>";
	print "<td colspan='3'>Пользователь</td>";
	print "</tr>\n";
	for my $host (sort keys %{$t->{host}}) {
		print "<tr>\n";
		my $h = $t->{host}->{$host};
		print "<td>".$h->{firstname}." ".$h->{surname}."</td>";
		print "<td>$host</td>";
		print "<td><a href='/cgi-bin/l3/current/".$host."/root'>root</a></td>";
		print "<td><a href='/cgi-bin/l3/current/".$host."/".$h->{user}."'>".$h->{user}."</a></td>";
		print "<td><a href='/cgi-bin/l3/current/".$host."'>все</a></td>";
		print "</td>\n";
		print "</tr>\n";
	}
	print "<tr>\n";
	print "<td>"."Инструктор"."</td>";
	print "<td>from linux.nt</td>";
	print "<td/>";
	print "<td/>";
	print "<td><a href='/cgi-bin/l3/current"."?login_from=192.168.15.254'>все</a></td>";
	print "</tr>\n";
	print "</table>\n";
	print "</body>\n";
	print "</html>\n";
}
elsif ($ENV{PATH_INFO} eq "/current") {
	open (FRONTEND, "./l3-frontend --output - --show_comments no |");
	while (<FRONTEND>) {
		print;
	}
	close(FRONTEND);
}
else {
	# Вызов производится по URL
	my ($skip, $course, $host, $user) = split /\//,$ENV{PATH_INFO},4;

	$l3config::Config{"class"}=$course if $course ne 'current';
	$XMLTraining = load_training;

	my @args=(
		"--output" 		=>	"-",
		"--show_comments" 	=>	"no",
		"--course-center"	=>	$XMLTraining->{center},
		"--course-trainer"	=>	$XMLTraining->{instructor}->{firstname}." ".$XMLTraining->{instructor}->{surname},
		"--course-student"	=>	$XMLTraining->{host}->{$host}->{firstname}." ".$XMLTraining->{host}->{$host}->{surname},
		"--course-code"		=>	$XMLTraining->{course},
		"--course-date"		=>	$XMLTraining->{date},
		"--encoding"		=>	$XMLTraining->{host}->{$host}->{charset},
	);
	if ($course ne 'current') {
		push @args, 	("--backend_datafile"	=>	"/var/lilalo/lablogs-xml/$course/$host/$user.xml");
	} else {
		$filter{hostname} = $host if $host;
		$filter{user} = $user if $user;
		push @args, ("--filter" => join ("&", (map("$_=$filter{$_}", keys %filter))));
		#push @args, ("--filter" => "hostname=".$host."&user=".$user);
	}

	open (FRONTEND, "./l3-frontend ".join(" ",map("\"$_\"",@args))." |");
	while (<FRONTEND>) {
		print;
	}
	close(FRONTEND);
}