]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - update.1.pl
move merge_settings to common library
[ott/bsta] / update.1.pl
index e2e59f67cff6c14b007ff2a4a2191a8b60a1b779..8cfba90fa447128fd0229b08e492037186888b16 100644 (file)
@@ -1,10 +1,10 @@
-###RUN_PERL: #!/usr/bin/perl
+#!/usr/bin/perl
 
 # update.pl is generated from update.1.pl.
 #
 # update already ONGed frames & stuff
 #
-# Copyright (C) 2016, 2017, 2023  Balthasar Szczepański
+# 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
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
-#use warnings;
+use utf8;
+use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
 ###PERL_LIB: use lib /botm/lib/bsta
-use bsta_lib qw(readdatafile writedatafile);
-use File::Copy;
+use botm_common (
+);
+use bsta_lib (
+       'STATE',
+       'ong',
+       'write_index', 'write_static_viewer_page', 'write_static_goto',
+       'read_frame_data', 'read_default', 'read_words_list',
+       'read_settings', 'read_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  = read_settings();
+my %default   = read_default();
+my %state     = read_state();
+my %all_frame_data = ();
+my %all_words_data = ();
 
-###PERL_DATA_PATH:      DATA_PATH      = /botm/data/bsta/
-###PERL_DATA_LIST_PATH: DATA_LIST_PATH = /botm/data/bsta/list
+my $update_goto = 0;
 
-my %framedata;
-my %gotolist;
+my $ong_state  = int($state{'state'});
+my $last_frame = ($ong_state > STATE->{'inactive'}) ? 
+       int($state{'last'}) :
+       0;
 
-my $frame;
+my @list;
 
-%gotolist=readdatafile(DATA_LIST_PATH);
+my $fail = 0;
 
-for($frame=0; ; ++$frame) {
-       %framedata=readdatafile(DATA_PATH.$frame);
-       if($framedata{'ongtime'} eq '') {
-               last;
+print $time."\n";
+
+foreach my $id (@ARGV) {
+       if ($id eq 'all') {
+               $update_goto = 1;
+               push @list, 'i';
+               push @list, 'c';
+               for (my $f=0; $f<=$last_frame; $f+=1) {
+                       push @list, $f;
+               }
+       }
+       elsif ($id =~ /^[0-9]+$/) {
+               $update_goto = 1;
+               my $f = int($&);
+               if ($f <= $last_frame) {
+                       push @list, $f
+               }
+               else {
+                       print "$f > $last_frame\n";
+                       $fail += 1;
+               }
+       }
+       elsif ($id =~ /^[ic]$/) {
+               push @list, $&;
+       }
+       else {
+               print "$id ???\n";
+               $fail += 1;
+       }
+}
+
+# duplicated - before AND after normal pages!
+if ($update_goto) {
+       print 'static GOTO';
+       my $r = write_static_goto(
+               \%state,
+               \%settings,
+               '' # \%goto_list
+       );
+       if ($r) {
+               print " OK\n";
+       }
+       else {
+               print " FAIL\n";
+               $fail += 1;
+       }
+}
+
+foreach my $id (@list) {
+       print "ONG $id\n";
+       my $r = ong(
+               $id,   # frame ID
+               $time, # ONG time
+               '',    # timer value; not relevant
+               1,     # update
+               1,     # print
+               \%settings,
+               \%default,
+               get_frame_data($id),
+               ''    # %goto_list
+       );
+       unless ($r) {
+               $fail += 1;
+               print "ONG FAIL!\n";
+       }
+       make_static_pages($id);
+}
+
+# duplicated - before AND after normal pages!
+if ($update_goto) {
+       print 'static GOTO';
+       my $r = write_static_goto(
+               \%state,
+               \%settings,
+               '' # \%goto_list
+       );
+       if ($r) {
+               print " OK\n";
+       }
+       else {
+               print " FAIL\n";
+               $fail += 1;
+       }
+}
+
+print "\n";
+
+
+sub get_frame_data {
+       (my $id) = @_;
+       
+       unless ($id =~ /^[0-9]+$/) {
+               return '';
        }
+       my $f = int($id);
        
-       print $frame.' '.$framedata{'ongtime'}.' '.$framedata{'title'}."\n";
+       unless (($f >= 0) && ($f <= $last_frame)) {
+               return '';
+       }
+       
+       my $r = $all_frame_data{$f};
+       if (ref ($r)) {
+               return $r;
+       }
+       
+       my %frame_data = read_frame_data($f);
+       $all_frame_data{$f} = \%frame_data;
+       return \%frame_data;
+}
+
+sub get_words_data {
+       (my $id) = @_;
+       
+       unless ($id =~ /^[0-9]+$/) {
+               return '';
+       }
+       my $f = int($id);
+       
+       unless (($f >= 0) && ($f <= $last_frame)) {
+               return '';
+       }
+       
+       my $r = $all_words_data{$f};
+       if (ref ($r)) {
+               return $r;
+       }
+       
+       my %words_data = read_words_list($f, 1);
+       $all_words_data{$f} = \%words_data;
+       return \%words_data;
+}
+
+sub make_static_page {
+       (my $id) = @_;
+       unless ($id =~ /^[0-9]+$/) {
+               return;
+       }
+       my $f = int($id);
+       unless (
+               ($f >= 0) && (
+                       ($f < $last_frame) || (
+                               ($ong_state >= STATE->{'end'}) &&
+                               ($f <= $last_frame)
+                       )
+               )
+       ) {
+               return;
+       }
+       my $r;
+       
+       if (($f == 0) && ($ong_state > STATE->{'inactive'})) {
+               print 'index';
+               $r = write_index(
+                       \%state,
+                       \%settings,
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+       elsif ($f > 0) {
+               print 'static page '.$f;
+               $r = write_static_viewer_page (
+                       $f,
+                       \%state,
+                       \%settings,
+                       \%default,
+                       get_frame_data($f),
+                       get_frame_data($f-1),
+                       get_frame_data($f+1),
+                       get_words_data($f)
+               );
+               print (($r) ? " OK\n" : " FAIL\n");
+       }
+}
+
+sub make_static_pages {
+       (my $id) = @_;
+       unless ($id =~ /^[0-9]+$/) {
+               return;
+       }
+       my $f = int($id);
+       unless (($f >= 0) && ($f <= $last_frame)) {
+               return '';
+       }
        
-       $gotolist{'title-'.$frame}=$framedata{'title'};
-       $gotolist{'ongtime-'.$frame}=$framedata{'ongtime'};
+       make_static_page($f);
+       make_static_page($f-1);
+       make_static_page($f+1);
+       make_static_page($f);
 }
 
-writedatafile (DATA_LIST_PATH,%gotolist);
+exit $fail;