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

# ong.pl is generated from ong.1.pl.
#
# The ONG bot
#
# Copyright (C) 2016, 2017, 2023  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 warnings;
###PERL_LIB: use lib /botm/lib/bsta
use bsta_lib qw(entityencode readdatafile writedatafile urlencode);
use File::Copy;

###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_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list

my %framedata;
my %nextframedata;
my %default;
my %settings;
my %state;
my %gotolist;

my $statefile;
my $ongstate;
my $frame;
my $nextong;
my $inpath;
my $outpath;
my $ongtime;
my $static;
my $dynamic;
my $last;

my $time = time();
srand ($time-$$);
$ongtime = int($time / 3600) * 3600;

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

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


if (open ($statefile,"+<",DATA_STATE_PATH)){
	if (flock($statefile,2)) {
		%state=readdatafile($statefile);
		$ongstate=int($state{'state'});
		print 'state: '.$ongstate."\n";
		if($ongstate > 0) {
			$nextong = int($state{'nextong'});
			print 'ongtime: '.$nextong."\n";
			
			if($ongtime >= $nextong) {
				%settings=readdatafile(DATA_SETTINGS_PATH);
				$static=int($settings{'ongtime'});
				$dynamic=int($settings{'dynamicongtime'});
				$last=int($settings{'last'});
				$frame=int($state{'last'})+1;
				
				if($dynamic > 0 && $frame < $last) {
					$dynamic = int($dynamic/($last-$frame));
				}
				else {
					$dynamic=0;
				}
				
				if($static>$dynamic){
					$dynamic=$static;
				}
				$nextong=$ongtime+($dynamic*3600);
				$state{'nextong'}=$nextong;
				print 'next ongtime: '.$nextong.' (+'.$dynamic.")\n";
				$state{'ongtime'}=$dynamic;
				
				if($ongstate == 2) {
					print 'next frame: '.$frame."\n";
					%framedata=readdatafile(DATA_PATH.$frame);
					%default=readdatafile(DATA_DEFAULT_PATH);
					%gotolist=readdatafile(DATA_LIST_PATH);
					
					$framedata{'ongtime'}=$time;
					$framedata{'timer'}=$dynamic;
					$gotolist{'title-'.$frame}=$framedata{'title'};
					$gotolist{'ongtime-'.$frame}=$framedata{'ongtime'};
					writedatafile(DATA_PATH.$frame,%framedata);
					writedatafile(DATA_LIST_PATH,%gotolist);
					
					foreach my $ind (keys %default) {
						unless(defined($framedata{$ind})){
							$framedata{$ind}=$default{$ind};
						}
						unless(defined($nextframedata{$ind})){
							$nextframedata{$ind}=$default{$ind};
						}
					}
					
					$inpath = DATA_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
					$outpath = WWW_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
					
					print $inpath.' -> '.$outpath."\n";
					
					# ALSO-ATTACHMENTS!!! (here)
					
					if(copy ($inpath, $outpath)) {
						$state{'last'}=$frame;
						$state{'state'}=1;
						$state{'ip1'}='';
						$state{'ip2'}='';
						$state{'ip3'}='';
						$state{'ongtime'}=$dynamic;
						print "ONG\n";
					}
					else {
						print "NO ONG\n";
					}
				}
				writedatafile($statefile,%state);
			}
			else {
				print "WAIT\n";
			}
		}
		else {
			print "INACTIVE\n";
		}
	}
	else {
		print "NO STATELOCK\n";
	}
	close ($statefile);
}
else {
	print "NO STATEFILE\n";
}
print "\n";