#/usr/bin/perl -w
###############################################################################
# Perl-skripti harjoitustyon ottamiseksi WWW:sta tai laittamiseksi sinne
#
#  Kutsun syntaksi:
#
#    luedemo.pl oppilastiedosto tuloshakemisto alihakemisto kurssi kurkikenttä tulostiedosto  n|s maxpoints  [hakusana]
#
#  Esim:
#
#    luedemo.pl k:\demowww.txt n:\kurssit\cpp\demot\demo01 demo1 ohj d1 ohjimp.txt 10
#
#  luedemo siirtyy ensin hakemistoon 
#    n:\kurssit\cpp\demot\demo01\demo1
#  rupeaa lukemaan tiedostoa k:\demowww.txt
#  siirtyy jokaisen opiskelijan kohdalla omaan hakemistoon 
#    n:\kurssit\cpp\demot\demo01\demo1\mattim
#  avaa hakemistossa olevan tiedoston files.txt, laskee tehtävien summan
#  ja kirjoittaa uuden rivin tiedostoon
#    n:\kurssit\cpp\demot\demo01\demo1\index.html
#      <a href="mattim">Matti Meikäläinen 5</a><br>
#  sekä
#    ohjimp.txt
#      555;d1;5
# Parametrilla n|s valitaan joko n (=no secret) tai s (=secret)
#
#  Tiedoston DemoWWW.txt muoto (erottimina tab-merkki):
# OpiskelijaID	Sukunimi	Etunimet	Email	+DemoWWW	+Salattu
# 688	Lappalainen	Vesa Tapani	vesal@mit.jyu.fi	abc123	SALATTU
#
# Jos hakusana on annettu, käsitellään vain ne oppilaat, joilla
# sukunimi + " " + etunimi + " " + email sisältää hakusanan
#
# Vesa Lappalainen 
#  13.1.2001
#
###############################################################################

use strict;

###############################################################################
sub trim($) { # merkkijono => merkkijono
  my($s) = @_;
# Poistetaan merkkijonosta alkutyhjat, lopputyhjat ja rivinvaihdot (lopusta)
# Esim. "   kissa istuu\n   "  => "kissa istuu"
  $s =~ s/^\s*//;      # alkutyhjat
  $s =~ s/\s*$//;      # lopputyhjat (myös rivinvaihdot)
# s/^\s*(.*?)\s*$/\1/;
  return $s;
}

###############################################################################
sub CountPoints($) { # merkkijono => double
  my($filename) = @_;
  my $FILESTXT;
  my $points = 0;
  if ( !open(FILESTXT,$filename) ) { return -1; }
  while ( my $row = <FILESTXT> ) {
    $row = trim($row);
#print "$row\n";
    if ( $row eq "" ) { next; }
    my ($file,$comment) = split("=",$row,2);
    if  ( !( $comment =~ /\[T/ )  ) { next; }  # Jos ei ole [T, niin ei tehtävää
    my ($task,$com) = split("\]",$comment,2);
    my ($tasknr,$p) = split(":",$task);  $p = trim($p);
#    print "$p $com\n";
    $points += $p;
  }
  return $points;
}



###############################################################################
# Pääohjelma
###############################################################################
my $studentfile = $ARGV[0];
my $outputdir   = $ARGV[1];
my $demoname    = $ARGV[2];
my $course      = $ARGV[3];
my $kurkifield  = $ARGV[4];
my $outputfile  = $ARGV[5];
my $getsecret   = $ARGV[6];
my $maxpoints   = $ARGV[7];
my $searchword  = $ARGV[8];

if ( $maxpoints eq "" )  { $maxpoints = 10; }

print "$studentfile $outputdir $demoname $kurkifield $outputfile $searchword\n";

open(NODEMOS,">nodemo.txt$getsecret") or die("File can not be created: nodemo.txt$getsecret!\n");

open(STUDENTFILE,$studentfile) or die("File not found: $studentfile!\n");
open(OUTPUTFILE,">$outputfile") or die("File can not be created: $outputfile!\n");


mkdir $outputdir;
chdir $outputdir or die("Can not change to $outputdir\n");
mkdir $demoname;
chdir $demoname or die("Can not change to $demoname\n");

open(INDEXFILE,">index.html") or die("File can not be created: index.html!\n");
  print INDEXFILE <<"__STOP__";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head><title>$demoname</title></head>
<frameset cols=\"15%,85%\">
<frame src="users.html" NAME=m0>
<frame src="../suostumus.html" NAME=m1>
<noframes>
<body>
<h1>$demoname</h1>
</body>
</noframes>
</frameset>
</html>
__STOP__
  close INDEXFILE;

open(INDEXFILE,">users.html") or die("File can not be created: users.html!\n");
  print INDEXFILE <<"__STOP__";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<base target=m1>
<title>$demoname</title>
</head>
<body>
<h1>$demoname</h1>
__STOP__


my $row=<STUDENTFILE>;
my $nr = 0;
print INDEXFILE "$nr <a href=\"malli/index.html\">Mallivastauksia</a><br>\n";
mkdir "malli";

while ( $row = trim(<STUDENTFILE>) ) {
  if ( $row eq "" ) { next; }
  my ($stunr,$lastname,$firstnames,$email,$demowww,$secret,$demoid) = split("\t",$row,7);
  my ($firstname,$rest) = split(" ",$firstnames,2);
  my ($user) = split("@",$email); 
  my $name = "$firstname $lastname";

  if (!( "$lastname $firstname $email" =~ /$searchword/i ) ) { next; }

  if ( ( $getsecret ne "s" ) && ( $secret eq "SALATTU" ) ) { print "SALATTU\n"; next; }
  if ( ( $getsecret eq "s" ) && ( $secret ne "SALATTU" ) ) { next;                    }

  mkdir $user;
  chdir $user;


  my $points = CountPoints("files.txt");
  print "$stunr $name $user $points\n";
  if ( $points < 0 ) {
#    print INDEXFILE "<a href=\"eidemoja.html\">$name</a> = -<br>\n";
#    print NODEMOS "$email ! $name\n";
    print NODEMOS "$email\n";
    $points = "";
  }
  else {
    $nr = $nr + 1;
    print INDEXFILE "$nr <a href=\"$user/index.html?r=$demoid\">$name</a> = $points<br>\n";
    system("perl n:\\bin\\lue.pl PUREHTML files.txt w: $user $course\\$demoname");
    if ( $points > $maxpoints ) { $points = $maxpoints; }
    print OUTPUTFILE "$stunr;$kurkifield;$points\n";
  }
  
  chdir "..";
}


  print INDEXFILE <<"__STOP__";
</body>
</html>
__STOP__
  close INDEXFILE;
  close OUTPUTFILE;

    open(INDEX,">eidemoja.html");
    print INDEX <<"__STOP__";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head><title>Ei demoja</title></head>
<body>
<h1>Ei demoja!</h1>
</body>
</html>
__STOP__
    close INDEX;
  
