]> bicyclesonthemoon.info Git - ott/bsta/blob - update.1.pl
fix ONG; fix update; total reset
[ott/bsta] / update.1.pl
1 #!/usr/bin/perl
2
3 # update.pl is generated from update.1.pl.
4 #
5 # update already ONGed frames & stuff
6 #
7 # Copyright (C) 2016, 2017, 2023, 2024  Balthasar SzczepaƄski
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU Affero General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 use strict;
23 use utf8;
24 use Encode::Locale ('decode_argv');
25 use Encode ('encode', 'decode');
26
27 ###PERL_LIB: use lib /botm/lib/bsta
28 use botm_common (
29         'read_data_file',
30         'join_path'
31 );
32 use bsta_lib (
33         'STATE',
34         'ong',
35         'write_index', 'write_static_viewer_page', 'write_static_goto'
36 );
37
38 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
39
40 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta/
41 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
42 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
43 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
44 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
45
46 binmode STDIN,  ':encoding(UTF-8)';
47 binmode STDOUT, ':encoding(UTF-8)';
48 binmode STDERR, ':encoding(UTF-8)';
49 decode_argv();
50
51 my $time = time();
52 srand ($time-$$);
53
54 my %settings  = read_data_file(DATA_SETTINGS_PATH());
55 my %default   = read_data_file(DATA_DEFAULT_PATH());
56 my %state     = read_data_file(DATA_STATE_PATH());
57 my %all_frame_data = ();
58 my %all_words_data = ();
59
60 my $update_goto = 0;
61
62 my $ong_state  = int($state{'state'});
63 my $last_frame = ($ong_state > STATE->{'inactive'}) ? 
64         int($state{'last'}) :
65         0;
66
67 my @list;
68
69 my $fail = 0;
70
71 print $time."\n";
72
73 foreach my $id (@ARGV) {
74         if ($id eq 'all') {
75                 $update_goto = 1;
76                 push @list, 'i';
77                 push @list, 'c';
78                 for (my $f=0; $f<=$last_frame; $f+=1) {
79                         push @list, $f;
80                 }
81         }
82         elsif ($id =~ /^[0-9]+$/) {
83                 $update_goto = 1;
84                 my $f = int($&);
85                 if ($f <= $last_frame) {
86                         push @list, $f
87                 }
88                 else {
89                         print "$f > $last_frame\n";
90                         $fail += 1;
91                 }
92         }
93         elsif ($id =~ /^[ic]$/) {
94                 push @list, $&;
95         }
96         else {
97                 print "$id ???\n";
98                 $fail += 1;
99         }
100 }
101
102 # duplicated - before AND after normal pages!
103 if ($update_goto) {
104         print 'static GOTO';
105         my $r = write_static_goto(
106                 \%state,
107                 \%settings,
108                 '' # \%goto_list
109         );
110         if ($r) {
111                 print " OK\n";
112         }
113         else {
114                 print " FAIL\n";
115                 $fail += 1;
116         }
117 }
118
119 foreach my $id (@list) {
120         print "ONG $id\n";
121         my $r = ong(
122                 $id,   # frame ID
123                 $time, # ONG time
124                 '',    # timer value; not relevant
125                 1,     # update
126                 1,     # print
127                 \%settings,
128                 \%default,
129                 get_frame_data($id),
130                 ''    # %goto_list
131         );
132         unless ($r) {
133                 $fail += 1;
134                 print "ONG FAIL!\n";
135         }
136         make_static_pages($id);
137 }
138
139 # duplicated - before AND after normal pages!
140 if ($update_goto) {
141         print 'static GOTO';
142         my $r = write_static_goto(
143                 \%state,
144                 \%settings,
145                 '' # \%goto_list
146         );
147         if ($r) {
148                 print " OK\n";
149         }
150         else {
151                 print " FAIL\n";
152                 $fail += 1;
153         }
154 }
155
156 print "\n";
157
158
159 sub get_frame_data {
160         (my $id) = @_;
161         
162         unless ($id =~ /^[0-9]+$/) {
163                 return '';
164         }
165         my $f = int($id);
166         
167         unless (($f >= 0) && ($f <= $last_frame)) {
168                 return '';
169         }
170         
171         my $r = $all_frame_data{$f};
172         if (ref ($r)) {
173                 return $r;
174         }
175         
176         my %frame_data = read_data_file(
177                 join_path(PATH_SEPARATOR(), DATA_PATH(), $f)
178         );
179         $all_frame_data{$f} = \%frame_data;
180         return \%frame_data;
181 }
182
183 sub get_words_data {
184         (my $id) = @_;
185         
186         unless ($id =~ /^[0-9]+$/) {
187                 return '';
188         }
189         my $f = int($id);
190         
191         unless (($f >= 0) && ($f <= $last_frame)) {
192                 return '';
193         }
194         
195         my $r = $all_words_data{$f};
196         if (ref ($r)) {
197                 return $r;
198         }
199         
200         my %frame_data = read_data_file(
201                 join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $f),
202                 '', # encoding,
203                 0,  # no header
204                 1,  # header only
205                 1,  # as list; not relevant
206         );
207         $all_words_data{$f} = \%frame_data;
208         return \%frame_data;
209 }
210
211 sub make_static_page {
212         (my $id) = @_;
213         unless ($id =~ /^[0-9]+$/) {
214                 return;
215         }
216         my $f = int($id);
217         unless (
218                 ($f >= 0) && (
219                         ($f < $last_frame-1) || (
220                                 ($ong_state >= STATE->{'ready'}) &&
221                                 ($f <= $last_frame-1)
222                         ) || (
223                                 ($ong_state >= STATE->{'end'}) &&
224                                 ($f <= $last_frame)
225                         )
226                 )
227         ) {
228                 return;
229         }
230         my $r;
231         
232         if (($f == 0) && ($ong_state > STATE->{'inactive'})) {
233                 print 'index';
234                 $r = write_index(
235                         \%state,
236                         \%settings,
237                 );
238                 print (($r) ? " OK\n" : " FAIL\n");
239         }
240         elsif ($f > 0) {
241                 print 'static page '.$f;
242                 $r = write_static_viewer_page (
243                         $f,
244                         \%state,
245                         \%settings,
246                         \%default,
247                         get_frame_data($f),
248                         get_frame_data($f-1),
249                         get_frame_data($f+1),
250                         get_words_data($f)
251                 );
252                 print (($r) ? " OK\n" : " FAIL\n");
253         }
254 }
255
256 sub make_static_pages {
257         (my $id) = @_;
258         unless ($id =~ /^[0-9]+$/) {
259                 return;
260         }
261         my $f = int($id);
262         unless (($f >= 0) && ($f <= $last_frame)) {
263                 return '';
264         }
265         
266         make_static_page($f);
267         make_static_page($f-1);
268         make_static_page($f+1);
269         make_static_page($f);
270 }
271
272 exit $fail;