#!/usr/bin/perl

# update.pl is generated from update.1.pl.
#
# update already ONGed frames & stuff
#
# Copyright (C) 2016, 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');

use lib '/botm/lib/test-bsta';
use botm_common (
	'read_data_file',
	'join_path'
);
use bsta_lib (
	'STATE',
	'ong',
	'write_index', 'write_static_viewer_page', 'write_static_goto'
);

###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /

###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/

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

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

my %settings  = read_data_file(DATA_SETTINGS_PATH());
my %default   = read_data_file(DATA_DEFAULT_PATH());
my %state     = read_data_file(DATA_STATE_PATH());
my %all_frame_data = ();
my %all_words_data = ();

my $update_goto = 0;

my $ong_state  = int($state{'state'});
my $last_frame = ($ong_state > STATE->{'inactive'}) ? 
	int($state{'last'}) :
	0;

my @list;

my $fail = 0;

print $time."\n";

foreach my $id (@ARGV) {
	if ($id eq 'all') {
		$update_goto = 1;
		push @list, 'i';
		push @list, 'c';
		for (my $f=0; $f<=$last_frame; $f+=1) {
			push @list, $f;
		}
	}
	elsif ($id =~ /^[0-9]+$/) {
		$update_goto = 1;
		my $f = int($&);
		if ($f <= $last_frame) {
			push @list, $f
		}
		else {
			print "$f > $last_frame\n";
			$fail += 1;
		}
	}
	elsif ($id =~ /^[ic]$/) {
		push @list, $&;
	}
	else {
		print "$id ???\n";
		$fail += 1;
	}
}

foreach my $id (@list) {
	print "ONG $id\n";
	my $r = ong(
		$id,   # frame ID
		$time, # ONG time
		'',    # timer value; not relevant
		1,     # update
		1,     # print
		\%settings,
		\%default,
		get_frame_data($id),
		''    # %goto_list
	);
	unless ($r) {
		$fail += 1;
		print "ONG FAIL!\n";
	}
	make_static_pages($id);
}
if ($update_goto) {
	print 'static GOTO';
	my $r = write_static_goto(
		\%state,
		\%settings,
		'' # \%goto_list
	);
	if ($r) {
		print " OK\n";
	}
	else {
		print " FAIL\n";
		$fail += 1;
	}
}

print "\n";


sub get_frame_data {
	(my $id) = @_;
	
	unless ($id =~ /^[0-9]+$/) {
		return '';
	}
	my $f = int($id);
	
	unless (($f >= 0) && ($f <= $last_frame)) {
		return '';
	}
	
	my $r = $all_frame_data{$f};
	if (ref ($r)) {
		return $r;
	}
	
	my %frame_data = read_data_file(
		join_path(PATH_SEPARATOR(), DATA_PATH(), $f)
	);
	$all_frame_data{$f} = \%frame_data;
	return \%frame_data;
}

sub get_words_data {
	(my $id) = @_;
	
	unless ($id =~ /^[0-9]+$/) {
		return '';
	}
	my $f = int($id);
	
	unless (($f >= 0) && ($f <= $last_frame)) {
		return '';
	}
	
	my $r = $all_words_data{$f};
	if (ref ($r)) {
		return $r;
	}
	
	my %frame_data = read_data_file(
		join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $f),
		'', # encoding,
		0,  # no header
		1,  # header only
		1,  # as list; not relevant
	);
	$all_words_data{$f} = \%frame_data;
	return \%frame_data;
}

sub make_static_page {
	(my $id) = @_;
	unless ($id =~ /^[0-9]+$/) {
		return;
	}
	my $f = int($id);
	unless (
		($f >= 0) && (
			($f < $last_frame-1) || (
				($ong_state >= STATE->{'ready'}) &&
				($f <= $last_frame-1)
			) || (
				($ong_state >= STATE->{'end'}) &&
				($f <= $last_frame)
			)
		)
	) {
		return;
	}
	my $r;
	
	if (($f == 0) && ($ong_state > STATE->{'inactive'})) {
		print 'index';
		$r = write_index(
			\%state,
			\%settings,
		);
		print (($r) ? " OK\n" : " FAIL\n");
	}
	elsif ($f > 0) {
		print 'static page '.$f;
		$r = write_static_viewer_page (
			$f,
			\%state,
			\%settings,
			\%default,
			get_frame_data($f),
			get_frame_data($f-1),
			get_frame_data($f+1),
			get_words_data($f)
		);
		print (($r) ? " OK\n" : " FAIL\n");
	}
}

sub make_static_pages {
	(my $id) = @_;
	unless ($id =~ /^[0-9]+$/) {
		return;
	}
	my $f = int($id);
	unless (($f >= 0) && ($f <= $last_frame)) {
		return '';
	}
	
	make_static_page($f);
	make_static_page($f-1);
	make_static_page($f+1);
	make_static_page($f);
}

exit $fail;
