chiark / gitweb /
Normalise commodity name case (from uploads)
[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 #    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 BEGIN { unshift @INC, qw(.) }
56
57 use strict (qw(vars));
58 use DBI;
59 use Commods;
60 use CommodsDatabase;
61 use File::Copy;
62
63 $ENV{'LC_CTYPE'}= 'en_GB.UTF-8';
64
65 sub full ($) {
66     my ($ocean) = @_;
67     quick($ocean);
68     print "## updating topology of $ocean\n";
69     system('./yppedia-chart-parser',$ocean); die "$ocean $?" if $?;
70     print "\n";
71 }
72
73 sub quick ($) {
74     my ($ocean) = @_;
75     print STDERR "## updating schema and commodities for $ocean\n";
76     system('./db-idempotent-populate',$ocean); die $? if $?;
77 }
78
79 my $rsyncdir;
80
81 sub process_some_info ($$$) {
82     my ($v,$df,$sfn) = @_;
83     my $sf= new IO::File $sfn or die "$sfn $!";
84
85     my $h;
86     while (<$sf>) {
87         chomp; s/\s+$//;
88         next if m/^\s*\#/ || !m/\S/;
89         if (m/^\S.*/) {
90             $h= $&;
91         }
92         die "$_ ?" unless defined $h;
93         if ($h =~ m/^commods|^\%[a-z]\b/) {
94             s/\t.*//;
95         }
96         if ($v<2) {
97             next if $h =~ m/^nocommods/;
98         }
99         next if $sfn =~ m/source-info/ && $h =~ m/^ocean\b/;
100         next if $h =~ m/^client|^vessels|^shot\b|^commodclasses/;
101
102         print $df $_, "\n" or die $!;
103     }
104
105     $sf->error and die $!;
106 }
107
108 sub update_master_info () {
109     foreach my $v (1..$masterinfoversion) {
110         my $dfnl= sprintf "master-info%s.txt", ($v>1 ? "-v$v" : '');
111         print STDERR "installing new $dfnl...\n";
112     
113         my $dfn= "$rsyncdir/$dfnl";
114         my $df= new IO::File "$dfn.tmp", 'w' or die "$dfn.tmp $!";
115
116         process_some_info($v,$df, 'source-info.txt');
117         foreach my $ocean (sort keys %oceans) {
118             process_some_info($v,$df, '_ocean-'.(lc $ocean).'.txt');
119         }
120
121         close $df or die $!;
122         rename "$dfn.tmp", "$dfn" or die $!;
123     }
124 }
125
126
127 my @specoceans;
128 my $alloceans;
129 my $backup;
130
131 sub optarg () {
132     return $_ if length;
133     die unless @ARGV;
134     return scalar shift @ARGV;
135 }
136
137 while (@ARGV && $ARGV[0] =~ m/^-/) {
138     $_= shift @ARGV;
139     last if m/^--?$/;
140     while (m/^-./) {
141         if (s/^-d//) {
142             die if defined $rsyncdir;
143             $rsyncdir= optarg();
144         } elsif (s/^-O//) {
145             push @specoceans, optarg();
146         } elsif (s/^-b/-/) {
147             die if $backup;
148             $backup=1;
149         } elsif (s/^-a/-/) {
150             die if $alloceans;
151             $alloceans=1;
152         } else {
153             die "$_ ?";
154         }
155     }
156 }
157 die if @ARGV;
158
159 die if @specoceans && $alloceans;
160 die if $backup && !$alloceans && !@specoceans;
161
162 parse_info_serverside();
163
164 if ($backup) {
165     my @oceans= $alloceans ? (sort keys %oceans) : @specoceans;
166     foreach my $ocean (@oceans) {
167         print "## database backup for $ocean\n";
168         db_setocean($ocean);
169         db_writer();
170         db_connect();
171         $dbh->selectall_arrayref("SELECT * FROM commods WHERE commodid=1");
172         my $src= db_filename();
173         my $dst= $src; $dst =~ s,.*/,,; $dst= "$rsyncdir/$dst";
174         copy($src,"$dst.tmp") or die "$src -> $dst.tmp $!";
175         rename("$dst.tmp",$dst) or die "$dst.tmp -> $dst $!";
176         $dbh->rollback();
177     }
178 } elsif (@specoceans) {
179     print "### full update of specified oceans ...\n";
180     foreach my $ocean (@specoceans) {
181         die "$ocean ?" unless defined $oceans{$ocean};
182         full($ocean);
183     }
184 } elsif ($alloceans) {
185     print "### full (inc.topology) update of all oceans ...\n";
186     foreach my $ocean (sort keys %oceans) {
187         full($ocean);
188     }
189 } else {
190     print "### quick (no topology) update only (of all oceans) ...\n";
191     foreach my $ocean (sort keys %oceans) {
192         quick($ocean);
193     }
194 }
195
196 if (defined $rsyncdir and !$backup) {
197     print "### master-info update ...\n";
198     update_master_info();
199 }