#!/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 IPC::Open3;
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%';
my $username = '%username%';
my $password = '%password%';

##Begin SecretKey Generation##
$lower=1000;
$upper=2000000;
$random = int(rand( $upper-$lower+1 ) ) + $lower;
$random2 = int(rand( $upper-$lower+10 ) ) + $lower;

require Digest::MD5;
$md5 = Digest::MD5->new;
$md5->add($random);
$hashedrandom = $md5->hexdigest;

$md52 = Digest::MD5->new;
$md52->add($random2);
$hashedrandom2 = $md52->hexdigest;
##End SecretKey Generation##


require Digest::MD5;
$md5 = Digest::MD5->new;
$md5->add($password);
$hashedpass = $md5->hexdigest;

system("echo \"INSERT INTO \\`mos_users\\` VALUES (62, 'Administrator', '$username', 'admin@yoursite.com', '$hashedpass', 'superadministrator', 0, 1, 25, '2004-12-12 15:09:41', '0000-00-00 00:00:00', '', '');\" > $installdir/pkg/sql/mamboinstall2.sql");

print "We are installing in $installdir...\n";

system("mv $installdir/pkg/* $installdir/");
system("mysql -u${sqluser} -p${sqlpass} ${sqldb} < $installdir/sql/mamboinstall.sql");
system("mysql -u${sqluser} -p${sqlpass} ${sqldb} < $installdir/sql/mamboinstall2.sql");

regsrep("$installdir/configuration.php","mosConfig_absolute_path = \'","\$mosConfig_absolute_path='$installdir';");
regsrep("$installdir/configuration.php","mosConfig_secret = \'","\$mosConfig_secret = '$hashedrandom\@$hashedrandom2\!';");
regsrep("$installdir/configuration.php","mosConfig_cachepath = \'","\$mosConfig_cachepath = '$installdir/cache';");
regsrep("$installdir/configuration.php","mosConfig_live_site = \'","\$mosConfig_live_site='http\://$domain$installpath';");
regsrep("$installdir/configuration.php","mosConfig_host = \'","\$mosConfig_host = '$sqlserver';");
regsrep("$installdir/configuration.php","mosConfig_user = \'","\$mosConfig_user='$sqluser';");
regsrep("$installdir/configuration.php","mosConfig_password = \'","\$mosConfig_password='$sqlpass';");
regsrep("$installdir/configuration.php","mosConfig_db = \'","\$mosConfig_db='$sqldb';");
unlink("$installdir/configuration.php.tmpeditlib");
system("rm","-rf","$installdir/installation");
#system("rm","-rf","$installdir/sql");
system("rm","-rf","$installdir/pkg");
chmod(0644,"$installdir/configuration.php");
if (hasphpsuexec() eq "0") {
system("chmod","-R","0777","$installdir/administrator/backups/");
system("chmod","-R","0777","$installdir/administrator/components/");
system("chmod","-R","0777","$installdir/administrator/modules/");
system("chmod","-R","0777","$installdir/administrator/templates/");
system("chmod","-R","0777","$installdir/cache/");
system("chmod","-R","0777","$installdir/components/");
system("chmod","-R","0777","$installdir/images/");
system("chmod","-R","0777","$installdir/language/");
system("chmod","-R","0777","$installdir/mambots/");
system("chmod","-R","0777","$installdir/media/");
system("chmod","-R","0777","$installdir/modules/");
system("chmod","-R","0777","$installdir/templates/");
}
print "<b>Complete!</b>\n";

print "<br>You can access your new Mambo Open Source installation at: <a href=\"http://$domain$installpath/\">http://$domain$installpath/</a><br>\n";
print "<br>Here is your administrator login information:\n";
print "<br><b>Administrator Username: $username</b>\n";
print "<br><b>Administrator Password: $password</b>\n";
print "<br><br>\n";

sub hasphpsuexec {
        my $hasphpsuexec = 0;
        open(SUEXEC,"<","/usr/local/apache/bin/suexec");
        while(<SUEXEC>) {
                if (/php /i) {
                        $hasphpsuexec = 1;
                }
        }
        close(SUEXEC);
        return($hasphpsuexec);
}

