#!/usr/bin/perl

$pi = $ENV{'PATH_INFO'};
if( $pi =~ s/^\/nopl// ) { $nopl = 1; }
$qs = $ENV{'QUERY_STRING'};
$pn = "http://$ENV{'SERVER_NAME'}$ENV{'SCRIPT_NAME'}";
$head = "
<head>
  $head
  <META http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-2\">
  <title>Programy konwertujące polskie litery</title>
</head>
";
for $c ( qw( s c u d a w ) )
{
  if( $qs =~ /code=[^&]*$c/ )
  {
    $code{$c} = 'CHECKED';
  }
}

print "Content-type: text/html; charset=ISO-8859-2\n\n";

if( $pi eq '' )
{
  print <<END;
<html>$head
<frameset rows="50%,*">
  <frameset cols="30%,*">
     <frame name="sel" src="$pn/sel?$qs">
     <frame name="index" src="$pn/index?$qs">
  </frameset>
  <frame name="dsc" src="$pn/dsc=0">
</frameset>
END
  print "\n<noframes>\n";
  PrintSel();
  print "<hr>\n";
  PrintIndex();
  print "</noframes>\n</html>\n";
  exit;
}

if( $pi eq '/sel' )
{
  print "<html>$head";
  PrintSel();
  print "</html>\n";
  exit;
}

if( $pi eq '/index' )
{
  print "<html>$head";
  PrintIndex();
  print "</html>\n";
  exit;
}

if( $pi =~ /dsc=(\d+)/ )
{
  print "<html>$head";
  PrintRec($1);
  print "</html>\n";
  exit;
}


sub PrintSel 
{
  print <<END;
<form action="$pn" target="_parent">
  <p align="center">
  <b>Wyświetlaj programy dla:</b>
  </p>
  <input type="checkbox" name="code" value="u" $code{'u'}> Unix'a, <br>
  <input type="checkbox" name="code" value="d" $code{'d'}> DOS'a, <br>
  <input type="checkbox" name="code" value="w" $code{'w'}> Windows, <br>
  <input type="checkbox" name="code" value="a" $code{'a'}> Apple. <br>
  <input type="checkbox" name="code" value="c" $code{'c'}> Programy w źródłach.</font> <br>
  <input type="checkbox" name="code" value="s" $code{'s'}> Na serwery WWW.
  <p>
  <center>
  <input type="submit" value="Zmien">
  </center>
</form>
END
}



sub PrintIndex
{
  print "<p align=\"center\"><b>Programy konwertujące:</b></p>\n";
  print "<ol>\n";
  open( F, "konwersje.dat" ) || die;
  $n=0;
  while( <F> )
  {
    chop;
    
    if( /^\$\$/ ) { $n++; $allow=0; next; }
    
    if( /^\$c (\w+)/ )
    {
      $code = $1;
      $l = length($code);
      for( $i=0; $i<$l; $i++ )
      {
        $c = substr($code,$i,1);
        if( $code{$c} ne '' ) { $allow=1; break; }
      }
      next;
    }
    
    if( /^\$t (.+)$/ ) { $title = $1; next; }
    if( /^\$d (.+)$/ ) 
    { 
      $dsc = $1; 
      if( $allow )
      {
        print "<li> <a href=\"$pn/dsc=$n\" target=\"dsc\">$title</a>: $dsc\n";
      }
      next; 
    }
  }
  close F;
  print "</ol>\n";
}



sub PrintRec
{
  my( $rec ) = @_;

  print "<p align=\"center\"><b>Szczegóły nt. programu</b></p>\n";
  open( F, "konwersje.dat" ) || die;
  # Przewiń plik do początku danego rekordu
  $n=0;
  while( <F> ) { if( /^\$\$/ ) { $n++; if( $n==$rec ) {last;} } }

  # Wczytaj rekord
  $longdsc='';
  while( <F> )
  {
    chop;
    if( /^\$\$/ ) { last; }

    if( $longdsc ne '' ) { $longdsc .= "$_\n"; next; }

    if( /^\$t (.+)$/ ) { $title = $1; next; }
    if( /^\$d (.+)$/ ) { $dsc = $1; next; }
    if( /^\$u (.+)$/ ) { $url = $1; next; }
    if( /^\$a (.+)$/ ) { $autor = $1; next; }
    if( /^\$e (.+)$/ ) { $email = $1; next; }
    if( /^\s*$/ ) { $longdsc="\n"; next; }
  }
  close F;

  if( $title eq '' ) { return; }

  # Wypisz rekord w tabelce
  print "<table border=3 cellpadding=4>\n";
  print "<tr><th> Tytuł: <td> <big><b>$title</b></big>\n";
  print "<tr><th> Opis: <td> $dsc\n";
  print "<tr><th> URL: <td> ";
  @urls = split( /\s+/, $url );
  for( @urls )
  {
    print "<a href=\"$_\">$_</a>\n";
  }
  print "<tr><th> Autor: <td> ";
  if( $email ne '' ) { print "<a href=\"mailto:$email\">$autor</a>\n"; }
  else               { print "$autor\n"; }
  print "<tr><td colspan=2>\n$longdsc\n";
  print "</table>\n";
}