#!/usr/bin/perl
use locale;
use CGI qw/-no_xhtml -compile :standard :cgi-lib *table *div *ol *ul *Tr *td *dl *dd *pre/;

$author='zdenek@precek.cz';

@pages= qw/ EDITACE NOVINKY ROZPIS POKYNY STARTOVKA-KATEGORIE STARTOVKA-ODDÍLY VÝSLEDKY MEZIČASY PROTOKOL/;
@funcs= (
 undef,
 sub{ pg_file( $cfg->{fnov}, "Zatím nic") },       # NOVINKY
 sub{ pg_file( $cfg->{froz}, "Zatím není") },      # ROZPIS
 sub{ pg_file( $cfg->{fpok}, "Zatím nejsou") },    # POKYNY
 sub{ pg_oorg( $cfg->{fstk}, "Zatím není") },    # STARTOVKA-KAT
 sub{ pg_oorg( $cfg->{fsto}, "Zatím není") },    # STARTOVKA-ODD
 sub{ pg_oorg( $cfg->{fvyk}, "Budou po závodě") }, # VÝSLEDKY
 sub{ pg_oorg( $cfg->{fvym}, "Budou po závodě") }, # MEZIČASY
 sub{ pg_file( $cfg->{fpro}, "Zatím není") },      # ROZPIS
);
$dead="2147483647";

# start data processing
#

# save CGI parameters
#
param( 'page','1' ) unless param( 'page' );
$par= Vars;
$par->{page}= 0 if $par->{page}=~ /^Edit$/;
$par->{page}= 1 unless $par->{page}=~ /\d+/ && ($par->{page} <= $#pages);
$ts=`date -r oz.pl '+<%y%m%d.%H%M>'`;

# import the configuration file
#
if( open IN, "oz.cfg" ) {
 restore_parameters( \*IN ) ;
 close IN;
 $cfg= Vars;

 if( exists $cfg->{trat} ){
  @kat= ();
  @trat= split "\0", $cfg->{trat};
	for (@trat) {
    push @kat, (split)[0];
  }
 }
 if( exists $cfg->{dead} ){
  $dead= `date -d "$cfg->{dead}" +%s`;
  chomp($dead);
 }
}

# cleanup the CGI parameters
if( open IN, "/dev/null" ) {
  restore_parameters( \*IN ) ;
	close IN;
}

# generování HTML
#
print
 do_head()."\n".
 do_page()."\n".
 do_tail()."\n";

######################################################################
# funkce
######################################################################

######################################################################
# tělo stránky
#
sub do_page {
my $s="";

# debug: konfigurace
#my $c=""; for (sort keys %$cfg) { $c.= Tr(th($_),td($cfg->{$_})); };
#$s.= Tr(td(dl(dt('konfigurace'),dd(table({-class=>"vysledky"},$c)))));

# debug: parametry
#my $p=""; for (sort keys %$par) { $p.= Tr(th($_),td($par->{$_})); };
#$s.= Tr(td(dl(dt('parametry'),dd(table({-class=>"vysledky"},$p)))));

# data stránky
$s.= Tr(td($funcs[$par->{page}]()));

 return $s;
}

######################################################################
# Hlavičky HTTP, HTML a stránky
#
sub do_head {
	my $s=`LC_ALL=cs_CZ.utf8 date -d $cfg->{date} +"$pages[$par->{page}] - $cfg->{abbr} %x"`;
	chomp( $s ); $s=

# Hlavička HTTP
 header(
	-type=>        'text/html',
	-charset=>     'UTF-8',
 ).
# Hlavička HTML
 start_html(
	-title=>       $s,
  -author=>      $author,
	-lang=>        'cs',
	-encoding=>    'UTF-8',
  -head=> [
    meta({
     -http_equiv=> 'Content-Type',
     -content=>    'text/html; charset=UTF-8',
    }),
   ],
	-style=> {
	 -src=>        'oz.css',
   -code=>       '@media print { .noprint { display:none; } }',
	},
 ).
# Obsah
 start_table(
  {-style=> 'width:760px;margin-left:auto;margin-right:auto;background:#FFFFAA;'},
 ).
# Hlavička stránky
 Tr(
  td({-width=> '100%'},
	 div({-style=> 'vertical-align:top;font-size:120%;font-weight:bold;'},
  	 div({-style=> 'float:left;'},
      a({-href=> '../index.html'},
  		 img({
  			 -src=> 'img/scp.gif',
  			 -alt=> 'OK Sparta Praha',
  			}),
  		)
     ),
  	 div({-style=> 'float:right'},
      a({-href=> 'http://www.oksparta.cz'},
       'OK Sparta Praha',
  		)
     ),
  	 div({-style=> 'text-align:center'},
      a({-href=> 'http://psob.dig.cz/'},
  		 'Pražský svaz orientačních sportů, Na&nbsp;Děkance, Praha 4',
  		)
  	 ),
   ),
  ),
 ).
# logo
 Tr(
  td({-width=> '100%'},
   hr(),
   div({-style=> 'text-align:center'},
    a({-href=> 'http://www.enacon.cz'},
     img({
      -src=> 'img/enacon.gif',
      -alt=> 'Enacon&nbsp;Cup',
     }),
  	)
   ),
  ),
 ).
 
# navigace
 start_Tr.
 start_td.
 start_div({-class=>'noprint'}).
 hr;
 for ( 1 .. $#pages ) {
  $s.= 
   a({-href=> "?page=$_"},
    $pages[$_],
   ).'&nbsp;';
 };
# nadpis
 $s.=
  end_div.
  hr.
  h1($pages[$par->{page}]).
  end_td.
  end_Tr;
 return $s;
}

######################################################################
# Konec stránky a dokumentu
#
sub do_tail { return
 Tr(td(hr,
 div({-class=>'noprint'},
# W3C ikony
	 div({-style=> "margin-left:0;margin-right:auto;text-align:left;float:left;vertical-align:top"},
		 '
		 <a href="http://validator.w3.org/check?uri=referer">
			<img src="https://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
			 style="border:0;width:88px;height:31px">
		 </a>
		 <a href="http://jigsaw.w3.org/css-validator/check/referer">
			<img src="https://jigsaw.w3.org/css-validator/images/vcss" alt="Ověřit CSS!"
			 style="border:0;width:88px;height:31px">
		 </a>
		 '
	 ),
# moje ikony
	 div({-style=> "margin-left:auto;margin-right:0;text-align:right;float:right;vertical-align:top"},
		 a({-href=> "http://www.vim.org"},
			img({-src=> "img/html_by_vim.gif",
					 -alt=> "vytvořeno pomocí editoru &quot;vim&quot;",
				 -style=> "border:0;width:88px;height:31px"},
			),
		 ),
		 img({-src=> "img/anybrowser.gif",
					-alt=> "optimalizováno pro jakýkoli prohlížeč",
				-style=> "border:0;width:88px;height:31px"},
		 ),
	 ),
 ),
 p({-style=> "clear:both;text-align:right;font-size:x-small;"}, 'ing Zdeněk Přeček', $ts),
 )).
# konec stránky
 end_table.
# konec HTML
 end_html;
}

######################################################################
# prezentace html výstupu z oorgu
sub pg_oorg {
 my ($file, $text)= @_;
 open IN, $file or return dl(dt($text));
 open IN, "/usr/bin/iconv -f WINDOWS-1250 -t UTF-8 $file |" or return dl(dt("'/usr/bin/iconv -f WINDOWS-1250 -t UTF-8 $file' failure" ));
 $ts=`date -r $file '+<%y%m%d.%H%M>'`;
 my @s= (
   start_pre(),
  );
 my $skip= 1;
 while(<IN>){
  if( m-^<[/]?pre>-i ) {
   $skip=  not $skip;
   next ;
  }
  next if $skip;
  chomp;
  s-(\d\S+/1\*)-<u>$1</u>-g;
  s-</u>(\s+)<u>-$1-g;
  s-^(<I>\s+)17-$1   17-;
	push @s, $_;
 }
 return join("\n", @s, end_pre());
}

######################################################################
# načtení stránky ze souboru
sub pg_file {
 my ($file, $text)= @_;
 open IN, $file or return dl(dt($text));
 $ts=`date -r $file '+<%y%m%d.%H%M>'`;
 return join("", (<IN>));
}

