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

# update.pl is generated from update.1.pl.
#
# update already ONGed frames & stuff
#
# Copyright (C) 2016, 2017, 2023, 2024, 2025  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 (
);
use bsta_lib (
	'STATE',
	'ong',
	'write_index', 'write_static_viewer_page', 'write_static_goto',
	'read_frame_data', 'read_default', 'read_words_list',
	'read_settings', 'read_state'
);

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_settings();
my %default   = read_default();
my %state     = read_state();
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." update\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]+$/) {
		my $f = int($&);
		if ($f <= $last_frame) {
			$update_goto = 1;
			push @list, $f;
		}
		else {
			print "$f > $last_frame\n";
			$fail += 1;
		}
	}
	elsif ($id =~ /^([0-9]*)\-([0-9]+)$/) {
		my $f_start = int($1); # int('') == 0
		my $f_end = int($2);
		if ($f_start > $f_end) {
			print "$f_start > $f_end\n";
			$fail += 1;
			next;
		}
		for (my $f=$f_start; $f<=$f_end; $f+=1) {
			if ($f <= $last_frame) {
				$update_goto = 1;
				push @list, $f;
			}
			else {
				print "$f > $last_frame\n";
				$fail += 1;
			}
		}
	}
	elsif ($id =~ /^([0-9]+)\+$/) {
		my $f_start = int($1);
		if ($f_start > $last_frame) {
			print "$f_start > $last_frame\n";
			$fail += 1;
			next;
		}
		$update_goto = 1;
		for (my $f=$f_start; $f<=$last_frame; $f+=1) {
			push @list, $f;
		}
	}
	elsif ($id =~ /^[ic]$/) {
		push @list, $&;
	}
	else {
		print "$id ???\n";
		$fail += 1;
	}
}

# duplicated - before AND after normal pages!
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;
	}
}

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

# duplicated - before AND after normal pages!
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 (($fail > 0) ? "update FAIL\n\n" : "update OK\n\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_frame_data($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 %words_data = read_words_list($f, 1);
	$all_words_data{$f} = \%words_data;
	return \%words_data;
}

sub make_static_page {
	(my $id) = @_;
	unless ($id =~ /^[0-9]+$/) {
		return;
	}
	my $f = int($id);
	unless (
		($f >= 0) && (
			($f < $last_frame) || (
				($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;
