#!/usr/bin/perl
#

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

if (!eval("use HTML::Template; 1;")) {
   print "<h1>HTML::Template not found.<br/>";
   print "Please have your system admin install this prior to installing cPSupport!</h1>";
   exit;
}
if (!eval("use GD::SecurityImage; 1;")) {
   print "<h1>GD::SecurityImage not found<br/>";
   print "Please have your system admin install this prior to installing cPSupport!</h1>";
   exit;
}


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

require "${installdir}/pkg/bin/sql_tables.pl";
unlink("${installdir}/pkg/bin/sql_tables.pl");

# Copy public_html files over to 
if ($system =~ /freebsd/i) {
   system("cp -Rpf ${installdir}/pkg/* ${installdir}/");
} else {
   system("cp -af ${installdir}/pkg/* ${installdir}/");
}
system("chmod -R 755 ${installdir}/*");
system("rm -rf ${installdir}/pkg");
# Create database tables.
print "Creating database tables..<br/>";
my $dbh = DBI->connect("DBI:mysql:${sqldb}:${sqlserver}:",$sqluser,$sqlpass);
if (!defined($dbh)) {
   print "Unable to connect to database.\n";
   exit;
}

print "Initializing databases..<br/>";
foreach my $table (@sql_tables) {
   my $insert = $dbh->prepare($table);
   $insert->execute;
}

foreach my $data (@sql_data) {
   my $insert = $dbh->prepare($data);
   $insert->execute;
}

my $insert;
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('domain','${domain}')");
$insert->execute;
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('contact','${user}\@${domain}')");
$insert->execute;
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('theme','default')");
$insert->execute;
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('emailsupport','n')");
$insert->execute;
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('baseurl','http://${domain}${installpath}/')");
$insert->execute;
$insert = $dbh->prepare("INSERT INTO users(name,level,password,class,realname) VALUES(?,'6',ENCRYPT(?,?),'admin','Administrator')");
$insert->execute($username,$password,$username);
$insert = $dbh->prepare("INSERT INTO config(name,value) VALUES('path','${installpath}/')");
$insert->execute;


# Create configuration file.
if (! -e "${homedir}/etc" ) {
   mkdir("${homedir}/etc",755);
}
if (!defined($sqlserver) || $sqlserver eq "") {
   $sqlserver = "localhost";
}
print "Creating config file...<br/>";
open(CONF,">","${homedir}/etc/support.conf");
print CONF "dbase=${sqldb}\n";
print CONF "user=${sqluser}\n";
print CONF "password=${sqlpass}\n";
print CONF "host=${sqlserver}\n";
close(CONF);
chmod(0700,"${homedir}/etc/support.conf");

open(CGI,">","${installdir}/config.cgi");
print CGI "push \@INC, \"${installdir}/includes/\";";
close(CGI);

print "Adding cronjob...<br/>";
@CL = split(/\n/, `crontab -l`);
push(@CL,"01 * * * * ${installdir}/bin/waitnotice");
open(CRON,">.crontab");
foreach $line (@CL) {
   print CRON "$line\n";
}
close(CRON);
system("crontab .crontab");
unlink(".crontab");



# Print install information..
print "<br/>You can access your new cPSupport installation at: <a href=\"http://${domain}${installpath}/\">http://${domain}${installpath}/</a>\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";
