#!/usr/bin/perl
#
#Copyright (c) 2001-2005 cPanel, Inc.
#
#This auto install script, "install" is hereinafter referred to as the "Software."  No other parts of this package
#will be considered, infered or implied to be part of the the "Software."
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
#documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
#rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
#Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
############################################################################################
BEGIN {
	push(@INC,"/usr/local/cpanel");
}

use SafeFile;
require '/scripts/regsrep.pl';

#VARIBLES
my $domain = '%domain%';
my $installpath = '%installpath%';
my $modulename = '%modulename%';
my $modulepath = '%modulepath%';
my $homedir = '%homedir%';
my $user = '%user%';
my $installdir = '%installdir%';
my $publichtml = '%public_html%';
my $sqldb = '%sqldb%';
my $sqluser = '%sqluser%';
my $sqlpass = '%sqlpass%';
my $sqlserver = '%sqlhost%';

print "<b>We are installing in $installdir...\n</b><br/>";

print "<b>Moving installation files into place...</b>";
system("mv $installdir/pkg/blogs/* $installdir/");
print "<b>Done\n</b><br/>";

print "<b>Setting up config file...</b>";
my $email = `cat ~/.contactemail`;
chomp($email);
if ( ! $email =~ /^[-\w.]+@([A-z0-9]+\.)+[A-z]{2,4}$/ ) {
	$email = `whoami` . "@${domain}";
}

regsrep("$installdir/conf/_config.php",'DB_USER',"define( 'DB_USER', '${sqluser}' );");
regsrep("$installdir/conf/_config.php",'DB_PASSWORD',"define( 'DB_PASSWORD', '${sqlpass}' );");
regsrep("$installdir/conf/_config.php",'DB_NAME',"define( 'DB_NAME', '${sqldb}' );");
regsrep("$installdir/conf/_config.php",'DB_HOST',"define( 'DB_HOST', '${sqlhost}' );");
regsrep("$installdir/conf/_config.php",'\$baseurl',"\$baseurl = 'http://${domain}${installpath}';");
regsrep("$installdir/conf/_config.php",'\$admin_email',"\$admin_email = '${email}';");
regsrep("$installdir/conf/_config.php",'\$config_is_done',"\$config_is_done = 1;");
print "<b>Done<br/>\n";

print "<b>Doing mysql database installation...</b>";
my $install = `curl http://${domain}${installpath}/install/index.php?action=newdb`;
if ( $install =~ /with the login "(.*)" and password "(.*)"/ ) {
	$username = $1;
	$password = $2;
}
print "<b>Done<br/></b>\n";

chmod(0000,"${installdir}/install");
system("rm -rf ${installdir}/pkg");

print "<b>Installation Finished<br/>";
print "<b>To access your b2evolution installation go to: <a href='http://${domain}${installpath}/'>http://${domain}${installpath}/</a></br>";
print "<b>To access the admin area of your WordPress installation go to: <a href='http://${domain}${installpath}/admin/'>http://${domain}${installpath}/admin/</a><br/>";
print "<b>The default randomly generated username and password are:</b><br/>";
print "<b>Username: $username</b><br/>";
print "<b>Password: $password</b><br/>";