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