]> git.imager.perl.org - bse.git/blob - localinst.perl
abb88a720a7d4c1301d157d5fc185379ad8f4447
[bse.git] / localinst.perl
1 #!/usr/bin/perl -w
2 use strict;
3 #use File::Tree;
4 use File::Copy;
5 use lib 't';
6 use BSE::Test ();
7 require ExtUtils::Manifest;
8
9 my $dist = shift or die "Usage: $0 distdir [leavedb]";
10 my $leavedb = shift or 0;
11 my $instbase = shift || BSE::Test::base_dir() || die "No base_dir";
12
13 my $mysql = BSE::Test::mysql_name;
14
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 #  }
19
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";
24 #system "rm -f $instbase/htdocs/{*.html,a/*.html,shop/*.html,images/*.jpg}"
25 #  and die "Cannot remove htdocs";
26
27 -d "$instbase/cgi-bin" or mkdir "$instbase/cgi-bin"
28   or die "Cannot create $instbase/cgi-bin: $!";
29 system "cp -rf $dist/site/cgi-bin/* $instbase/cgi-bin"
30   and die "Cannot copy cgi-bin";
31
32 my $perl = BSE::Test::test_perl();
33 if ($perl ne '/usr/bin/perl') {
34   my $manifest = ExtUtils::Manifest::maniread();
35
36   for my $file (keys %$manifest) {
37     (my $work = $file) =~ s!^site!!;
38     my $full = $instbase . $work;
39     open my $script, "<", $full
40       or next;
41     binmode $script;
42     my $first = <$script>;
43     if ($first =~ s/^#!\S*perl\S*/#!$perl/) {
44       my @all = <$script>;
45       close $script;
46       open my $out_script, ">", $full or die "Cannot create $full: $!";
47       binmode $out_script;
48       print $out_script $first, @all;
49       close $out_script;
50     }
51   }
52 }
53
54 system "cp -rf $dist/site/htdocs/* $instbase/htdocs"
55   and die "Cannot copy htdocs";
56 system "cp -rf $dist/site/templates/* $instbase/templates"
57   and die "Cannot copy templates";
58 system "cp -rf $dist/site/data $instbase"
59   and die "Cannot copy data";
60 system "cp -rf $dist/site/util/ $instbase";
61
62 print "Updating conf\n";
63 # try to update Constants.pm
64 open CON, "< $instbase/cgi-bin/modules/Constants.pm"
65   or die "Cannot open Constants.pm";
66 my $con = do { local $/; <CON> };
67 close CON;
68
69 my $dbuser = BSE::Test::test_dbuser();
70 my $dbpass = BSE::Test::test_dbpass();
71
72 #$con =~ s/(^\$DSN = ')[^']*/$1 . BSE::Test::test_dsn()/me;
73 #$con =~ s/(^\$DBCLASS = ')[^']*/$1 . BSE::Test::test_dbclass()/me;
74 #$con =~ s/(^\$UN = ')[^']*/$1$dbuser/m;
75 #$con =~ s/(^\$PW = ')[^']*/$1$dbpass/m;
76 #$con =~ s/(^\$BASEDIR = ')[^']+/$1 . BSE::Test::base_dir/me;
77 #$con =~ s/(^\$URLBASE = ["'])[^'"]+/$1 . BSE::Test::base_url/me;
78 #$con =~ s/(^\$SECURLBASE = ["'])[^'"]+/$1 . BSE::Test::test_securl/me;
79 $con =~ s/(^\$SESSION_CLASS = [\"\'])[^\'\"]+/$1 . BSE::Test::test_sessionclass()/me;
80 open CON, "> $instbase/cgi-bin/modules/Constants.pm"
81   or die "Cannot open Constants.pm for write: $!";
82 print CON $con;
83 close CON;
84
85
86 # rebuild the config file
87 # first load values from the test.cfg file
88 my $conffile = BSE::Test::test_conffile();
89 my %conf;
90 $conf{site}{url} = BSE::Test::base_url();
91 $conf{site}{secureurl} = BSE::Test::base_securl();
92 $conf{paths}{siteroot} = $instbase;
93 #my $uploads = "$instbase/uploads";
94 #$conf{paths}{downloads} = $uploads;
95 #my $templates = "$instbase/templates";
96 #$conf{paths}{templates} = $templates;
97 #$conf{paths}{public_html} = "$instbase/htdocs";
98 open TESTCONF, "< $conffile"
99   or die "Could not open config file $conffile: $!";
100 while (<TESTCONF>) {
101   chomp;
102   /^\s*(\w[^=]*\w)\.([\w-]+)\s*=\s*(.*)\s*$/ or next;
103   $conf{lc $1}{$2} = $3;
104 }
105
106 #$uploads = $conf{paths}{downloads};
107 # create installation config
108
109 $conf{db}{class} = BSE::Test::test_dbclass();
110 $conf{db}{dsn} = BSE::Test::test_dsn();
111 $conf{db}{user} = $dbuser;
112 $conf{db}{password} = $dbpass;
113
114 open CFG, "> $instbase/cgi-bin/bse-install.cfg"
115   or die "Cannot create $instbase/cgi-bin/bse-install.cfg: $!";
116
117 print CFG "; DO NOT EDIT - created during installation\n";
118 for my $section_name (keys %conf) {
119   print CFG "[$section_name]\n";
120   my $section = $conf{$section_name};
121   for my $key (keys %$section) {
122     print CFG "$key=$section->{$key}\n";
123   }
124   print CFG "\n";
125 }
126
127 close CFG;
128
129 #-d $uploads 
130 #  or mkdir $uploads, 0777 
131 #  or die "Cannot find or create upload directory: $!";
132
133
134 # build the database
135 my $dsn = BSE::Test::test_dsn();
136 if ($dsn =~ /:mysql:(?:database=)?(\w+)/) {
137   my $db = $1;
138
139   unless ($leavedb) {
140     system "$mysql -u$dbuser -p$dbpass $db <$dist/schema/bse.sql"
141       and die "Cannot initialize database";
142     system "cd $instbase/util ; $perl initial.pl"
143       and die "Cannot load database";
144   }
145
146   # always load stored procedures
147   system qq($mysql "-u$dbuser" "-p$dbpass" "$db" <$dist/schema/bse_sp.sql)
148     and die "Error loading stored procedures\n";
149 }
150 else {
151   print "WARNING: cannot install to $dsn database\n";
152 }
153
154