#!/usr/bin/perl


use strict;
use Inline::Files;
use Data::Dumper;
use Switch;
use XML::Simple;
use Getopt::Long;

our $XMLClass;
our $XMLCourse;
our @Labs;

our %Machines;			# Machines list from class.xml
our @SelectedMachines;		# Machines list given as the command line argument

our $Config_File = "labmaker.conf";
our %Config = (
	"show_host" 	=> "no",

	# Каталоги
	"path_labmaker" => "/var/labmaker/",
	"path_classes"	=> "/var/labmaker/classes/",
	"lablogs_path"	=> "/var/labmaker/lablogs/",
	"courses_path"	=> "/var/labmaker/courses/",
	"outpath"	=> "/var/labmaker/out/",

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

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

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

our %Run = (
	"lab" => ""
);

our %Scripts;

sub load_class;
sub load_config;
sub load_course;
sub load_scripts;

sub lm_next;
sub lm_prev;
sub lm_start;
sub lm_stop;
sub lm_set;
sub lm_do;
sub lm_report;
sub lm_show_hosts;
sub lm_show_labs;

sub load_run;
sub save_run;
sub print_log;
sub print_usage_info;
sub main();

main();

sub main()
{
	binmode STDOUT, ":utf8";

	if (! @ARGV) {
		print_usage_info();
		exit(0);
	}

	load_config;
	load_run;
	load_scripts;
	load_class;
	load_course;
	
	my $arg = join " ", @ARGV;

	# Getting @SelectedMachines if any
	if ($arg =~ s/@(.*?)\s//) {
		my $machines = $1;
		my @list = split /,/, $machines;
		for my $interval (@list) {
			my ($first, $last) = split /-/, $interval;

			push @SelectedMachines, $first;
			while ($first < $last) {
				push @SelectedMachines, ++$first;
			}	
		}
	}

	# Choose command to do
	switch ($arg) {
		case "next"	{ lm_next }
		case "prev"	{ lm_prev }
		case /set /	{ $arg =~ /set (.*)/; lm_set $1 }
		case "report"	{ lm_report }
		case "start"	{ lm_start }
		case "stop"	{ lm_stop }
		case "show hosts" { lm_show_hosts }
		case "show labs" { lm_show_labs }
		case /do /  { $arg =~ /do (.*)/;  lm_do "$1" }
		else 		{ print_usage_info() }
	}
	save_run;
	exit(0);
}

sub load_scripts
{
	local $/;
	$_=<SCRIPTS>;
	%Scripts = ("empty-element", split (/###(.*)\n/));
	delete($Scripts{"empty-element"});
}

sub load_config
{
	my %file_config;
	my %argv_config;
	#read_config_file(\%file_config, $Config_File);
	GetOptions(\%argv_config, map "$_=s", keys %Config);
	%Config = (%Config, %file_config, %argv_config);
}

sub load_course
{
	$XMLCourse = XMLin($Config{"courses_path"}.$XMLClass->{"course"}.".xml", ForceArray => 1 )  
		or die "Can't open file of the course ",$XMLClass->{"course"}," [with .xml extension]\n";
#	print Dumper($XMLCourse);
	for my $lab (@{$XMLCourse->{"module"}}) {
		push @Labs, $lab->{"code"};
	}
}

sub load_class
{
	my $classfile =
	$Config{"path_classes"}."/".$Config{"class"}.$Config{"class_suffix"};
	$XMLClass = XMLin($classfile , ForceArray => [ 'student' ] )  
		or die "Can't open file of the class ",$classfile,"\n";

	for my $student (@{$XMLClass->{"student"}}) {
		$Machines{$student->{"host"}} = {
			"name"	=> "$student->{firstname} $student->{surname}",
			"user"	=> "$student->{user}",
			"student" => $student,
		}	
	}
#	print Dumper($XMLClass);
#	print Dumper(\%Machines);
}


sub lm_next
{
	for(my $i=0; $i<=$#Labs; $i++){
		if ( $Labs[$i] eq $Run{"lab"} ) {
			if ($i < $#Labs) {
				lm_set($Labs[$i+1]);
				return ;
			} else {
				die "Lab ", $Run{"lab"}, " is the last. Switch to next lab is impossible"
			}
		}
		
	}
	die "Lab ", $Run{"lab"}, " not found. Don't know which is next"
}

sub lm_prev
# Switch to previous lab
{
	for(my $i=0; $i<=$#Labs; $i++){
		if ( $Labs[$i] eq $Run{"lab"} ) {
			if ($i > 0) {
				lm_set($Labs[$i-1]);
				return ;
			} else {
				die "Lab ", $Run{"lab"}, " is the first. Switch to previous lab is impossible"
			}
		}
		
	}
	die "Lab ", $Run{"lab"}, " not found. Don't know which is previous"
}

sub lm_set
# Switch to $_[0] lab
# FIXME
{
	my $lab = shift;
	print "Current lab is $lab\n";
	$Run{"lab"} = "$lab";
	lm_do "setlab", $lab;
}


sub lm_start
# Start new training day
{
	print_log(`date`." STARTED\n");
	if ($Run{"lab"}) {
		lm_next;
	}
	else
	{
		# First lab in the course
		lm_set($Labs[0]);
	}
}

sub lm_stop
# Stop this training day
{
	print_log(`date`." STOPPED\n");
}


sub lm_show_hosts
# Show hosts used to run a commands
{
	my $i=1;
	for my $m (sort keys %Machines) {
		if (!@SelectedMachines || grep /^$i$/, @SelectedMachines) {
			print "($i)","\t",$m,"\t",$Machines{$m}->{"name"},"\n";
		}	
		$i++;
	}
}

sub lm_show_labs
# Show hosts used to run a commands
{
	my $i=1;
	for my $lab (@Labs) {
		print $lab;
		print "*" if $lab eq $Run{"lab"};
		print "\n";
	}
}

sub lm_do
# Do the $_[0] command on all of the hosts 
{
	my $command = shift;
	my $arg = join " ", @_;
	my $i=1;
	for my $m (sort keys %Machines) {
		if (!@SelectedMachines || grep $_ eq $i, @SelectedMachines) {
			print "$m:\n" if $Config{"show_host"} =~ /y/i;

			my %myenv = ( %Config, 
				host 	=>	$m,
				dirs	=>	"/root /home/".$Machines{$m}->{"user"},
				lablogs =>	$Config{"lablogs_path"}."/".
						$XMLClass->{"course"}."/".
						$XMLClass->{"date"}."/".
						"$m",
				lab	=> 	$arg,			
				
				email	=>	$Machines{$m}->{"student"}->{"email"},
				company	=>	$Machines{$m}->{"student"}->{"company"},
				center 	=>	$XMLClass->{"center"},
				course 	=>	$XMLClass->{"course"},
				date 	=>	$XMLClass->{"date"},
				name	=> 	$Machines{$m}->{"name"},
				coursepath =>	$XMLCourse->{"path"},

			);
			if (grep { $_ eq $command} keys %Scripts) {
				$_=$Scripts{"$command"};
				s/\$(\w+)/$myenv{$1}/ge;
				open(SHELL, "|/bin/sh -s");
				binmode SHELL, ":utf8";
				print SHELL $_;
				close (SHELL);
			}
			else {
				my $res = `ssh $Config{"ssh_user"}\@$m $command`;
				if ($res) {
					my $count = ($res =~ s/(^)/$m: /mg);
					print $res;
					print "\n" if ($count > 1);
				}
			}	
		}	
		$i++;
	}
}


sub lm_report
{

	print "Not implemented yet\n";
	exit(1);

=cut comment

Дальше идут скрипты, код которых нужно реализовать здесь.
Как минимум.

=cut 

	my $lm_script_make_report_web = <<'SCRIPT';

#!/bin/sh

####cd /home/devi/lm
./lm do copy-lablogs
##./lm-report --input /home/murk/.labmaker --output /var/www/lm/murk.html
##exit

COURSE=ug-h
DATE=2005-04-25
SUFF=".linux.nt"
#MACHINES="m01 m02 m03 m04 m05 m06 m07 m08 m09 m10 m11 m12 m13 m14 m15"
#MACHINES="m1 m2 m3 m4 m5 m6 m7 f1 f2 f3 f4 f5"
MACHINES="m01 m02 m03 m04 m05 m06 m07 m08"
#MACHINES="m1"
USERS="user root"
WEBDIR=/var/www/lm

for u in $USERS
	do
		for m in $MACHINES
			do
				e=utf-8
				[ "${m##f}" = "$m" ] || e=koi8-r
				#e=koi8-r
				mkdir -p $WEBDIR/$DATE/$m
				cp share/*.ico share/*.css $WEBDIR/$m
				#echo Processing Lablogs/$COURSE/$DATE/$m$SUFF/$u/
				./lm-report\
					--input	Lablogs/$COURSE/$DATE/$m$SUFF/$u/ \
					--diffs "Lablogs/$COURSE/$DATE/$m$SUFF/$u/ Lablogs/$COURSE/$DATE/$m$SUFF/root/"\
					--output /var/www/lm/$DATE/$m/$u.html \
					--encoding $e
			done
	done
					

SCRIPT

	my $lm_script_make_report_all = <<'SCRIPT';

#!/bin/sh
WEBDIR=/var/www/lm

find . -type d -maxdepth 5 -mindepth 5 |\
	while read dir
	do
		subdir=${dir##Lablogs/}
		e=utf-8
		echo $dir | grep -qi bsd && e=koi8-r
		echo $dir | grep -qi /f && e=koi8-r

		mkdir -p $WEBDIR/${subdir%/*}
		cp share/*.ico share/*.css $WEBDIR/${subdir%/*}
		#echo Processing Lablogs/$COURSE/$DATE/$m$SUFF/$u/
		./lm-report\
			--input $dir	 \
			--diffs "${dir%/user}/root $dir"\
			--output $WEBDIR/$subdir.html \
			--encoding $e
	done

SCRIPT
}

sub load_run
{
	my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
	open (RUN, $runfile)
		or return;
	while (<RUN>) {
		chomp;
		my ($var, $val) = split /\s+/,$_,2;
		$Run{$var}=$val;
	}
	close (RUN);	
}

sub save_run
{
	my $runfile = $Config{"path_labmaker"}."/".$Config{"path_runfile"};
	open (RN, "$runfile")
		or die "Can't save running state to $runfile";
	for my $var (keys %Run) {
		print RN $var,"\t",$Run{$var},"\n";
	}
	close (RN);	
}

sub print_log
{
	my $logfile = $Config{"path_labmaker"}."/".$Config{"path_logfile"};
	open (LOG, ">>$logfile")
		or die "Can't open logfile $logfile for writing";
	print LOG  @_;
	close (LOG);	
}


sub print_usage_info
{
	print "Usage:\n\n\t$0 [host-list] command\n";
	while (<USAGE>) {
		print $_;
	}
}

__USAGE__

Commands:

	next		-- next lab
	prev		-- prev lab
	set LAB		-- set current lab to LAB
	start		-- start this day training
	stop		-- stop this day training
	show hosts	-- show available hosts in the class
	show labs	-- show available labs in the course
	do COMMAND	-- do specified command on the hosts of hostlist
	report		-- generate XML/HTML reports

	
do commands:
	
	install [PROFILE] -- install profile 
	
Host list:	

	@N		-- machine N
	@N1-N2		-- all of the machines from N1 to N2
	@N1,N2,N3	-- machine N1, N2 and N3

	N* is numbers or domain names of the machines.
	
	If host list is not specified, 
	command is executed on all of the machines



__SCRIPTS__
###install
cat $sshkey | $lmssh $ssh_user@$host /bin/sh -c '"mkdir -p ~/.ssh; cat >>~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys"'

###install-lm
cat $lminstall | ssh $ssh_user@$host /bin/sh -s $dirs

###copy-lablogs
for i in $dirs
do
	mkdir -p $lablogs/${i##*/}
	scp -q $ssh_user@$host:${i}/.labmaker/* $lablogs/${i##*/}
done

###setlab
for i in $dirs
do
	echo $lab | ssh $ssh_user@$host "cat > "${i}"/.labmaker/lab"
done

###makeout
common=$course-$date
personal=$course-$date-$email
mkdir -p $outpath/${common}/{Lablogs,Docs}
mkdir -p $outpath/${personal}/{Course,Files}
cd $outpath/${personal}
ln -s ../${common}/Lablogs .
ln -s ../${common}/Docs .
cd ~-
export UG_PERSONAL=${PWD}/$outpath/${personal}/Course
export UG_CENTER="$center"
export UG_COURSE="$course"
export UG_DATE="$date"
export UG_STUDENT="$name"
export UG_COMPANY="$company"
cd $coursepath; make personal; cd ~-

###watch
cat taillast.pl | ssh $ssh_user@$host perl - /root/.labmaker