#!/usr/bin/perl
use strict;
use FindBin qw($Bin);
use lib $Bin;
use onserver;
use File::Path;

setup();

print "\nPlease decide upon a title for your site and enter it below.\n";
print "Desired title: ";
my $title = <STDIN>;
chomp($title);

print "\nSetting up your configuration file...\n";
open CONFIGTEMPLATE, "configuration.php-dist";
open CONFIGURATION, ">configuration.php";
while (<CONFIGTEMPLATE>) {
  chomp;
  if (/var \$sitename /) {
    $_ = "	var \$sitename = '$title';";
  } elsif (/var \$dbtype /) {
    $_ = "	var \$dbtype = 'mysql';";
  } elsif (/var \$host /) {
    $_ = "	var \$host = '$sqlhost';";
  } elsif (/var \$user /) {
    $_ = "	var \$user = '$sqluser';";
  } elsif (/var \$password /) {
    $_ = "	var \$password = '$sqlpass';";
  } elsif (/var \$db /) {
    $_ = "	var \$db = '$sqldb';";
  } elsif (/var \$dbprefix /) {
    $_ = "	var \$dbprefix = 'jos_';";
  } elsif (/var \$secret /) {
    my $random = `dd if=/dev/urandom bs=1k count=1 | md5sum | cut -c1-32`;
    $random =~ s/\n//;
    $_ = "	var \$secret = '$random';";
  } elsif (/var \$mailfrom /) {
    $_ = "	var \$mailfrom = '$email';";
  } elsif (/var \$fromname /) {
    $_ = "	var \$fromname = '$title';";
  }
  print CONFIGURATION "$_\n";
}
close CONFIGURATION;
close CONFIGTEMPLATE;

print "\nInitializing database schema...\n";
my $schemafile = "installation/sql/mysql/joomla-real.sql";
open SCHEMATEMPLATE, "installation/sql/mysql/joomla.sql";
open SCHEMA, ">$schemafile";
while (<SCHEMATEMPLATE>) {
  if (/#__/) {
    $_ =~ s/#__/jos_/g;
  }
  print SCHEMA "$_";
}
close SCHEMA;
close SCHEMATEMPLATE;
system("cat $schemafile | mysql $sqldb");

print "\nLoading sample data...\n";
my $sampledatafile = "installation/sql/mysql/sample_data-real.sql";
open SAMPLETEMPLATE, "installation/sql/mysql/sample_data.sql";
open SAMPLE, ">$sampledatafile";
while (<SAMPLETEMPLATE>) {
  if (/#__/) {
    $_ =~ s/#__/jos_/g;
  }
  print SAMPLE "$_";
}
close SAMPLE;
close SAMPLETEMPLATE;
system("cat $sampledatafile | mysql $sqldb");

print "\nCreating your admin account...\n";
system("mysql -e \"INSERT INTO jos_users VALUES (62, 'Administrator', '$admin_username', '$email', MD5('$admin_password'), 'Super Administrator', 0, 1, 25, NOW(), NOW(), '', '')\" $sqldb");
system("mysql -e \"INSERT INTO jos_core_acl_aro VALUES (10, 'users', '62', 0, 'Administrator', 0)\" $sqldb");
system("mysql -e \"INSERT INTO jos_core_acl_groups_aro_map VALUES (25, '', 10)\" $sqldb");

print "\nCleaning up installation tree...\n";
rmtree('installation');

print "\nDone!\n";
exit 0;

