#!/usr/bin/perl
# cpanel12 - install (mod_mono)              Copyright(c) 2004-2007 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use lib '/usr/local/cpanel';
use strict;
use warnings;
use Cpanel::SafeRun ();

eval {
    require Cpanel::Config;
    if ( defined &Cpanel::Config::is_ea3 ) {
        if ( Cpanel::Config::is_ea3() ) {
            print "This module is now managed via easyapache.\n";
            print "To keep this message from appearing: ";
            print "Remove its entry from /var/cpanel/addonmodules\n";
            exit;
        }
    }
};

my $apache_ver = get_httpd_version();
if ( $apache_ver !~ m/^1\.3/ ) {
    print <<"EOM";


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Unable to install the Mono addon. This addon is designed specifically
for Apache 1.3. You are currently running Apache $apache_ver. This addon
has been included in the latest version of EasyApache.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


EOM
    exit;
}
else {
    print "Detected Apache $apache_ver\n";
}


umask(0022);

my $mirror      = "http://layer1.cpanel.net/buildapache/1/mod_mono";
my $workdir     = "/usr/src/mod_mono";
my $glibver     = "2.0.7";
my $glibfile    = "glib-${glibver}.tar.gz";
my $glibdir     = "glib-${glibver}";
my $icuver      = "3.0";
my $icufile     = "icu-${icuver}.tgz";
my $icudir      = "icu";
my $modmonofile = "mod_mono-1.0.2.tar.gz";
my $modmonodir  = "mod_mono-1.0.2";
my $monofile    = "mono-1.0.2.tar.gz";
my $monodir     = "mono-1.0.2";
my $xspfile     = "xsp-1.0.2.tar.gz";
my $xspdir      = "xsp-1.0.2";

my $installdir  = `pwd`;
chomp $installdir;
my $instver = `cat ${installdir}/version`;
chomp $instver;
my $distdir = `pwd`;
chomp $distdir;


print "<b>Now starting the install of mod_mono to support asp.net on this server</b>\n";
print "<b>This is cPanel installer version ${instver}</b>\n";

system( 'rm', '-rf', $workdir );
mkdir $workdir;
chdir $workdir or die "Failed to chdir into $workdir: $!";

my $glibcurver = `glib-genmarshal --version 2>&1 | grep version | cut -c 25-29`;
if ( $glibcurver ne $glibver ) {
    print "<b>Downloading and installing ${glibfile}...</b>\n";
    system 'wget', '-O', $glibfile, $mirror . '/' . $glibfile;
    system 'tar', '-zxf', $glibfile;
    chdir $workdir . '/' . $glibdir or die "Unable to chdir into $workdir/$glibdir: $!";
    system './configure', '--prefix=/usr';
    system 'make';
    system 'make', 'install';
    chdir $workdir or die "Failed to chdir into $workdir: $!";

    print "<b>Done</b>\n";
}
else {
    print "<b>glib is currently up to date and will not be reinstalled</b>\n";
}

my $icucurver = `icu-config --version`;
if ( $icucurver ne $icuver ) {
    print "<b>Downloading and installing ${icufile}...</b>\n";
    system 'wget', '-O', $icufile, $mirror . '/' . $icufile;
    if ( !-e $icufile ) {
        die "Failed to fetch $icufile\n";
    }
    system 'tar', '-zxf', $icufile;
    chdir $workdir . '/' . $icudir . '/source' or die "Unable to chdir into $workdir/$icudir/source: $!";
    system './configure', '--prefix=/usr';
    system 'make';
    system 'make', 'install';
    chdir $workdir or die "Failed to chdir into $workdir: $!";
    print "<b>Done\n</b>";
}
else {
    print "<b>icu is up to date and will not be reinstalled</b>\n";
}

print "<b>Downloading and installing ${monofile}...</b>\n";
system 'wget', '-O', $monofile, $mirror . '/' . $monofile;
system 'tar', '-zxf', $monofile;
chdir $workdir . '/' . $monodir or die "Unable to chdir into $workdir/$monodir: $!";
system './configure', '--prefix=/usr';
system 'make';
system 'make', 'install';
chdir $workdir or die "Failed to chdir into $workdir: $!";
print "<b>Done</b>\n";


print "<b>Downloading and installing $xspfile ...</b>\n";
system 'wget', '-O', $xspfile, $mirror . '/' . $xspfile;
system 'tar', '-zxf', $xspfile;
chdir $workdir . '/' . $xspdir or die "Unable to chdir into $workdir/$xspdir: $!";
system './configure', '--prefix=/usr';
system 'make';
system 'make', 'install';
chdir $workdir or die "Failed to chdir into $workdir: $!";
print "<b>Done</b>\n";


my $modbuilddir = "/usr/src/modmono_apache/$modmonodir";

system 'rm', '-rf', '/usr/src/modmono_apache';
mkdir '/usr/src/modmono_apache', 0700;
if ( !-d $modbuilddir ) { 
    mkdir $modbuilddir, 0700;
}
chdir '/usr/src/modmono_apache' or die "Unable to chdir into /usr/src/modmono_apache: $!";

print "<b>Downloading and installing ${modmonofile}...</b>\n";
system( "wget", "-O", "${modmonofile}", "${mirror}/${modmonofile}" );
system( "tar", "-zxf", "${modmonofile}" );
if ( !-d "/var/cpanel/apachemod" ) { mkdir( "/var/cpanel/apachemod", 0700 ); }
system 'cp', '-f', $distdir . '/modmono.apache', '/var/cpanel/apachemod';
system 'chmod', '0755', '/var/cpanel/apachemod/modmono.apache';
system '/var/cpanel/apachemod/modmono.apache';
system '/scripts/restartsrv', 'httpd';
print "<b>Done</b>\n";

chdir '/';

print "<b>Cleaning up install files...</b>";
system 'cp', '-f', $installdir . '/addasp', '/scripts/';
system 'chmod', '0755', '/scripts/addasp';
system 'rm',    '-rf',  $workdir;
print "<b>Done</b>\n";

print "<b><i>Please note that this install is considered very early beta! It will most likely change drasticly in the near future!\n";
print "In order to allow users to use .aspx files please run the following from the command line:\n";
print "/scripts/addasp</b></i>\n";

sub get_httpd_version {
    my $bin = '/usr/local/apache/bin/httpd';
    return 'UNKNOWN' if !-x $bin;
    if ( Cpanel::SafeRun::saferunallerrors( $bin, '-v' ) =~ m{Apache/(\d+\.\d+\.\d+)}m ) {
        return $1;
    }
    return 'UNKNOWN';
}

