use strict;
#use File::Tree;
use File::Copy;
-use lib 't';
-use BSE::Test ();
-require ExtUtils::Manifest;
+use lib 'lib';
+use BSE::Install qw(util_dir cgi_dir public_html_dir templates_dir data_dir mysql_name);
+#use BSE::Test ();
+use ExtUtils::Manifest qw(maniread);
+use File::Copy qw(copy);
+use File::Spec;
+use File::Path qw(make_path);
+use Getopt::Long;
+
+my $verbose;
+GetOptions("v|verbose" => \$verbose);
my $dist = shift or die "Usage: $0 distdir [leavedb]";
my $leavedb = shift or 0;
-my $instbase = shift || BSE::Test::base_dir() || die "No base_dir";
-my $mysql = BSE::Test::mysql_name;
+my $mysql = mysql_name();
-# if (-e "$instbase/cgi-bin/modules/Constants.pm"
-# && !-e "$instbase/Constants.pm") {
-# system "cp $instbase/cgi-bin/modules/Constants.pm $instbase/Constants.pm"
-# }
+my $cfg = BSE::Install::cfg();
-system("rm -rf $instbase/cgi-bin")
- and die "Cannot remove cgi-bin";
-system "rm -rf $instbase/data"
- and die "Cannot remove data";
-system "rm -f $instbase/htdocs/{*.html,a/*.html,shop/*.html,images/*.jpg}"
- and die "Cannot remove htdocs";
+my $manifest = maniread();
-system "cp -rf $dist/site/cgi-bin $instbase"
- and die "Cannot copy cgi-bin";
+# fake BSE::Modules
+$manifest->{"site/cgi-bin/modules/BSE/Modules.pm"} = "";
-my $perl = BSE::Test::test_perl();
+install_files("site/htdocs/", public_html_dir());
+install_files("site/templates/", templates_dir());
+install_files("site/cgi-bin", cgi_dir());
+install_files("site/util/", util_dir());
+install_files("site/data/", data_dir());
+
+my $perl = BSE::Install::perl();
if ($perl ne '/usr/bin/perl') {
my $manifest = ExtUtils::Manifest::maniread();
- for my $file (grep /\.pl$/, keys %$manifest) {
- (my $work = $file) =~ s!^site!!;
- next unless $work =~ /cgi-bin/;
- my $full = $instbase . $work;
- open SCRIPT, "< $full" or die "Cannot open $full: $!";
- binmode SCRIPT;
- my @all = <SCRIPT>;
- close SCRIPT;
- $all[0] =~ s/^#!\S*perl\S*/#!$perl/;
- open SCRIPT, "> $full" or die "Cannot create $full: $!";
- binmode SCRIPT;
- print SCRIPT @all;
- close SCRIPT;
+ for my $file (keys %$manifest) {
+ (my $work = $file) =~ s!^site/!!;
+ $work =~ s(^(cgi-bin|util)/)()
+ or next;
+ my $base = $work eq "util" ? util_dir() : cgi_dir();
+ my $full = File::Spec->catfile($base, $work);
+ open my $script, "<", $full
+ or next;
+ binmode $script;
+ my $first = <$script>;
+ if ($first =~ s/^#!\S*perl\S*/#!$perl/) {
+ my @all = <$script>;
+ close $script;
+ open my $out_script, ">", $full or die "Cannot create $full: $!";
+ binmode $out_script;
+ print $out_script $first, @all;
+ close $out_script;
+ }
}
}
-system "cp -rf $dist/site/htdocs $instbase"
- and die "Cannot copy htdocs";
-system "cp -rf $dist/site/templates $instbase"
- and die "Cannot copy templates";
-system "cp -rf $dist/site/data $instbase"
- and die "Cannot copy data";
-system "cp -rf $dist/site/util $instbase";
-
print "Updating conf\n";
-# try to update Constants.pm
-open CON, "< $instbase/cgi-bin/modules/Constants.pm"
- or die "Cannot open Constants.pm";
-my $con = do { local $/; <CON> };
-close CON;
-
-my $dbuser = BSE::Test::test_dbuser();
-my $dbpass = BSE::Test::test_dbpass();
-
-$con =~ s/(^\$DSN = ')[^']*/$1 . BSE::Test::test_dsn()/me;
-$con =~ s/(^\$DBCLASS = ')[^']*/$1 . BSE::Test::test_dbclass()/me;
-$con =~ s/(^\$UN = ')[^']*/$1$dbuser/m;
-$con =~ s/(^\$PW = ')[^']*/$1$dbpass/m;
-$con =~ s/(^\$BASEDIR = ')[^']+/$1 . BSE::Test::base_dir/me;
-#$con =~ s/(^\$URLBASE = ["'])[^'"]+/$1 . BSE::Test::base_url/me;
-#$con =~ s/(^\$SECURLBASE = ["'])[^'"]+/$1 . BSE::Test::test_securl/me;
-$con =~ s/(^\$SESSION_CLASS = [\"\'])[^\'\"]+/$1 . BSE::Test::test_sessionclass()/me;
-open CON, "> $instbase/cgi-bin/modules/Constants.pm"
- or die "Cannot open Constants.pm for write: $!";
-print CON $con;
-close CON;
-
-# rebuild the config file
-# first load values from the test.cfg file
-my $conffile = BSE::Test::test_conffile();
-my %conf;
-$conf{site}{name} = "Test Server";
-$conf{site}{url} = BSE::Test::base_url();
-$conf{site}{secureurl} = BSE::Test::base_securl();
-my $uploads = "$instbase/uploads";
-$conf{paths}{downloads} = $uploads;
-my $templates = "$instbase/templates";
-$conf{paths}{templates} = $templates;
-open TESTCONF, "< $conffile"
- or die "Could not open config file $conffile: $!";
-while (<TESTCONF>) {
- chomp;
- /^\s*(\w[^=]*\w)\.(\w+)\s*=\s*(.*\S)\s*$/ or next;
- $conf{lc $1}{lc $2} = $3;
-}
-$uploads = $conf{paths}{downloads};
-# fix bse.cfg
-open CFG, "< $instbase/cgi-bin/bse.cfg"
- or die "Cannot open $instbase/cgi-bin/bse.cfg: $!";
-my $section = "";
-my @cfg;
-while (<CFG>) {
- chomp;
- if (/^\[(.*)\]\s*$/) {
- my $newsect = lc $1;
- if ($conf{$section} && keys %{$conf{$section}}) {
- for my $key (sort keys %{$conf{$section}}) {
- push @cfg, "$key=$conf{$section}{$key}";
- }
- delete $conf{$section};
- }
- $section = $newsect;
- }
- elsif (/^\s*(\w+)\s*=\s*.*$/ && exists $conf{$section}{lc $1}) {
- my $key = lc $1;
- print "found $section.$key\n";
- $_ = "$key=$conf{$section}{$key}";
- delete $conf{$section}{$key};
- }
- push @cfg, $_;
-}
-if ($conf{$section} && keys %{$conf{$section}}) {
- for my $key (sort keys %{$conf{$section}}) {
- push @cfg, "$key=$conf{$section}{$key}";
- }
- delete $conf{$section};
-}
-for my $sect (keys %conf) {
- if ($conf{$sect} && keys %{$conf{$sect}}) {
- push @cfg, "[$sect]";
- for my $key (sort keys %{$conf{$sect}}) {
- push @cfg, "$key=$conf{$sect}{$key}";
- }
- push @cfg, "";
- }
-}
-close CFG;
-
-open CFG, "> $instbase/cgi-bin/bse.cfg"
- or die "Cannot create $instbase/cgi-bin/bse.cfg: $!";
-for my $line (@cfg) {
- print CFG $line, "\n";
-}
-close CFG;
+my $conf_src = BSE::Install::conffile();
+my $conf_dest = File::Spec->catfile(cgi_dir(), "bse-install.cfg");
+copy($conf_src, $conf_dest)
+ or die "Cannot copy $conf_src to $conf_dest: $!\n";
--d $uploads
- or mkdir $uploads, 0777
- or die "Cannot find or create upload directory: $!";
+#-d $uploads
+# or mkdir $uploads, 0777
+# or die "Cannot find or create upload directory: $!";
+my $dbuser = BSE::Install::db_user();
+my $dbpass = BSE::Install::db_password();
# build the database
-unless ($leavedb) {
- my $dsn = BSE::Test::test_dsn();
- if ($dsn =~ /:mysql:(?:database=)?(\w+)/) {
- my $db = $1;
+my $dsn = BSE::Install::db_dsn();
+if ($dsn =~ /:mysql:(?:database=)?(\w+)/) {
+ my $db = $1;
+
+ unless ($leavedb) {
system "$mysql -u$dbuser -p$dbpass $db <$dist/schema/bse.sql"
and die "Cannot initialize database";
- system "cd $instbase/util ; perl initial.pl"
+ system "cd ".util_dir." ; $perl initial.pl"
and die "Cannot load database";
}
- else {
- print "WARNING: cannot install to $dsn database\n";
- }
+
+ # always load stored procedures
+ system qq($mysql "-u$dbuser" "-p$dbpass" "$db" <$dist/schema/bse_sp.sql)
+ and die "Error loading stored procedures\n";
+}
+else {
+ print "WARNING: cannot install to $dsn database\n";
}
-
+sub install_files {
+ my ($prefix, $destbase) = @_;
+
+ print "Install $prefix to $destbase\n";
+ for my $file (sort grep /^\Q$prefix/, keys %$manifest) {
+ (my $rel = $file) =~ s/^\Q$prefix//;
+ my $src = File::Spec->catfile($dist, $file);
+ my $dest = File::Spec->catfile($destbase, $rel);
+ my ($destvol, $destdir) = File::Spec->splitpath($dest);
+ my $destpath = File::Spec->catdir($destvol, $destdir);
+ unless (-e $destpath) {
+ make_path($destpath); # croak on error
+ }
+ elsif (!-d $destpath) {
+ die "$destpath isn't a directory!\n";
+ }
+ print " Copy $rel to $dest\n" if $verbose;
+ copy($src, $dest)
+ or die "Cannot copy $src to $dest: $!\n";
+ }
+}