Fix documentation relating to conffile location.
[usenet/newsstats.git] / install / install.pl
CommitLineData
3f817eb4 1#! /usr/bin/perl
2832c235
TH
2#
3# install.pl
4#
5# This script will create database tables as necessary.
dfc2b81c 6#
2832c235
TH
7# It is part of the NewsStats package.
8#
07c0b258 9# Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
2832c235 10#
dfc2b81c 11# It can be redistributed and/or modified under the same terms under
2832c235
TH
12# which Perl itself is published.
13
14BEGIN {
24d2011f 15 our $VERSION = "0.02";
2832c235 16 use File::Basename;
2ad99c20
TH
17 # we're in .../install, so our module is in ../lib
18 push(@INC, dirname($0).'/../lib');
2832c235
TH
19}
20use strict;
3f817eb4 21use warnings;
2832c235
TH
22
23use NewsStats qw(:DEFAULT);
24
25use Cwd;
26
27use DBI;
1fa94799
TH
28use Getopt::Long qw(GetOptions);
29Getopt::Long::config ('bundling');
2832c235
TH
30
31################################# Main program #################################
32
33### read commandline options
23ab67a0 34my ($OptUpdate,$OptConfFile);
1fa94799 35GetOptions ('u|update=s' => \$OptUpdate,
23ab67a0 36 'conffile=s' => \$OptConfFile,
1fa94799
TH
37 'h|help' => \&ShowPOD,
38 'V|version' => \&ShowVersion) or exit 1;
2832c235
TH
39
40### change working directory to .. (as we're in .../install)
512781bd 41chdir dirname($FullPath).'/..';
f6d15ca7 42my $Path = cwd();
2832c235
TH
43
44### read configuration
45print("Reading configuration.\n");
23ab67a0 46my %Conf = %{ReadConfig($OptConfFile)};
2832c235
TH
47
48##### --------------------------------------------------------------------------
49##### Database table definitions
50##### --------------------------------------------------------------------------
51
36cffe7a
TH
52my $DBCreate = <<SQLDB;
53CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
54SQLDB
55
2832c235 56my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS);
23ab67a0 57--
2832c235 58-- Table structure for table DBTableRaw
23ab67a0 59--
2832c235
TH
60
61CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` (
62 `id` bigint(20) unsigned NOT NULL auto_increment,
63 `day` date NOT NULL,
64 `mid` varchar(250) character set ascii NOT NULL,
65 `date` datetime NOT NULL,
66 `timestamp` bigint(20) NOT NULL,
67 `token` varchar(80) character set ascii NOT NULL,
68 `size` bigint(20) NOT NULL,
69 `peer` varchar(250) NOT NULL,
70 `path` varchar(1000) NOT NULL,
71 `newsgroups` varchar(1000) NOT NULL,
72 `headers` longtext NOT NULL,
73 `disregard` tinyint(1) default '0',
74 PRIMARY KEY (`id`),
75 KEY `day` (`day`),
76 KEY `mid` (`mid`),
77 KEY `peer` (`peer`)
78) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data';
79RAW
23ab67a0 80--
2832c235 81-- Table structure for table DBTableGrps
23ab67a0 82--
2832c235
TH
83
84CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` (
85 `id` bigint(20) unsigned NOT NULL auto_increment,
86 `month` varchar(7) character set ascii NOT NULL,
87 `newsgroup` varchar(100) NOT NULL,
88 `postings` int(11) NOT NULL,
89 `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
90 PRIMARY KEY (`id`),
91 UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`),
92 KEY `newsgroup` (`newsgroup`),
93 KEY `postings` (`postings`)
94) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup';
95GRPS
96
f6d15ca7
TH
97##### --------------------------------------------------------------------------
98##### Installation / upgrade instructions
99##### --------------------------------------------------------------------------
2832c235 100
f6d15ca7
TH
101my $Install = <<INSTALL;
102----------
2832c235
TH
103Things left to do:
104
1051) Setup an INN feed to feedlog.pl
106
107 a) Edit your 'newsfeeds' file and insert something like
108
109 ## gather statistics for NewsStats
9c141b0c
TH
110 newsstats!\\
111 :!*,de.*\\
2832c235
TH
112 :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl
113
114 Please
115
116 * check that you got the path to feedlog.pl right
117 * check that feedlog.pl can be executed by the news user
118 * adapt the pattern (here: 'de.*') to your needs
119
120 b) Check your 'newsfeeds' syntax:
121
122 # ctlinnd checkfile
123
124 and reload 'newsfeeds':
125
126 # ctlinnd reload newsfeeds 'Adding newsstats! feed'
127
128 c) Watch your 'news.notice' and 'errlog' files:
129
130 # tail -f /var/log/news/news.notice
131 ...
132 # tail -f /var/log/news/errlog
133
1342) Watch your $Conf{'DBTableRaw'} table fill.
135
1363) Read the documentation. ;)
137
138Enjoy!
139
140-thh <thh\@inter.net>
f6d15ca7
TH
141INSTALL
142
da6fc073
TH
143my $Upgrade ='';
144if ($OptUpdate) {
145 $Upgrade = <<UPGRADE;
f6d15ca7 146----------
1fa94799 147Your installation was upgraded from $OptUpdate to $PackageVersion.
f6d15ca7
TH
148
149Don't forget to restart your INN feed so that it can pick up the new version:
150
151 # ctlinnd begin 'newsstats!'
152
153(or whatever you called your feed).
154UPGRADE
da6fc073 155}
f6d15ca7 156
f6d15ca7
TH
157##### --------------------------- End of definitions ---------------------------
158
36cffe7a
TH
159### create DB, if necessary
160if (!$OptUpdate) {
161 print "----------\nStarting database creation.\n";
162 # create database
163 # we can't use InitDB() as that will use a table name of
164 # the table that doesn't exist yet ...
165 my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
166 $Conf{'DBHost'}), $Conf{'DBUser'},
167 $Conf{'DBPw'}, { PrintError => 0 });
168 my $DBQuery = $DBHandle->prepare($DBCreate);
169 $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
170 $Conf{'DBDatabase'}, $DBI::errstr));
23ab67a0 171
36cffe7a
TH
172 printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
173 $DBHandle->disconnect;
174};
175
f6d15ca7
TH
176### DB init, read list of tables
177print "Reading database information.\n";
178my $DBHandle = InitDB(\%Conf,1);
1fa94799
TH
179my %TablesInDB =
180 %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')};
f6d15ca7 181
1fa94799 182if (!$OptUpdate) {
f6d15ca7 183 ##### installation mode
f6d15ca7
TH
184 # check for tables and create them, if they don't exist yet
185 foreach my $Table (keys %DBCreate) {
186 &CreateTable($Table);
187 };
188 print "Database table generation done.\n";
dfc2b81c 189
f6d15ca7
TH
190 # Display install instructions
191 print $Install;
192} else {
193 ##### upgrade mode
194 print "----------\nStarting upgrade process.\n";
59d0c5ef 195 $PackageVersion = '0.03';
1fa94799
TH
196 if ($OptUpdate < $PackageVersion) {
197 if ($OptUpdate < 0.02) {
59d0c5ef
TH
198 # 0.01 -> 0.02
199 # &DoMySQL('...;');
200 # print "v0.02: Database upgrades ...\n";
201 # &PrintInstructions('0.02',<<" INSTRUCTIONS");
202 # INSTRUCTIONS
f6d15ca7
TH
203 };
204 };
59d0c5ef 205 # Display general upgrade instructions
f6d15ca7
TH
206 print $Upgrade;
207};
208
209# close handle
210$DBHandle->disconnect;
211
212exit(0);
213
214################################# Subroutines ##################################
215
59d0c5ef 216sub CreateTable {
f6d15ca7
TH
217 my $Table = shift;
218 if (defined($TablesInDB{$Conf{$Table}})) {
1fa94799
TH
219 printf("Database table %s.%s already exists, skipping ....\n",
220 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
221 return;
222 };
223 my $DBQuery = $DBHandle->prepare($DBCreate{$Table});
1fa94799
TH
224 $DBQuery->execute() or
225 &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table,
226 $Conf{'DBDatabase'},$DBI::errstr));
227 printf("Database table %s.%s created succesfully.\n",
228 $Conf{'DBDatabase'},$Conf{$Table});
f6d15ca7
TH
229 return;
230};
231
59d0c5ef 232sub DoMySQL {
f6d15ca7
TH
233 my $SQL = shift;
234 my $DBQuery = $DBHandle->prepare($SQL);
1fa94799 235 $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr));
f6d15ca7
TH
236 return;
237};
238
59d0c5ef
TH
239sub PrintInstructions {
240 my ($UpVersion,$Instructions) = @_;
241 print "v$UpVersion: Upgrade Instructions >>>>>\n";
242 my $Padding = ' ' x (length($UpVersion) + 3);
243 $Instructions =~ s/^ /$Padding/mg;
244 print $Instructions;
245 print "<" x (length($UpVersion) + 29) . "\n";
246};
247
2832c235
TH
248
249__END__
250
251################################ Documentation #################################
252
253=head1 NAME
254
255install - installation script
256
257=head1 SYNOPSIS
258
e39d4207 259B<install> [B<-Vh> [--update I<version>] [B<--conffile> I<filename>]
2832c235
TH
260
261=head1 REQUIREMENTS
262
1fa94799 263See L<doc/README>.
2832c235
TH
264
265=head1 DESCRIPTION
266
267This script will create database tables as necessary and configured.
268
269=head2 Configuration
270
44c19709
TH
271B<install> will read its configuration from F<newsstats.conf> which should
272be present in etc/ via Config::Auto or from a configuration file submitted
273by the B<--conffile> option.
2832c235 274
1fa94799 275See L<doc/INSTALL> for an overview of possible configuration options.
2832c235
TH
276
277=head1 OPTIONS
278
279=over 3
280
1fa94799 281=item B<-V>, B<--version>
2832c235 282
1fa94799 283Print out version and copyright information and exit.
2832c235 284
1fa94799 285=item B<-h>, B<--help>
2832c235
TH
286
287Print this man page and exit.
288
1fa94799
TH
289=item B<-u>, B<--update> I<version>
290
291Don't do a fresh install, but update from I<version>.
292
23ab67a0
TH
293=item B<--conffile> I<filename>
294
295Load configuration from I<filename> instead of F<newsstats.conf>.
296
2832c235
TH
297=back
298
299=head1 FILES
300
301=over 4
302
2ad99c20 303=item F<install/install.pl>
2832c235
TH
304
305The script itself.
306
2ad99c20 307=item F<lib/NewsStats.pm>
2832c235
TH
308
309Library functions for the NewsStats package.
310
2ad99c20 311=item F<etc/newsstats.conf>
2832c235 312
1fa94799 313Runtime configuration file.
2832c235
TH
314
315=back
316
317=head1 BUGS
318
319Please report any bugs or feature requests to the author or use the
320bug tracker at L<http://bugs.th-h.de/>!
321
322=head1 SEE ALSO
323
324=over 2
325
326=item -
327
1fa94799 328L<doc/README>
2832c235
TH
329
330=item -
331
1fa94799 332L<doc/INSTALL>
2832c235
TH
333
334=back
335
336This script is part of the B<NewsStats> package.
337
338=head1 AUTHOR
339
340Thomas Hochstein <thh@inter.net>
341
342=head1 COPYRIGHT AND LICENSE
343
28717921 344Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
2832c235
TH
345
346This program is free software; you may redistribute it and/or modify it
347under the same terms as Perl itself.
348
349=cut
This page took 0.030269 seconds and 4 git commands to generate.