###RUN_PERL: #!/usr/bin/perl

# /bsta/g
# goto is generated from goto.1.pl.
#
# The frame list
#
# Copyright (C) 2017, 2023, 2024  Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use utf8;
# use Encode::Locale ('decode_argv');
use Encode ('encode', 'decode');

###PERL_LIB: use lib /botm/lib/bsta
use botm_common (
	'read_header_env',
	'read_data_file',
	'url_query_encode', 'url_query_decode',
	'merge_url',
	'join_path',
	'html_entity_encode_dec'
);
use bsta_lib (
	'STATE',
	'fail_method', 'fail_content_type',
	'get_password',
	'print_html_start', 'print_html_end',
	'print_html_head_start', 'print_html_head_end',
	'print_html_body_start', 'print_html_body_end',
	'write_index'
);

###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /

###PERL_CGI_PATH:           CGI_PATH           = /bsta/
###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v

###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list

###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon

binmode STDIN,  ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
# decode_argv();

my $time = time();
srand ($time-$$);

my %http;
my %cgi;
my %settings;
my %state;
my %goto_list;

my $method;
my $password;
my $password_ok;
my $password_query;
my $frame;
my $last_frame;
my $ong_state;
my $line;

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;

if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
	$method = $1;
}
else{
	exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
}

%http = read_header_env(\%ENV);
%cgi = url_query_decode($ENV{'QUERY_STRING'});

if ($method eq 'POST') {
	if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
		my %cgi_post = url_query_decode( <STDIN> );
		%cgi = merge_settings(\%cgi, \%cgi_post);
	}
	# multipart not supported
	else{
		exit fail_content_type($method, $http{'content-type'});
	}
}

$password = get_password(\%cgi);

%settings  = read_data_file(DATA_SETTINGS_PATH());
%state     = read_data_file(DATA_STATE_PATH());
%goto_list = read_data_file(DATA_LIST_PATH());

$last_frame = int($state{'last'});
$ong_state  = int($state{'state'});

$password_ok = ($password eq $settings{'password'});
$password_query = url_query_encode({'p', $settings{'password'}});

print "Content-type: text/html\n";
print "\n";
if($method eq 'HEAD') {
	exit;
}

my $_title        = html_entity_encode_dec($settings{'story'}, 1);
my $_website_name = html_entity_encode_dec(WEBSITE_NAME()    , 1);
my $_base_url     = html_entity_encode_dec(CGI_PATH()        , 1);

print_html_start(\*STDOUT);
print_html_head_start(\*STDOUT);

print '  <title>GOTO &bull; '.$_title.' &bull; '.$_website_name.'</title>'."\n";

print_html_head_end(\*STDOUT);
print_html_body_start(\*STDOUT);

print '   <div id="inst" class="ins">'."\n";

print '    <div id="title">'."\n";
print '     <h1 id="titletext">'.$_title.'</h1>'."\n";
print '    </div>'."\n";

print '   </div>'."\n";
print '   <div id="insb" class="ins">'."\n";

print '    <div id="chat">'."\n";
for ($frame = 0; ; $frame += 1) {
	unless (
		$password_ok || (
			($frame <= $last_frame) &&
			($ong_state >= STATE->{'waiting'})
		)
	) {
		last;
	}
	my $title;
	my $ongtime;
	my @time_tab;
	my $time_text;
	my $timer_color;
	my $frame_text;
	my $viewer_url;
	
	$ongtime = $goto_list{'ongtime-'.$frame};
	$title   = $goto_list{'title-'  .$frame};
	if (($ongtime eq '') && ($title eq '')) {
		my $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
		my %frame_data = read_data_file($frame_data_path);
		$ongtime = $frame_data{'ongtime'};
		$title   = $frame_data{'title'};
		unless (keys %frame_data) {
			last;
		}
	}
	
	if ($ongtime ne '') {
		@time_tab = gmtime($ongtime);
		$time_text = sprintf(
			'%02d.%02d.%02d %02d:%02d',
			$time_tab[3],
			$time_tab[4]+1,
			$time_tab[5]-100,
			$time_tab[2],
			$time_tab[1]
		);
	}
	else {
		$time_text = (($frame <= $last_frame) && ($ong_state >= STATE->{'waiting'})) ?
			'EE.EE.EE EE:EE' : '--.--.-- --:--';
	}
	if ($title eq '') {
		$title = '_';
	}
	$timer_color = (($frame > $last_frame) || ($ong_state < STATE->{'waiting'})) ?
		'cz' : (
			(($frame == $last_frame) && ($ong_state < STATE->{'ready'})) ?
				'ni' : 'br'
		);
	$frame_text = sprintf('%03d',$frame);
	$viewer_url = merge_url(
		{'path' => CGI_VIEWER_PATH()},
		{'path' => $frame}
	);
	if ($password_ok) {
		$viewer_url = merge_url($viewer_url, {'query' => $password_query, 'append_query' => 1});
	}
	
	my $_viewer_url = html_entity_encode_dec($viewer_url, 1);
	my $_title      = html_entity_encode_dec($title     , 1);
	
	print '     <span class="'.$timer_color.'">'.$frame_text.'</span> '.$time_text.' <a href="'.$_viewer_url.'">'.$_title.'</a><br>'."\n";
}
print '    </div>'."\n";

print '    <div id="underlinks">'."\n";
print '     <a href="'.$_base_url.'">BSTA</a>'."\n";
print '    </div>'."\n";

print '   </div>'."\n";

print_html_body_end(\*STDOUT, $ong_state == STATE->{'inactive'});
print_html_end(\*STDOUT);
