perform loaddata.pl updates in a transaction per table, to speed it up
[bse.git] / localinst.perl
CommitLineData
5a2cc1bd
TC
1#!/usr/bin/perl -w
2use strict;
ecabd3cb
TC
3#use File::Tree;
4use File::Copy;
5bbf7309
TC
5use lib 'lib';
6use BSE::Install qw(util_dir cgi_dir public_html_dir templates_dir data_dir mysql_name);
7#use BSE::Test ();
8use ExtUtils::Manifest qw(maniread);
9use File::Copy qw(copy);
10use File::Spec;
11use File::Path qw(make_path);
12use Getopt::Long;
13
14my $verbose;
15GetOptions("v|verbose" => \$verbose);
5a2cc1bd 16
4bfb1a2b
TC
17my $dist = shift or die "Usage: $0 distdir [leavedb]";
18my $leavedb = shift or 0;
5a2cc1bd 19
5bbf7309 20my $mysql = mysql_name();
5a2cc1bd 21
5bbf7309 22my $cfg = BSE::Install::cfg();
5a2cc1bd 23
5bbf7309 24my $manifest = maniread();
5a2cc1bd 25
5bbf7309
TC
26install_files("site/htdocs/", public_html_dir());
27install_files("site/templates/", templates_dir());
28install_files("site/cgi-bin", cgi_dir());
29install_files("site/util/", util_dir());
30install_files("site/data/", data_dir());
5a2cc1bd 31
5bbf7309 32my $perl = BSE::Install::perl();
f0543260
TC
33if ($perl ne '/usr/bin/perl') {
34 my $manifest = ExtUtils::Manifest::maniread();
35
6271b583 36 for my $file (keys %$manifest) {
5bbf7309
TC
37 (my $work = $file) =~ s!^site/!!;
38 $work =~ s(^(cgi-bin|util)/)()
39 or next;
40 my $base = $work eq "util" ? util_dir() : cgi_dir();
41 my $full = File::Spec->catfile($base, $work);
6271b583
TC
42 open my $script, "<", $full
43 or next;
44 binmode $script;
45 my $first = <$script>;
46 if ($first =~ s/^#!\S*perl\S*/#!$perl/) {
47 my @all = <$script>;
48 close $script;
49 open my $out_script, ">", $full or die "Cannot create $full: $!";
50 binmode $out_script;
51 print $out_script $first, @all;
52 close $out_script;
53 }
f0543260
TC
54 }
55}
56
3bc94f98 57print "Updating conf\n";
d2473dc2 58
5bbf7309
TC
59my $conf_src = BSE::Install::conffile();
60my $conf_dest = File::Spec->catfile(cgi_dir(), "bse-install.cfg");
61copy($conf_src, $conf_dest)
62 or die "Cannot copy $conf_src to $conf_dest: $!\n";
3bc94f98 63
771ab646
TC
64#-d $uploads
65# or mkdir $uploads, 0777
66# or die "Cannot find or create upload directory: $!";
9168c88c 67
5bbf7309
TC
68my $dbuser = BSE::Install::db_user();
69my $dbpass = BSE::Install::db_password();
9168c88c 70
3bc94f98 71# build the database
5bbf7309 72my $dsn = BSE::Install::db_dsn();
f1fb6e3b
TC
73if ($dsn =~ /:mysql:(?:database=)?(\w+)/) {
74 my $db = $1;
75
76 unless ($leavedb) {
3bc94f98
TC
77 system "$mysql -u$dbuser -p$dbpass $db <$dist/schema/bse.sql"
78 and die "Cannot initialize database";
5bbf7309 79 system "cd ".util_dir." ; $perl initial.pl"
3bc94f98
TC
80 and die "Cannot load database";
81 }
f1fb6e3b
TC
82
83 # always load stored procedures
84 system qq($mysql "-u$dbuser" "-p$dbpass" "$db" <$dist/schema/bse_sp.sql)
85 and die "Error loading stored procedures\n";
86}
87else {
88 print "WARNING: cannot install to $dsn database\n";
5a2cc1bd 89}
3bc94f98 90
5bbf7309
TC
91sub install_files {
92 my ($prefix, $destbase) = @_;
93
94 print "Install $prefix to $destbase\n";
95 for my $file (sort grep /^\Q$prefix/, keys %$manifest) {
96 (my $rel = $file) =~ s/^\Q$prefix//;
97 my $src = File::Spec->catfile($dist, $file);
98 my $dest = File::Spec->catfile($destbase, $rel);
99 my ($destvol, $destdir) = File::Spec->splitpath($dest);
100 my $destpath = File::Spec->catdir($destvol, $destdir);
101 unless (-e $destpath) {
102 make_path($destpath); # croak on error
103 }
104 elsif (!-d $destpath) {
105 die "$destpath isn't a directory!\n";
106 }
107 print " Copy $rel to $dest\n" if $verbose;
108 copy($src, $dest)
109 or die "Cannot copy $src to $dest: $!\n";
110 }
111}