$class=7;
$subject="Алгебра";
$firstlesson=-1;
$i=0;
$le=0;
$maxlesson=118;

sub initles {
  open(F,"<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,"<1");
  while(<F>){
    /(.*);(.*)/;
    $filename[$1]="lessons/$2";
  }
  close(F);
}

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

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

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

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

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

initles;
filenames;
topics;
createindex;


open(F,"<1");
print qq[<?xml version = '1.0' encoding = 'windows-1251' ?>
<?xml-stylesheet href="standings-refreshing.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,"plan.csv");
while(<Fcsv>) {
  chomp;
  /(.*);(.*);(.*)/;
  if($1!='') {   #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' name='$title' hours='$hours' file='index".${firstlesson}.".htm' />\n";


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