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

# ong.pl is generated from ong.1.pl.
#
# The ONG bot
#
# 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');

###PERL_LIB: use lib /botm/lib/bsta
use botm_common (
	'read_data_file', 'write_data_file',
	'open_encoded'
);
use bsta_lib (
	'STATE',
	'ong'
);

###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/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;
my %state;
my %goto_list;

my $fh;
my $ongstate;
my $frame;
my $next_ong;
my $ongtime;
my $static_timer;
my $timer;
my $last;
my $r = 0;

$ongtime = int($time / 3600) * 3600; # check time as if it was last full hour!

print $time.' - '.$ongtime."\n";

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

unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
	print "NO STATEFILE\n";
}
else {
	unless (flock($fh, 2)) {
		print "NO STATELOCK\n";
	}
	else {
		%state = read_data_file($fh);
		$ongstate = int($state{'state'});
		print 'state: '.$ongstate."\n";
		unless ($ongstate > STATE->{'inactive'}) {
			print "INACTIVE\n";
		}
		else {
			$next_ong = int($state{'nextong'});
			print 'ongtime: '.$next_ong."\n";
			
			unless ($ongtime >= $next_ong) {
				print "WAIT\n";
			}
			else {
				%settings = read_data_file(DATA_SETTINGS_PATH());
				$static_timer = int($settings{'ongtime'});
				$timer        = int($settings{'dynamicongtime'});
				$last         = int($settings{'last'});
				$frame        = int($state   {'last'})+1;
				
				if (($timer > 0) && ($frame < $last)) {
					$timer = int($timer / ($last - $frame));
				}
				else {
					$timer=0;
				}
				
				if ($static_timer > $timer) {
					$timer = $static_timer;
				}
				$next_ong = $ongtime + ($timer*3600);
				$state{'nextong'} = $next_ong;
				print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
				$state{'ongtime'}=$timer;
				
				if ($ongstate == STATE->{'ready'}) {
					print 'next frame: '.$frame."\n";
					
					$r = ong (
						$frame, # frame ID
						$time,  # ONG time
						$timer, # timer to next ONG
						0,      # update
						1,      # print
						\%settings,
						'',     # %default
						'',     # %frame_data
						''      # %goto_list
					);
					
					if ($r) {
						$state{'last'}   = $frame;
						$state{'state'}  = 1;
						$state{'ip1'}    = '';
						$state{'ip2'}    = '';
						$state{'ip3'}    = '';
						$state{'ongtime'}= $timer;
						print "ONG\n";
					}
					else {
						print "NO ONG\n";
					}
				}
				write_data_file($fh, \%state);
			}
		}
	}
	close ($fh);
}

print "\n";