#!/usr/bin/perl

BEGIN {
	push(@INC,"/usr/local/cpanel");
}

use SafeFile;

#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";

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

#system("cp -af $installdir/pkg/extras $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);
}


