#!/usr/bin/perl
use locale;
use utf8;
use open ':utf8', ':std';
use Encode qw/ encode /;
use POSIX qw/ strftime setlocale locale_h /;
use Time::Local qw/ timelocal_nocheck /;

use CGI qw/-no_xhtml -compile :standard :cgi-lib *table *div *ol *ul *Tr *td *dl *dd *pre/;

setlocale(LC_TIME,"cs_CZ.utf8");
$LANG="cs_CZ";
$CHARSET="utf-8";

$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í") },      # PROTOKOL
);

# set default values
#
$date='1.1.2000';
$deadline='(never)';
$dead_sec='2147483647';
$ts= strftime('%y%m%d.%H%M', localtime((stat $0)[9]));

# 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);

# 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} ){
		my($y,$m,$d,$H,$M)= split '[-:/\s]', $cfg->{dead};
		$dead_sec= timelocal_nocheck 0,$M,$H,$d,$m-1,$y-1900;
		$deadline= strftime('%d.%m.%Y-%H:%M', localtime $dead_sec);
	}
	if( exists $cfg->{date} ){
		my($y,$m,$d,$H,$M)= split '[-:/\s]', $cfg->{date};
		$date= join '.', 0+$d, 0+$m, $y ;
	}
}

# 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=

# Hlavička HTTP
	header(
		-type=>						"text/html",
		-charset=>				$CHARSET,
#		-cache_control=>	"max-age=60, must-revalidate",
	).
# Hlavička HTML
	start_html(
    -head=> [
      meta({
        -http_equiv=> "Content-Type",
        -content=>    "text/html; charset=$CHARSET",
      }),
    ],
		-title=>			"$pages[$par->{page}] - $cfg->{abbr} $date",
		-author=>			$AUTHOR,
		-lang=>				$LANG,
		-style=> {
			-src=>			'oz.css',
#			-code=>			"BODY { background-image: URL($cfg->{logo}); }",
		},
	).
# Obsah
	start_table({-class=> 'm0'}).
# 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 a odkaz na sponzora
	Tr(
		td({-width=> '100%'},
			hr(),
			div({-style=> 'text-align:center'},
				a({-href=> 'http://'.$cfg->{link}},
					img({
						-src=> $cfg->{logo},
						-alt=> $cfg->{name},
					}),
				)
			),
		),
	).
 
# 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;
}

######################################################################
# načtení stránky ze souboru
sub pg_file {
	my ($file, $text)= @_;
	open IN, "<", $file
		or return dl(dt(($!=~/^No such file/)?"$text":"$file: $!" ));
	$ts= strftime('%y%m%d.%H%M', localtime((stat $file)[9]));

	return join('', (<IN>));
}

######################################################################
# prezentace html výstupu z oorgu
sub pg_oorg {
	my ($file, $text)= @_;
	open IN, "<:encoding(windows-1250)", $file
		or return dl(dt(($!=~/^No such file/)?"$text":"$file: $!" ));
	$ts= strftime('%y%m%d.%H%M', localtime((stat $file)[9]));

	my @s= ();
	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-i;
		push @s, $_;
	}

	return join("\n", start_pre(), @s, end_pre());
}

# vim: set ts=2 sw=2 sts=2 nu noet ai si filetype=perl:
