]> bicyclesonthemoon.info Git - ott/bsta/blob - update.1.pl
1cc780342f24fcf482d8e6b8dda0ca640fe85f3f
[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 use lib '/botm/lib/test-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 foreach my $id (@list) {
103         print "ONG $id\n";
104         my $r = ong(
105                 $id,   # frame ID
106                 $time, # ONG time
107                 '',    # timer value; not relevant
108                 1,     # update
109                 1,     # print
110                 \%settings,
111                 \%default,
112                 get_frame_data($id),
113                 ''    # %goto_list
114         );
115         unless ($r) {
116                 $fail += 1;
117                 print "ONG FAIL!\n";
118         }
119         make_static_pages($id);
120 }
121 if ($update_goto) {
122         print 'static GOTO';
123         my $r = write_static_goto(
124                 \%state,
125                 \%settings,
126                 '' # \%goto_list
127         );
128         if ($r) {
129                 print " OK\n";
130         }
131         else {
132                 print " FAIL\n";
133                 $fail += 1;
134         }
135 }
136
137 print "\n";
138
139
140 sub get_frame_data {
141         (my $id) = @_;
142         
143         unless ($id =~ /^[0-9]+$/) {
144                 return '';
145         }
146         my $f = int($id);
147         
148         unless (($f >= 0) && ($f <= $last_frame)) {
149                 return '';
150         }
151         
152         my $r = $all_frame_data{$f};
153         if (ref ($r)) {
154                 return $r;
155         }
156         
157         my %frame_data = read_data_file(
158                 join_path(PATH_SEPARATOR(), DATA_PATH(), $f)
159         );
160         $all_frame_data{$f} = \%frame_data;
161         return \%frame_data;
162 }
163
164 sub get_words_data {
165         (my $id) = @_;
166         
167         unless ($id =~ /^[0-9]+$/) {
168                 return '';
169         }
170         my $f = int($id);
171         
172         unless (($f >= 0) && ($f <= $last_frame)) {
173                 return '';
174         }
175         
176         my $r = $all_words_data{$f};
177         if (ref ($r)) {
178                 return $r;
179         }
180         
181         my %frame_data = read_data_file(
182                 join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $f),
183                 '', # encoding,
184                 0,  # no header
185                 1,  # header only
186                 1,  # as list; not relevant
187         );
188         $all_words_data{$f} = \%frame_data;
189         return \%frame_data;
190 }
191
192 sub make_static_page {
193         (my $id) = @_;
194         unless ($id =~ /^[0-9]+$/) {
195                 return;
196         }
197         my $f = int($id);
198         unless (
199                 ($f >= 0) && (
200                         ($f < $last_frame-1) || (
201                                 ($ong_state >= STATE->{'ready'}) &&
202                                 ($f <= $last_frame-1)
203                         ) || (
204                                 ($ong_state >= STATE->{'end'}) &&
205                                 ($f <= $last_frame)
206                         )
207                 )
208         ) {
209                 return;
210         }
211         my $r;
212         
213         if (($f == 0) && ($ong_state > STATE->{'inactive'})) {
214                 print 'index';
215                 $r = write_index(
216                         \%state,
217                         \%settings,
218                 );
219                 print (($r) ? " OK\n" : " FAIL\n");
220         }
221         elsif ($f > 0) {
222                 print 'static page '.$f;
223                 $r = write_static_viewer_page (
224                         $f,
225                         \%state,
226                         \%settings,
227                         \%default,
228                         get_frame_data($f),
229                         get_frame_data($f-1),
230                         get_frame_data($f+1),
231                         get_words_data($f)
232                 );
233                 print (($r) ? " OK\n" : " FAIL\n");
234         }
235 }
236
237 sub make_static_pages {
238         (my $id) = @_;
239         unless ($id =~ /^[0-9]+$/) {
240                 return;
241         }
242         my $f = int($id);
243         unless (($f >= 0) && ($f <= $last_frame)) {
244                 return '';
245         }
246         
247         make_static_page($f);
248         make_static_page($f-1);
249         make_static_page($f+1);
250         make_static_page($f);
251 }
252
253 exit $fail;