#!/usr/bin/perl

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

require '/scripts/regsrep.pl';


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%';
my $installedversion = %installedversion%;
my $system = `uname -s`;
chomp($system);

require '${installdir}/bin/sql_tables.pl';

print "We are upgrading in ${installdir}...\n";

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

system("rm","-rf","${installdir}/cpanel-upgrade");

if ($installedversion < 0.8.4) {
   print "Adding Karma Database<br/>";
   my $dbh = DBI->connect("DBI:mysql:${sqldb}:${sqlserver}:",$sqluser,$sqlpass);
   if (!defined($dbh)) {
      print "Unable to connect to database.\n";
      exit;
   }
   
   my $create;
   $create = "CREATE TABLE karma (id bigint(20) NOT NULL AUTO_INCREMENT, c_id int(11) NOT NULL, tech_id int(11) NOT NULL,ticket_id bigint(20) NOT NULL,text varchar(255) NOT NULL default '',value int(1) NOT NULL,PRIMARY KEY( id )) TYPE=MyISAM";
   $create->execute();
}
if ($installedversion < 0.8.5) {
   print "Updating Config Database<br/>";
   my $dbh2 = DBI->connect("DBI:mysql:${sqldb}:${sqlserver}:",$sqluser,$sqlpass);
   if (!defined($dbh)) {
      print "Unable to connect to database.\n";
      exit;
   }
   
   my $insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('path','${installpath}/')");
   $insert->execute;
}
print "<br/><b>Complete!</b>";
print "<br/>You can access your upgraded cPSupport installation at: <a href='http://${domain}${installpath}/'>http://${domain}${installpath}/</a>";

