#!/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...\n";

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

system("mv $installdir/pkg/html/* $installdir/");

regsrep("$installdir/config.php","dbhost = \"","\$dbhost='$sqlserver';");
regsrep("$installdir/config.php","dbuname = \"","\$dbuname='$sqluser';");
regsrep("$installdir/config.php","dbpass = \"","\$dbpass='$sqlpass';");
regsrep("$installdir/config.php","dbname = \"","\$dbname='$sqldb';");
unlink("$installdir/config.php.tmpeditlib");

chmod(0777,"$installdir/ultramode.txt");

print "<b>Complete!</b>\n";

print "<br>You can access your new PHP Nuke installation at: <a href=\"http://$domain/$installpath/\">http://${domain}${installpath}/</a><br>You should setup a superuser right away.\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);
}


