disable templater debug output
[bse.git] / localinst.perl
CommitLineData
5a2cc1bd
TC
1#!/usr/bin/perl -w
2use strict;
ecabd3cb
TC
3#use File::Tree;
4use File::Copy;
3bc94f98
TC
5use lib 't';
6use BSE::Test ();
f0543260 7require ExtUtils::Manifest;
5a2cc1bd 8
4bfb1a2b
TC
9my $dist = shift or die "Usage: $0 distdir [leavedb]";
10my $leavedb = shift or 0;
3bc94f98 11my $instbase = shift || BSE::Test::base_dir() || die "No base_dir";
5a2cc1bd 12
3bc94f98 13my $mysql = BSE::Test::mysql_name;
5a2cc1bd 14
3bc94f98
TC
15# if (-e "$instbase/cgi-bin/modules/Constants.pm"
16# && !-e "$instbase/Constants.pm") {
17# system "cp $instbase/cgi-bin/modules/Constants.pm $instbase/Constants.pm"
18# }
5a2cc1bd 19
ce07f199
TC
20#system("rm -rf $instbase/cgi-bin")
21# and die "Cannot remove cgi-bin";
22#system "rm -rf $instbase/data"
23# and die "Cannot remove data";
ab2cd916
TC
24#system "rm -f $instbase/htdocs/{*.html,a/*.html,shop/*.html,images/*.jpg}"
25# and die "Cannot remove htdocs";
5a2cc1bd 26
ce07f199
TC
27-d "$instbase/cgi-bin" or mkdir "$instbase/cgi-bin"
28 or die "Cannot create $instbase/cgi-bin: $!";
29system "cp -rf $dist/site/cgi-bin/* $instbase/cgi-bin"
5a2cc1bd
TC
30 and die "Cannot copy cgi-bin";
31
f0543260
TC
32my $perl = BSE::Test::test_perl();
33if ($perl ne '/usr/bin/perl') {
34 my $manifest = ExtUtils::Manifest::maniread();
35
36 for my $file (grep /\.pl$/, keys %$manifest) {
37 (my $work = $file) =~ s!^site!!;
38 next unless $work =~ /cgi-bin/;
39 my $full = $instbase . $work;
40 open SCRIPT, "< $full" or die "Cannot open $full: $!";
41 binmode SCRIPT;
42 my @all = <SCRIPT>;
43 close SCRIPT;
44 $all[0] =~ s/^#!\S*perl\S*/#!$perl/;
45 open SCRIPT, "> $full" or die "Cannot create $full: $!";
46 binmode SCRIPT;
47 print SCRIPT @all;
48 close SCRIPT;
49 }
50}
51
5a2cc1bd
TC
52system "cp -rf $dist/site/htdocs $instbase"
53 and die "Cannot copy htdocs";
54system "cp -rf $dist/site/templates $instbase"
55 and die "Cannot copy templates";
56system "cp -rf $dist/site/data $instbase"
57 and die "Cannot copy data";
58system "cp -rf $dist/site/util $instbase";
59
3bc94f98
TC
60print "Updating conf\n";
61# try to update Constants.pm
62open CON, "< $instbase/cgi-bin/modules/Constants.pm"
63 or die "Cannot open Constants.pm";
64my $con = do { local $/; <CON> };
65close CON;
5a2cc1bd 66
3bc94f98
TC
67my $dbuser = BSE::Test::test_dbuser();
68my $dbpass = BSE::Test::test_dbpass();
69
70$con =~ s/(^\$DSN = ')[^']*/$1 . BSE::Test::test_dsn()/me;
71$con =~ s/(^\$DBCLASS = ')[^']*/$1 . BSE::Test::test_dbclass()/me;
72$con =~ s/(^\$UN = ')[^']*/$1$dbuser/m;
73$con =~ s/(^\$PW = ')[^']*/$1$dbpass/m;
74$con =~ s/(^\$BASEDIR = ')[^']+/$1 . BSE::Test::base_dir/me;
75#$con =~ s/(^\$URLBASE = ["'])[^'"]+/$1 . BSE::Test::base_url/me;
76#$con =~ s/(^\$SECURLBASE = ["'])[^'"]+/$1 . BSE::Test::test_securl/me;
9168c88c 77$con =~ s/(^\$SESSION_CLASS = [\"\'])[^\'\"]+/$1 . BSE::Test::test_sessionclass()/me;
3bc94f98
TC
78open CON, "> $instbase/cgi-bin/modules/Constants.pm"
79 or die "Cannot open Constants.pm for write: $!";
80print CON $con;
81close CON;
5a2cc1bd 82
9168c88c
TC
83# rebuild the config file
84# first load values from the test.cfg file
85my $conffile = BSE::Test::test_conffile();
86my %conf;
9168c88c
TC
87$conf{site}{url} = BSE::Test::base_url();
88$conf{site}{secureurl} = BSE::Test::base_securl();
89my $uploads = "$instbase/uploads";
90$conf{paths}{downloads} = $uploads;
aefcabcb
TC
91my $templates = "$instbase/templates";
92$conf{paths}{templates} = $templates;
9168c88c
TC
93open TESTCONF, "< $conffile"
94 or die "Could not open config file $conffile: $!";
95while (<TESTCONF>) {
96 chomp;
d44b5da9 97 /^\s*(\w[^=]*\w)\.(\w+)\s*=\s*(.*)\s*$/ or next;
9168c88c
TC
98 $conf{lc $1}{lc $2} = $3;
99}
100
101$uploads = $conf{paths}{downloads};
3bc94f98
TC
102# fix bse.cfg
103open CFG, "< $instbase/cgi-bin/bse.cfg"
104 or die "Cannot open $instbase/cgi-bin/bse.cfg: $!";
9168c88c
TC
105my $section = "";
106my @cfg;
107while (<CFG>) {
108 chomp;
109 if (/^\[(.*)\]\s*$/) {
110 my $newsect = lc $1;
111 if ($conf{$section} && keys %{$conf{$section}}) {
112 for my $key (sort keys %{$conf{$section}}) {
113 push @cfg, "$key=$conf{$section}{$key}";
114 }
115 delete $conf{$section};
116 }
117 $section = $newsect;
118 }
119 elsif (/^\s*(\w+)\s*=\s*.*$/ && exists $conf{$section}{lc $1}) {
120 my $key = lc $1;
121 print "found $section.$key\n";
122 $_ = "$key=$conf{$section}{$key}";
123 delete $conf{$section}{$key};
124 }
125 push @cfg, $_;
126}
127if ($conf{$section} && keys %{$conf{$section}}) {
128 for my $key (sort keys %{$conf{$section}}) {
129 push @cfg, "$key=$conf{$section}{$key}";
130 }
131 delete $conf{$section};
132}
133for my $sect (keys %conf) {
134 if ($conf{$sect} && keys %{$conf{$sect}}) {
135 push @cfg, "[$sect]";
136 for my $key (sort keys %{$conf{$sect}}) {
df45a70d 137 push @cfg, "$key=$conf{$sect}{$key}";
9168c88c
TC
138 }
139 push @cfg, "";
140 }
141}
3bc94f98 142close CFG;
9168c88c 143
3bc94f98
TC
144open CFG, "> $instbase/cgi-bin/bse.cfg"
145 or die "Cannot create $instbase/cgi-bin/bse.cfg: $!";
9168c88c
TC
146for my $line (@cfg) {
147 print CFG $line, "\n";
148}
3bc94f98
TC
149close CFG;
150
9168c88c
TC
151-d $uploads
152 or mkdir $uploads, 0777
153 or die "Cannot find or create upload directory: $!";
154
155
3bc94f98
TC
156# build the database
157unless ($leavedb) {
158 my $dsn = BSE::Test::test_dsn();
159 if ($dsn =~ /:mysql:(?:database=)?(\w+)/) {
160 my $db = $1;
161 system "$mysql -u$dbuser -p$dbpass $db <$dist/schema/bse.sql"
162 and die "Cannot initialize database";
163 system "cd $instbase/util ; perl initial.pl"
164 and die "Cannot load database";
165 }
166 else {
167 print "WARNING: cannot install to $dsn database\n";
4bfb1a2b 168 }
5a2cc1bd 169}
3bc94f98 170
ecabd3cb 171