use Getopt::Std;

getopt("dcsm");

$dir=$opt_d;
$class=$opt_c;
$subject=$opt_s;
$firstlesson=-1;
$i=0;
$le=0;
$maxlesson=$opt_m;

sub initles {
  open(F,"<$dir/1");
  while(<F>){
    /(.*);(.*)/;
      $_=$2;
      if (/(\d+)_(\d+)/) {
        $f=$1;$l=$2;
        for($i=$f;$i<=$l;$i++) {
          $nextles[$i]=($2+1<=$maxlesson)?($2+1):(-1);
          $prevles[$i]=($1-1>0)?($1-1):-1;
        }
      } 
      else {
        if (/(\d+)/) {
            $nextles[$1]=($1+1<=$maxlesson)?($1+1):(-1);
            $prevles[$1]=($1-1>0)?($1-1):(-1);
        }
      }
  }
  close(F);
}

sub filenames {
  open(F,"<$dir/1");
  while(<F>){
    /(.*);(.*)/;
    $filename[$1]="lessons/$2";
  }
  close(F);
}

sub topics {
  open(Fcsv,"<$dir/plan.csv");
  while(<Fcsv>){
    /(.*);(.*);(.*)/;
   if ($1 eq '') {
     $curtopic=$2;
     $prevtopiclesson=$i;
          print STDERR "*".$i;
   }
   else {
   $lt=$2;
   $_=$1;
      if (/(\d+)\s*-\s*(\d+)/) {
        $f=$1;$l=$2;
        print STDERR $f." ".$l."\n";
        for($i=$f;$i<=$l;$i++) {
          if($curtopic ne '') {
            $topic[$i]="&gt;&gt; ".$curtopic;
		    $lessontitle[$i]=$lt;
          }
          print STDERR "*".$i;
        }
      } 
      else {
        if (/(\d+)/) {
          if($curtopic ne '') {
            $topic[$1]="&gt;&gt; ".$curtopic;
		    $lessontitle[$i]=$lt;
          }
        }
      }
   
  }
  }
  close(Fcsv);
}

sub createindex {
  for($i=1;$i<=$maxlesson;$i++) {
    open(OFile,">$dir/index$i.htm");
    print OFile qq[<html>
      <frameset rows="70,*" BORDER="0">
        <frame src="index_top$i.htm" marginheight="0" marginwidth="10">
        <frame src="].$filename[$i].qq[" marginheight="0" marginwidth="10">
      </frameset>
      </frameset>
     </html>
];
    close(OFile);

    open(OFile,">$dir/index_top$i.htm");
	print OFile qq[<!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.0 Transitional/EN">
<HTML><HEAD><TITLE></TITLE>
<META content="text/html; charset=windows-1251" http-equiv=Content-Type><LINK 
href="assets/index2.css" rel=stylesheet type=text/css>
</HEAD>
<BODY>
<style>
table {font-family:Tahoma,Arial;font-size:9pt;}
a {color: #303070}
a:visited {color : #707090}
</style>
<table width="100%" cellpadding=5>
  <tr><td align="center" colspan=4><font color="#303070"><B>&nbsp;$subject &gt;&gt; $class класс $topic[$i]</B></a></td><tr>
  <tr>];

if($prevles[$i]!=-1) {
   print OFile qq[
						<td width="25%" align="left">&nbsp;<a href="index].$prevles[$i].qq[.htm" target="_parent">&lt;&lt;&nbsp;предыдущий урок</a></td>
   ];
}
   print OFile qq[
						<td width="50%" align="center">&nbsp;];
if ($lessontitle[$i]=~/(Самостоятельная работа №)(\d+)/) 
{print OFile qq[<a href="work/sr${2}.doc" target="_blank">$1$2</a>];}
if ($lessontitle[$i]=~/(Контрольная работа №)(\d+)/) 
{print OFile qq[<a href="work/kr${2}.doc" target="_blank">$1$2</a>];}

print OFile qq[</a></td>
   ];
if($nextles[$i]!=-1) {
   print OFile qq[
						<td width="25%" align="right"><a href="index].$nextles[$i].qq[.htm" target="_parent">следующий урок&nbsp;&gt;&gt;</a>&nbsp;</td>                   
   ];
}
print OFile qq[
						<tr>
</table>];

print OFile qq[
</body>
</html>];	
    close(OFile);
  
  }
}

initles;
filenames;
topics;
createindex;


open(F,"<$dir/1");
open(FMenu,">$dir/menu.xml");
print qq[<?xml version = '1.0' encoding = 'windows-1251' ?>
<?xml-stylesheet href="../standings-refreshing.xsl" type="text/xsl"?>             
<?cocoon-process type="xslt"?>
<lessons>
];
print FMenu qq[<?xml version = '1.0' encoding = 'windows-1251' ?>
<?xml-stylesheet href="../menu.xsl" type="text/xsl"?>             
<?cocoon-process type="xslt"?>
<lessons>
];

while(<F>) {
  chomp;
  /(.*);(.*)/;
  $oldb=$b;
  $a=$1;$b=$2;
  $les{$a}=$b;
  if($b ne $oldb) {
    $le++;
    $i++;
    $ordles[$i]=$a;

  }
}

$i=0;
  open(Fcsv,"$dir/plan.csv");
while(<Fcsv>) {
  chomp;
  /(.*);(.*);(.*)/;
  if($1 ne '') {   #lesson
    $i++;
    $par=0;
    $title=$2;
    $hours=$3;
    $lessons=$1;
    $lessons=~/(\d+)/;
    $firstlesson=$1;
  } 
  else {         #part
    $par=1;
    $title=$2;
    $parttitle=$title;
    $hours=$3;
  }
  if($par==0) {
    print "<lesson fullnumber='$lessons' hours='$hours' file='index".${firstlesson}.".htm'> <name> $title </name></lesson>\n";
    print FMenu "<lesson fullnumber='$lessons' hours='$hours' file='index".${firstlesson}.".htm'> <name> $title </name></lesson>\n";


  } 
  else {
    print "<part name='$title' hours='$hours' />\n";
    print FMenu "<part name='$title' hours='$hours' />\n";
    
  }
}
print "</lessons>";
print FMenu "</lessons>";
close(FMenu);