#!/usr/bin/perl
#
#Copyright (c) 2001-2004 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;
use POSIX;

#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%';

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

my($system, $nodename, $release, $version, $machine) = POSIX::uname();

system("mysql -u${sqluser} -p${sqlpass} ${sqldb} < $installdir/pkg//catalog/install/oscommerce.sql");

if ($system =~ /freebsd/i) {
 system("cp -Rpf $installdir/pkg/catalog/* $installdir/");
      } else {
 system("cp -af $installdir/pkg/catalog/* $installdir/");
}

regsrep("$installdir/includes/configure.php","\\(\'HTTP_SERVER\'","define('HTTP_SERVER', 'http://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTPS_SERVER\'","define('HTTPS_SERVER', 'https://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTP_COOKIE_DOMAIN\'","define('HTTP_COOKIE_DOMAIN', 'http://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTPS_COOKIE_DOMAIN\'","define('HTTPS_COOKIE_DOMAIN', 'https://$domain');");
regsrep("$installdir/includes/configure.php","\\(\'HTTP_COOKIE_PATH\'","define('HTTP_COOKIE_PATH', '/');");
regsrep("$installdir/includes/configure.php","\\(\'HTTPS_COOKIE_PATH\'","define('HTTPS_COOKIE_PATH', '/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_HTTP_CATALOG\'","define('DIR_WS_HTTP_CATALOG', '${installpath}/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_HTTPS_CATALOG\'","define('DIR_WS_HTTPS_CATALOG', '${installpath}/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_IMAGES\'","define('DIR_WS_IMAGES', '${installpath}/images/');");
regsrep("$installdir/includes/configure.php","\\(\'DIR_WS_ICONS\'","define('DIR_WS_ICONS', '${installpath}/icons/');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER\'","define('DB_SERVER', '$sqlserver');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER_USERNAME\'","define('DB_SERVER_USERNAME', '$sqluser');");
regsrep("$installdir/includes/configure.php","\\(\'DB_SERVER_PASSWORD\'","define('DB_SERVER_PASSWORD', '$sqlpass');");
regsrep("$installdir/includes/configure.php","\\(\'DB_DATABASE\'","define('DB_DATABASE', '$sqldb');");
regsrep("$installdir/includes/configure.php","\\(\'STORE_SESSIONS\'","define('STORE_SESSIONS', 'mysql');");
unlink("$installdir/includes/configure.php.tmpeditlib");

regsrep("$installdir/admin/includes/configure.php","\\(\'HTTP_SERVER\'","define('HTTP_SERVER', 'http://$domain');");
regsrep("$installdir/admin/includes/configure.php","\\(\'HTTPS_SERVER\'","define('HTTPS_SERVER', 'https://$domain');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DIR_WS_ADMIN\'","define('DIR_WS_ADMIN', '${installpath}/admin/');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DIR_WS_CATALOG\'","define('DIR_WS_CATALOG', '${installpath}/');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER\'","define('DB_SERVER', '$sqlserver');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER_USERNAME\'","define('DB_SERVER_USERNAME', '$sqluser');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_SERVER_PASSWORD\'","define('DB_SERVER_PASSWORD', '$sqlpass');");
regsrep("$installdir/admin/includes/configure.php","\\(\'DB_DATABASE\'","define('DB_DATABASE', '$sqldb');");
unlink("$installdir/admin/includes/configure.php.tmpeditlib");

chmod(0777,"$installdir/images");
mkdir("$installdir/admin/backups",0777);
chmod(0777,"$installdir/admin/backups");

#---[ need to check for fileprotect here and chown/chmod appropriately. need root for the chown though; not gonna make them world readable. :/ ]---
if ($hasphpsuexec) {
	chmod(0400,"$installdir/includes/configure.php");
	chmod(0400,"$installdir/admin/includes/configure.php");
} else {
	chmod(0644,"$installdir/includes/configure.php");
	chmod(0644,"$installdir/admin/includes/configure.php");
}

system("rm","-rf","$installdir/install");
link("$installdir/default.php","$installdir/index.php");

print "<b>Complete!</b><br>\n";
print "<br>You can access your new cart at: <a href=\"http://$domain/$installpath/\">http://${domain}${installpath}/</a><br>\n";
print "<br>You can access the admin interface at: <a href=\"http://$domain/$installpath/admin/\">http://${domain}${installpath}/admin/</a> with the username: ${username}, and the password: ${password}.<br>\n";
print "<br><br>\n";

sub regsrep {
   my($file,$old,$new) = @_;
   $filelock = SafeFile::safeopen(\*FH,"$file");
   $fileeditlock = SafeFile::safeopen(\*FHT,">$file.tmpeditlib");
   while(<FH>) {
      if ($_ =~ /^$/ || $_ eq "\n" || $_ eq "\r\n") {
         print FHT "$_";
      } elsif($_ =~ /$old/) {
        $result = $1;
         my $mnew = $new;
        $mnew =~ s/\$1/$result/g;
         print FHT "$mnew\n";
      } else {
         print FHT $_;
      }
   }
   SafeFile::safeclose(\*FH,$filelock);
   SafeFile::safeclose(\*FHT,$fileeditlock);
   $fileeditlock = SafeFile::safeopen(\*FHT,"$file.tmpeditlib");
   $filelock = SafeFile::safeopen(\*FH,">$file");
   while(<FHT>) { print FH $_ };
   SafeFile::safeclose(\*FH,$filelock);
   SafeFile::safeclose(\*FHT,$fileeditlock);
}


