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

# /bsta/v
# viewer is generated from viewer.1.pl.
#
# The viewer interface
#
# Copyright (C) 2016, 2017, 2019, 2020, 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 (
	'HTTP_STATUS',
	'read_header_env',
	'url_query_decode',
	'join_path',
	'open_encoded', '_x_encoded',
	'http_header_status',
	'merge_url'
);
use bsta_lib (
	'STATE', 'TEXT_MODE', 'INTF_STATE',
	'fail_method', 'fail_content_type', 'redirect',
	'get_remote_addr', 'get_frame', 'get_password',
	'merge_settings',
	'print_viewer_page',
	'write_index', 'write_static_goto', 'write_static_viewer_page',
	'ong',
	'read_frame_data', 'read_default', 'read_noaccess',
	'read_words_list', 'read_settings', 'read_story', 'read_goto',
	'read_state', 'write_state',
	'get_page_file'
);

###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /

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

###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state

###PERL_WWW_PATH:           WWW_PATH           = /botm/www/

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 %frame_data;
my %prev_frame_data;
my %next_frame_data;
my %default;
my %settings;
my %state;
my %new_state;
my %goto_list;
my %words_data;

my $method;
my $frame;
my $prev_frame_data_path;
my $next_frame_data_path;
my $password;
my $password_ok;
my $IP;
my $access;
my $timer;
my $timer_unlocked;
my $fh;
my $show_command;
my $ongtime;
my $goto;
my $text_mode;
my $words_page;
my $words_data_path;
my $no_cgi;
my $force_redirect;

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'});
	}
}
$no_cgi = (scalar (keys %cgi) == 0);

$IP = get_remote_addr();
$frame = get_frame(\%cgi);
$password = get_password(\%cgi);

%settings  = read_settings();
%default   = read_default();

if ($frame >= 0) {
	%frame_data= read_frame_data($frame);
}

$password_ok = ($password eq $settings{'password'});

# state & activation logic
if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
	if (flock($fh, 2)) {
		
		%state = read_state($fh);
		
		if ($frame < 0) {
			$frame = int($state{'last'}) + $frame +1;
			if ($frame >= 0) {
				$force_redirect = 1;
				%frame_data = read_frame_data($frame);
			}
		}
		
		if (
			(int($state{'state'}) == STATE->{'waiting'}) &&
			($frame == int($state{'last'})) &&
			($method ne 'HEAD') &&
			(!$password_ok)
		) {
			# register IP for progress
			my %new_state = %state;
			unless (
				($state{'ip1'} eq $IP) ||
				($state{'ip2'} eq $IP) ||
				($state{'ip3'} eq $IP)
			)
			{
				if ($state{'ip1'} eq '') {
					$new_state{'ip1'} = $IP;
				}
				elsif ($state{'ip2'} eq '') {
					$new_state{'ip2'} = $IP;
				}
				elsif ($state{'ip3'} eq '') {
					$new_state{'ip3'} = $IP;
					$new_state{'state'} = STATE->{'ready'};
				}
				else {
					$new_state{'state'} = STATE->{'ready'};
				}
				if ($new_state{'state'} == STATE->{'ready'}) {
					write_static_goto(\%new_state, \%settings, '');
					write_static_viewer_page(
						$frame-1,
						\%new_state,
						\%settings,
						\%default,
						'', # frame data
						'', # prev frame data
						\%frame_data, # next frame data,
						'' # words data
					);
				}
				write_state($fh, \%new_state);
			}
		}
		elsif (
			(int($state{'state'}) == STATE->{'inactive'}) &&
			($frame == 1) &&
			(!$password_ok)
		) {
			# ready to activate?
			# NOTE: at this point frame 0 is already ONGed.
			my %story;
			my $ong_time = int($settings{'firstongtime'});
			my $r;
			
			%story     = read_story();
			%goto_list = read_goto();
			
			if (
				(int($story{'state'}) == INTF_STATE->{'>|'} ) &&
				(int($story{'pass'}) == 1)
			) {
				# conditions met; ACTIVATE!
				
				# set initial state
				$state{'state'} = STATE->{'waiting'};
				$state{'last'}  = 1; 
				$state{'ip1'}   = '0.0.0.0';
				$state{'ip2'}   = '0.0.0.0';
				$state{'ip3'}   = '';
				$state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
				$state{'ongtime'} = $ong_time;
				
				# prepare to ONG frame 1
				
				$r = ong(
					1,         # frame ID
					$time,     # ONG time,
					$ong_time, # timer
					0,         # update
					0,         # print
					\%settings,
					\%default,
					\%frame_data,
					\%goto_list
				);
				if ($r) {
					$r = write_index(\%state, \%settings);
				}
				if ($r) {
					$r = write_static_goto(\%state, \%settings, \%goto_list);
				}
				if ($r) {
					$r = write_state($fh, \%state);
				}
				unless ($r) {
					# FAILED ONG! Story as if it was inactive!
					$state{'state'} = STATE->{'inactive'};
				}
			}
		}
	}
	else {
		# FAILED GET STATE! Story as if it was inactive!
		$state{'state'} = STATE->{'inactive'};
	}
	close ($fh);
}
else {
	$state{'state'} = STATE->{'inactive'};
}

$access = (
	$password_ok || (
		(int($state{'state'}) >= STATE->{'waiting'}) &&
		($frame <= int($state{'last'})) &&
		($frame >= 0)
	)
);

if ($access) {
	if ($no_cgi) {
		# no CGI - static page is OK
		if ($frame == 0) {
			exit redirect($method, CGI_PATH(), HTTP_STATUS->{'see_other'});
		}
		elsif ($frame < int($state{'last'})) {
			my $page_file = get_page_file($frame, \%frame_data, \%settings);
			if (_x_encoded('-f',
				join_path(PATH_SEPARATOR(), WWW_PATH() , $page_file)
			)) {
				my $static_url = merge_url(
					{'path' => CGI_PATH()},
					{'path' => $page_file}
				);
				exit redirect($method, $static_url, HTTP_STATUS->{'see_other'});
			}
		}
	}
	if ($force_redirect) {
		my $redirect_url = merge_url(
			{'path' => CGI_VIEWER_PATH()},
			{'path' => $frame}
		);
		unless ($no_cgi) {
			delete $cgi{'f'}; # to avoid infinite loop
			$redirect_url = merge_url(
				{'path' => $redirect_url},
				{'query' => \%cgi}
			);
		}
		exit redirect($method, $redirect_url, HTTP_STATUS->{'see_other'});
	}
	
	if ($frame > 0) {
		%prev_frame_data = read_frame_data($frame-1, \%default);
	}
	else {
		%prev_frame_data = %default;
	}
	%next_frame_data = read_frame_data($frame+1, \%default);
	%frame_data      = merge_settings(\%default, \%frame_data);
}
else {
	# replace frame data with fail state replacement
	%frame_data = read_noaccess(\%default);
}

$timer   = int($state{'nextong'}) - $time;
$ongtime = int($state{'ongtime'});
if($ongtime == 0) {
	$ongtime = int($settings{'ongtime'})
}

$show_command = ($timer < ($ongtime*3600/3));
if ($state{'state'} >= STATE->{'ready'}) {
	$timer_unlocked = 3;
}
elsif ($state{'ip3'} ne '') {
	$timer_unlocked = 3;
}
elsif ($state{'ip2'} ne '') {
	$timer_unlocked = 2;
}
elsif ($state{'ip1'} ne '') {
	$timer_unlocked = 1;
}
else {
	$timer_unlocked = 0;
}

$text_mode = int($cgi{'b'});
if($text_mode > TEXT_MODE->{'words'}) {
	$text_mode = TEXT_MODE->{'normal'};
}
$words_page = int($cgi{'i'});
$goto = int($cgi{'g'});

%words_data = read_words_list(
	$frame,
	($text_mode != TEXT_MODE->{'words'})
);

if (!$access) {
	print http_header_status(HTTP_STATUS->{'forbidden'});
}
print "Content-type: text/html; charset=UTF-8\n\n";
if($method eq 'HEAD') {
	exit;
}

print_viewer_page (
	\*STDOUT,
	{
		'launch'        => 0,
		'frame'         => $frame,
		'access'        => $access,
		'password_ok'   => $password_ok,
		'timer_unlocked'=> $timer_unlocked,
		'timer'         => $timer,
		'static'        => 0,
		'show_command'  => $show_command,
		'text_mode'     => $text_mode,
		'words_page'    => $words_page,
		'goto'          => $goto
	},
	\%state,
	\%settings,
	\%frame_data,
	$access ? \%prev_frame_data : \%frame_data,
	$access ? \%next_frame_data : \%frame_data,
	\%words_data,
);
