Obsolete/broken perl compatibility workaround - [:space:] wasn't wised
[www.energie.vellum.cz.git] / obsah_init.pl
1 #! /usr/bin/perl
2 #
3 #       $Id$
4
5 use strict;
6 use DBI;
7
8 use vars qw/$db_driver $db_host $db_user $db_pwd $DB_PWD $db_name $db/;
9 use vars qw/$tb_obsah/;
10
11 $db_driver="mysql";
12 $db_host="";
13 $db_user="short";
14 $DB_PWD=$ENV{"HOME"}."/priv/mysql.${db_user}.pwd";
15 $db_name="short";
16 $tb_obsah="energie_obsah";
17
18 open DB_PWD or die "Failed open \"$DB_PWD\": $!";
19 $db_pwd=<DB_PWD>;
20 chomp $db_pwd;
21 close DB_PWD;
22
23 $db=DBI->connect("DBI:$db_driver:database=$db_name;host=$db_host",$db_user,$db_pwd) or die "Database open fail: $!";
24
25 sub db_do
26 {
27 my( $cmd )=@_;
28
29         $db->do($cmd) or die("SQL command \"$cmd\" failed: $!");
30 }
31
32 eval { &db_do("drop table $tb_obsah") };
33
34 &db_do("create table $tb_obsah ("
35                 ."year year(4) not null,"
36                 ."month tinyint not null,"
37                 ."month_last tinyint not null,"
38                 ."sequential smallint not null,"
39                 ."contents text not null"
40                 .")");
41
42 &db_do("alter table $tb_obsah add unique (year,month)");
43
44
45 use vars qw/$insert_tb_obsah $year $month $month_last $sequential $contents $first/;
46
47 sub where
48 {
49         return " in file $ARGV on line $.";
50 }
51
52 sub flush_month
53 {
54         $_=$contents;
55         return if !defined $_;
56         if (!$_) {
57                 print("Empty contents".&where()."!\n");
58                 }
59         else {
60                 tr/ \t\n/  \n/s;
61                 s/([\001\n]) | ([\002\n])/$1$2/g;
62                 die "Page marker not found somewhere in this month".&where() if (/^[^\004]*\001[^\003\004]*\002/);
63                 die "Duplicate page markers in this month".&where() if (/strana /);
64                 tr/\002//d;
65                 $_=substr($_,1);
66                 }
67         $insert_tb_obsah->execute($year,$month,$month_last,$sequential,$_) or die "SQL insert failure: $!";
68         undef $year,$month;
69         undef $contents;
70         undef $first;
71 }
72
73 $insert_tb_obsah=$db->prepare("insert into $tb_obsah (year,month,month_last,sequential,contents) values (?,?,?,?,?)")
74                 or die "Prepare fail: $!";
75
76 while (<>) {
77         chomp;
78         if (m#^EaP (\d+)(-(\d+))?/(\d+) \((\d+)(-\d+)?\)$#) {
79                 &flush_month();
80                 $month=$1;
81                 $month_last=($3 ? $3 : $1);
82                 $year =$4;
83                 $sequential=$5;
84                 $contents="";
85                 next;
86                 }
87         if (/^Pøíloha:$/) {
88                 die "Multiple \"Pøíloha\"s".&where() if ($contents=~/\004/);
89                 $contents.="\004";
90                 next;
91                 }
92         if (/^[^ \t]/ || (/^\s/ && $contents=~/\004/)) {
93                 $contents.="\001$_\002";
94                 $first=$_;
95                 next;
96                 }
97         if (/^\s+strana \.\.\. ([\d ,-]+)$/) {
98                 die "Page number without start".&where() if ("\002"!=substr($contents,-1,1));
99                 die "Page number already specified".&where() if ($contents=~/\003[^\001\002\004]*\002$/);
100                 $contents=substr($contents,0,-1)."\003$1\002";
101                 next;
102                 }
103         if (/^\s(.*)$/) {
104                 my( $s )=$1;
105                 die "INTERNAL: continuation for \"Pøíloha\"".&where() if ($contents=~/\004/);
106                 die "Continuation contents without start".&where() if ("\002"!=substr($contents,-1,1));
107                 if ($contents=~/\003[^\001\002\004]*\002$/) {
108                         die "First line not found".&where() if !defined($first);
109                         $contents.="\001$first\002";
110                         }
111                 $contents=substr($contents,0,-1)."\n$s\002";
112                 next;
113                 }
114         die "Unexpected text".&where().": $_" if (/\S/);
115         }
116 &flush_month();
117
118 print("success.\n");