chiark / gitweb /
Usage information
[ypp-sc-tools.db-test.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
30 # This is part of ypp-sc-tools, a set of third-party tools for assisting
31 # players of Yohoho Puzzle Pirates.
32 #
33 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
34 #
35 # This program is free software: you can redistribute it and/or modify
36 # it under the terms of the GNU General Public License as published by
37 # the Free Software Foundation, either version 3 of the License, or
38 # (at your option) any later version.
39 #
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43 # GNU General Public License for more details.
44 #
45 # You should have received a copy of the GNU General Public License
46 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
47 #
48 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
49 # are used without permission.  This program is not endorsed or
50 # sponsored by Three Rings.
51
52 use strict (qw(vars));
53 use DBI;
54 use Commods;
55
56 $ENV{'LC_CTYPE'}= 'en_GB.UTF-8';
57
58 sub full ($) {
59     my ($ocean) = @_;
60     quick($ocean);
61     print "## updating topology of $ocean\n";
62     system('./yppedia-chart-parser',$ocean); die "$ocean $?" if $?;
63 }
64
65 sub quick ($) {
66     my ($ocean) = @_;
67     print STDERR "## updating schema and commodities for $ocean\n";
68     system('./db-idempotent-populate',$ocean); die $? if $?;
69 }
70
71 my $rsyncdir;
72
73 sub process_some_info ($$$) {
74     my ($v,$df,$sfn) = @_;
75     my $sf= new IO::File $sfn or die "$sfn $!";
76
77     my $h;
78     while (<$sf>) {
79         chomp; s/\s+$//;
80         next if m/^\s*\#/ || !m/\S/;
81         if (m/^\S.*/) {
82             $h= $&;
83         }
84         die "$_ ?" unless defined $h;
85         if ($h =~ m/^commods|^\%[a-z]\b/) {
86             s/\t.*//;
87         }
88         if ($v<2) {
89             next if $h =~ m/^nocommods/;
90         }
91         next if $sfn =~ m/source-info/ && $h =~ m/^ocean/;
92
93         print $df $_, "\n" or die $!;
94     }
95
96     $sf->error and die $!;
97 }
98
99 sub update_master_info () {
100     foreach my $v (1..$masterinfoversion) {
101         my $dfnl= sprintf "master-info%s.txt", ($v>1 ? "-v$v" : '');
102         print STDERR "installing new $dfnl...\n";
103     
104         my $dfn= "$rsyncdir/$dfnl";
105         my $df= new IO::File "$dfn.tmp", 'w' or die "$dfn.tmp $!";
106
107         process_some_info($v,$df, 'source-info.txt');
108         foreach my $ocean (sort keys %oceans) {
109             process_some_info($v,$df, '_ocean-'.(lc $ocean).'.txt');
110         }
111
112         close $df or die $!;
113         rename "$dfn.tmp", "$dfn" or die $!;
114     }
115 }
116
117
118 my @specoceans;
119 my $alloceans;
120
121 sub optarg () {
122     return $_ if length;
123     die unless @ARGV;
124     return scalar shift @ARGV;
125 }
126
127 while (@ARGV && $ARGV[0] =~ m/^-/) {
128     $_= shift @ARGV;
129     last if m/^--?$/;
130     while (m/^-./) {
131         if (s/^-d//) {
132             die if defined $rsyncdir;
133             $rsyncdir= optarg();
134         } elsif (s/^-O//) {
135             push @specoceans, optarg();
136         } elsif (s/^-a//) {
137             die if $alloceans;
138             $alloceans=1;
139         } else {
140             die "$_ ?";
141         }
142     }
143 }
144 die if @ARGV;
145
146 die if @specoceans && $alloceans;
147
148 parse_info_serverside();
149
150 if (@specoceans) {
151     print "### full update of specified oceans ...\n";
152     foreach my $ocean (@specoceans) {
153         die "$ocean ?" unless defined $oceans{$ocean};
154         full($ocean);
155     }
156 } elsif ($alloceans) {
157     print "### full (inc.topology) update of all oceans ...\n";
158     foreach my $ocean (sort keys %oceans) {
159         full($ocean);
160     }
161 } else {
162     print "### quick (no topology) update only (of all oceans) ...\n";
163     foreach my $ocean (sort keys %oceans) {
164         quick($ocean);
165     }
166 }
167
168 if (defined $rsyncdir) {
169     print "### master-info update ...\n";
170     update_master_info();
171 }