chiark / gitweb /
Merge branch 'ceb'
[ypp-sc-tools.main.git] / yarrg / update-master-info
1 #!/usr/bin/perl -w
2 #
3 # MAIN ADMINISTRATIVE DATABASE UPDATE TOOL
4 #
5 #  Usage:
6 #     ./update-master-info [-d RSYNCDESTDIR] [-O OCEAN ... | -a]
7 #
8 #  Usual usages:
9 #
10 #    After editing source-info.txt to add commodities, or
11 #    changing db-idempotent-update (eg to change the schema):
12 #         ./update-master-info -d ~ftp/users/ijackson/yarrg/
13 #    This will update everything except the ocean topologies.
14 #
15 #    To take account of new islands, or to fix a mistake in
16 #    assigning island(s) to archipelago(es), or to add support
17 #    for a new ocean:
18 #         ./update-master-info -d ~ftp/users/ijackson/yarrg/ -O Midnight
19 #
20 #    After a fix to a mistake on a YPPedia chart page, correcting
21 #    only inter-island-distances:
22 #         ./update-master-info -O Midnight
23 #
24 #    After changing the algorithms for topology determination
25 #    or YPPedia scraping:
26 #         ./update-master-info -a
27 #    and check that nothing unexpected changes.
28 #
29 #    To just make backups of the databases into the rsync directory:
30 #         ./update-master-info -b -d ~ftp/users/ijackson/yarrg/ -a
31 #         ./update-master-info -b -d ~ftp/users/ijackson/yarrg/ -O ...
32
33 # This is part of the YARRG website.  YARRG is a tool and website
34 # for assisting players of Yohoho Puzzle Pirates.
35 #
36 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
37 #
38 # This program is free software: you can redistribute it and/or modify
39 # it under the terms of the GNU Affero General Public License as
40 # published by the Free Software Foundation, either version 3 of the
41 # License, or (at your option) any later version.
42 #
43 # This program is distributed in the hope that it will be useful,
44 # but WITHOUT ANY WARRANTY; without even the implied warranty of
45 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46 # GNU Affero General Public License for more details.
47 #
48 # You should have received a copy of the GNU Affero General Public License
49 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
50 #
51 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
52 # are used without permission.  This program is not endorsed or
53 # sponsored by Three Rings.
54
55 use strict (qw(vars));
56 use DBI;
57 use Commods;
58 use CommodsDatabase;
59 use File::Copy;
60
61 $ENV{'LC_CTYPE'}= 'en_GB.UTF-8';
62
63 sub full ($) {
64     my ($ocean) = @_;
65     quick($ocean);
66     print "## updating topology of $ocean\n";
67     system('./yppedia-chart-parser',$ocean); die "$ocean $?" if $?;
68     print "\n";
69 }
70
71 sub quick ($) {
72     my ($ocean) = @_;
73     print STDERR "## updating schema and commodities for $ocean\n";
74     system('./db-idempotent-populate',$ocean); die $? if $?;
75 }
76
77 my $rsyncdir;
78
79 sub process_some_info ($$$) {
80     my ($v,$df,$sfn) = @_;
81     my $sf= new IO::File $sfn or die "$sfn $!";
82
83     my $h;
84     while (<$sf>) {
85         chomp; s/\s+$//;
86         next if m/^\s*\#/ || !m/\S/;
87         if (m/^\S.*/) {
88             $h= $&;
89         }
90         die "$_ ?" unless defined $h;
91         if ($h =~ m/^commods|^\%[a-z]\b/) {
92             s/\t.*//;
93         }
94         if ($v<2) {
95             next if $h =~ m/^nocommods/;
96         }
97         next if $sfn =~ m/source-info/ && $h =~ m/^ocean\b/;
98         next if $h =~ m/^client|^vessels|^shot\b|^commodclasses/;
99
100         print $df $_, "\n" or die $!;
101     }
102
103     $sf->error and die $!;
104 }
105
106 sub update_master_info () {
107     foreach my $v (1..$masterinfoversion) {
108         my $dfnl= sprintf "master-info%s.txt", ($v>1 ? "-v$v" : '');
109         print STDERR "installing new $dfnl...\n";
110     
111         my $dfn= "$rsyncdir/$dfnl";
112         my $df= new IO::File "$dfn.tmp", 'w' or die "$dfn.tmp $!";
113
114         process_some_info($v,$df, 'source-info.txt');
115         foreach my $ocean (sort keys %oceans) {
116             process_some_info($v,$df, '_ocean-'.(lc $ocean).'.txt');
117         }
118
119         close $df or die $!;
120         rename "$dfn.tmp", "$dfn" or die $!;
121     }
122 }
123
124
125 my @specoceans;
126 my $alloceans;
127 my $backup;
128
129 sub optarg () {
130     return $_ if length;
131     die unless @ARGV;
132     return scalar shift @ARGV;
133 }
134
135 while (@ARGV && $ARGV[0] =~ m/^-/) {
136     $_= shift @ARGV;
137     last if m/^--?$/;
138     while (m/^-./) {
139         if (s/^-d//) {
140             die if defined $rsyncdir;
141             $rsyncdir= optarg();
142         } elsif (s/^-O//) {
143             push @specoceans, optarg();
144         } elsif (s/^-b/-/) {
145             die if $backup;
146             $backup=1;
147         } elsif (s/^-a/-/) {
148             die if $alloceans;
149             $alloceans=1;
150         } else {
151             die "$_ ?";
152         }
153     }
154 }
155 die if @ARGV;
156
157 die if @specoceans && $alloceans;
158 die if $backup && !$alloceans && !@specoceans;
159
160 parse_info_serverside();
161
162 if ($backup) {
163     my @oceans= $alloceans ? (sort keys %oceans) : @specoceans;
164     foreach my $ocean (@oceans) {
165         print "## database backup for $ocean\n";
166         db_setocean($ocean);
167         db_writer();
168         db_connect();
169         $dbh->selectall_arrayref("SELECT * FROM commods WHERE commodid=1");
170         my $src= db_filename();
171         my $dst= $src; $dst =~ s,.*/,,; $dst= "$rsyncdir/$dst";
172         copy($src,"$dst.tmp") or die "$src -> $dst.tmp $!";
173         rename("$dst.tmp",$dst) or die "$dst.tmp -> $dst $!";
174         $dbh->rollback();
175     }
176 } elsif (@specoceans) {
177     print "### full update of specified oceans ...\n";
178     foreach my $ocean (@specoceans) {
179         die "$ocean ?" unless defined $oceans{$ocean};
180         full($ocean);
181     }
182 } elsif ($alloceans) {
183     print "### full (inc.topology) update of all oceans ...\n";
184     foreach my $ocean (sort keys %oceans) {
185         full($ocean);
186     }
187 } else {
188     print "### quick (no topology) update only (of all oceans) ...\n";
189     foreach my $ocean (sort keys %oceans) {
190         quick($ocean);
191     }
192 }
193
194 if (defined $rsyncdir and !$backup) {
195     print "### master-info update ...\n";
196     update_master_info();
197 }