]> bicyclesonthemoon.info Git - ott/bsta/blob - update.1.pl
Generate static viewer pages
[ott/bsta] / update.1.pl
1 ###RUN_PERL: #!/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'
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 $ong_state  = int($state{'state'});
61 my $last_frame = ($ong_state > STATE->{'inactive'}) ? 
62         int($state{'last'}) :
63         0;
64
65 my @list;
66
67 my $fail = 0;
68
69 print $time."\n";
70
71 foreach my $id (@ARGV) {
72         if ($id eq 'all') {
73                 push @list, 'i';
74                 push @list, 'c';
75                 for (my $f=0; $f<=$last_frame; $f+=1) {
76                         push @list, $f;
77                 }
78         }
79         elsif ($id =~ /^[0-9]+$/) {
80                 my $f = int($&);
81                 if ($f <= $last_frame) {
82                         push @list, $f
83                 }
84                 else {
85                         print "$f > $last_frame\n";
86                         $fail += 1;
87                 }
88         }
89         elsif ($id =~ /^[ic]$/) {
90                 push @list, $&;
91         }
92         else {
93                 print "$id ???\n";
94                 $fail += 1;
95         }
96 }
97
98 foreach my $id (@list) {
99         print "ONG $id\n";
100         my $r = ong(
101                 $id,   # frame ID
102                 $time, # ONG time
103                 '',    # timer value; not relevant
104                 1,     # update
105                 1,     # print
106                 \%settings,
107                 \%default,
108                 get_frame_data($id),
109                 ''    # %goto_list
110         );
111         unless ($r) {
112                 $fail += 1;
113                 print "ONG FAIL!\n";
114         }
115         make_static_pages($id);
116 }
117 print "\n";
118
119
120 sub get_frame_data {
121         (my $id) = @_;
122         
123         unless ($id =~ /^[0-9]+$/) {
124                 return '';
125         }
126         my $f = int($id);
127         
128         unless (($f >= 0) && ($f <= $last_frame)) {
129                 return '';
130         }
131         
132         my $r = $all_frame_data{$f};
133         if (ref ($r)) {
134                 return $r;
135         }
136         
137         my %frame_data = read_data_file(
138                 join_path(PATH_SEPARATOR(), DATA_PATH(), $f)
139         );
140         $all_frame_data{$f} = \%frame_data;
141         return \%frame_data;
142 }
143
144 sub get_words_data {
145         (my $id) = @_;
146         
147         unless ($id =~ /^[0-9]+$/) {
148                 return '';
149         }
150         my $f = int($id);
151         
152         unless (($f >= 0) && ($f <= $last_frame)) {
153                 return '';
154         }
155         
156         my $r = $all_words_data{$f};
157         if (ref ($r)) {
158                 return $r;
159         }
160         
161         my %frame_data = read_data_file(
162                 join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $f),
163                 '', # encoding,
164                 0,  # no header
165                 1,  # header only
166                 1,  # as list; not relevant
167         );
168         $all_words_data{$f} = \%frame_data;
169         return \%frame_data;
170 }
171
172 sub make_static_page {
173         (my $id) = @_;
174         unless ($id =~ /^[0-9]+$/) {
175                 return;
176         }
177         my $f = int($id);
178         unless (($f >= 0) && ($f <= $last_frame)) {
179                 return;
180         }
181         my $r;
182         
183         if (($f == 0) && ($ong_state > STATE->{'inactive'})) {
184                 print 'index';
185                 $r = write_index(
186                         \%state,
187                         \%settings,
188                 );
189                 print (($r) ? " OK\n" : " FAIL\n");
190         }
191         elsif ($f > 0) {
192                 print 'static page '.$f;
193                 $r = write_static_viewer_page (
194                         $f,
195                         \%state,
196                         \%settings,
197                         \%default,
198                         get_frame_data($f),
199                         get_frame_data($f-1),
200                         get_frame_data($f+1),
201                         get_words_data($f)
202                 );
203                 print (($r) ? " OK\n" : " FAIL\n");
204         }
205 }
206
207 sub make_static_pages {
208         (my $id) = @_;
209         unless ($id =~ /^[0-9]+$/) {
210                 return;
211         }
212         my $f = int($id);
213         unless (($f >= 0) && ($f <= $last_frame)) {
214                 return '';
215         }
216         
217         make_static_page($f);
218         make_static_page($f-1);
219         make_static_page($f+1);
220         make_static_page($f);
221 }
222
223 exit $fail;