#!/usr/bin/perl
# Resin Install for cPanel
# (c) 2004 cPanel Inc
if ( -e "/usr/local/jakarta/tomcat" || -e "/var/cpanel/cptomcat" || -e "/usr/local/apache/libexec/mod_jk.so" ) {
	print "<b>You can only install either Resin or Tomcat, not both!\nInstall Failed\n</b>";
	system("echo \"failed\" > /var/cpanel/addonmoduleversions/resin");
	exit();
}

$mirror = "http://layer1.cpanel.net/buildapache/1/";
$resinver = "3.0.9";
$resinfile = "resin-${resinver}.tar.gz";
$resindir = "resin-${resinver}";
chomp($system = `uname -s`);
chomp($workdir = `pwd`);

if ($system =~ /freebsd/) {
   print "<b>Cannot install due to lack of support for Java from Sun Microsystems\n<b>";
   exit();
}

if ( -e "/var/cpanel/cpresin" ) {
   print "<b>Old config files found, backing up now</b>\n";
   system("/etc/init.d/resin","stop");
   mkdir("/tmp/cpresin");
   system("cp","-Rf","/usr/local/resin/conf","/tmp/cpresin");
}

print "<b>Verifying that Java is installed and up to date...</b>\n";
if ( ! -e "/scripts/installjava" ) {
   system("cp","installjava","/scripts");
}
system("/scripts/installjava");

print "<b>Now installing Resin\n</b>";
chdir("/usr/src");
system("wget","-O","${resinfile}","${mirror}/${resinfile}");
system("tar","-zxf","${resinfile}");
chdir("/usr/src/${resindir}");
system("./configure --with-apache=/usr/local/apache --with-apxs=/usr/local/apache/bin/apxs --prefix=/usr/local/resin");
system("make");
system("make install");
chdir($workdir);
system("cp","-f","addresin","/scripts/");
system("cp","-f","resin-a.sh","/usr/local/resin/bin");
system("cp","-f","resin","/etc/chkserv.d");
if ($system =~ /freebsd/ ) {
   system("cp","-f","resin-a.sh","/usr/local/rc.d/");
} else {
   system("cp","-f","resin-a.sh","/etc/init.d/resin.sh");
}

if ( -e "/var/cpanel/cpresin" ) {
   print "<b>Found backups, restoring them now</b>\n";
   system("cp","-Rf","/tmp/cpresin","/usr/local/resin");
   system("rm","-rf","/tmp/cpresin");
}

print "<b>Restarting system services</b>\n";
system("/scripts/restartsrv_httpd");
system("/etc/init.d/chkservd","restart");
system("/etc/init.d/resin.sh","start");
system("touch","/var/cpanel/cpresin");
print "<b>Done</b>\n";

print "<b><i>To add JSP/Servlets to a domain run /scripts/addresin</b></i>\n";
