package Cpanel::Easy::Utils::ModMono;

# cpanel - Cpanel/Easy/Utils/ModMono.pm            Copyright(c) 2014 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

our $version = q{2.6.3};

sub get_default_mono_easyconfig {

    return {
        'name'         => qq{Mod Mono $version (.Net 1.x)},
        'verify_on'    => 'Requires an experienced mono admin.  Some features in this version of mod_mono only function with Apache 2.0 or 2.2.',
        'version'      => $version,
        'note'         => qq{Experts ONLY: Do not install this unless you have a mono admin},
        'hastargz'     => 1,
        'src_cd2'      => 'mod_mono-2.6.3',
        'ensurepkg'    => [qw{tcl xorg-x11-devel xorg-x11-dev libpng-devel libpng-dev libjpeg-devel libjpeg-dev libX11-devel fontconfig fontconfig-devel freetype freetype-devel libXrender libXrender-devel giflib giflib-devel}],
        'perl_modules' => {
            'XML::Parser' => 1,
        },
        'url'     => 'http://www.mono-project.com/Mod_mono',
        'implies' => { 'Cpanel::Easy::ModMono2' => 0, },
        'modself' => sub {
            my ( $easy, $self_hr, $profile_hr ) = @_;

            # mod_mono is not currently supported with apache 2.4
            if ( $profile_hr->{'Apache'}{'version'} eq '2_4' ) {
                $self_hr->{'hastargz'}                   = 0;
                $self_hr->{'skip'}                       = 1;
                $self_hr->{'treat_as_off_while_skipped'} = 1;
                return ( 0, 'ModMono unsupported with Apache 2.4' );
            }
        },
        'when_i_am_off' => sub {
            my ($self) = @_;
            unlink '/usr/local/cpanel/scripts/addasp' if -e '/usr/local/cpanel/scripts/addasp';
        },
        'step' => {
            '0' => {
                'name'    => 'Checking glib2',
                'command' => sub {
                    my ($self) = @_;
                    return $self->get_path_installed('Cpanel::Easy::OptLib::glib2');
                },
            },
            '1' => {
                'name'    => 'Checking sqlite',
                'command' => sub {
                    my ($self) = @_;
                    return $self->get_path_installed('Cpanel::Easy::OptLib::sqlite');
                },
            },
            '2' => {
                'name'    => 'Checking mono',
                'command' => sub {
                    my ($self) = @_;

                    # -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
                    local $ENV{'CPPFLAGS'}        = '-I/opt/glib2/include/glib-2.0 -I/opt/glib2/lib/glib-2.0/include ' . $ENV{'CPPFLAGS'};
                    local $ENV{'PATH'}            = '/opt/glib2/bin:' . $ENV{'PATH'};
                    local $ENV{'PKG_CONFIG_PATH'} = '/opt/glib2/lib/pkgconfig/';

                    # If we had to custom compile sqlite3 we want it auto-linked to mono since it wont show up in the normal
                    # library search paths...  See http://www.mono-project.com/Interop_with_Native_Libraries for details on how Mono loads
                    # native libraries
                    local $ENV{'LDFLAGS'} = '-L/opt/sqlite/lib -Wl,-rpath,/opt/sqlite/lib ' . $ENV{'LDFLAGS'};
                    local $ENV{'LIBS'}    = '-lsqlite3';
                    return $self->get_path_installed('Cpanel::Easy::OptLib::mono');
                },
            },
            '3' => {
                'name'    => 'Checking xsp',
                'command' => sub {
                    my ($self) = @_;
                    local $ENV{'PATH'}            = '/opt/mono/bin:' . $ENV{'PATH'};
                    local $ENV{'PKG_CONFIG_PATH'} = '/opt/mono/lib/pkgconfig';

                    return $self->get_path_installed('Cpanel::Easy::OptLib::xsp');
                },
            },
            '5' => {
                'name'    => 'Checking mono-basic',
                'command' => sub {
                    my ($self) = @_;
                    local $ENV{'PATH'}            = '/opt/mono/bin:' . $ENV{'PATH'};
                    local $ENV{'PKG_CONFIG_PATH'} = '/opt/mono/lib/pkgconfig';
                    return $self->get_path_installed('Cpanel::Easy::OptLib::monobasic');
                },
            },
            '7' => {
                'name'    => 'configure mod_mono',
                'command' => sub {
                    my ($self) = @_;

                    # -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
                    local $ENV{'CFLAGS'}          = '-I/opt/glib2/include/glib-2.0 -I/opt/glib2/lib/glib-2.0/include ' . $ENV{'CFLAGS'};
                    local $ENV{'PATH'}            = '/opt/glib2/bin:' . $ENV{'PATH'};
                    local $ENV{'PKG_CONFIG_PATH'} = '/opt/glib2/lib/pkgconfig/';

                    my @configure = qw(
                      ./configure
                      --prefix=/opt/mono
                      --with-mono-prefix=/opt/mono
                      --with-apxs=/usr/local/apache/bin/apxs
                    );

                    push @configure, '--with-apr-config=/usr/local/apache/bin/apr-config'
                      if -x '/usr/local/apache/bin/apr-config';

                    return $self->run_system_cmd_returnable( \@configure );
                },
            },
            '8' => {
                'name'    => 'make mod_mono',
                'command' => sub {
                    my ($self) = @_;
                    return $self->run_system_cmd_returnable( [qw(make)] );
                },
            },
            '9' => {
                'name'    => 'make install mod_mono',
                'command' => sub {
                    my ($self) = @_;
                    return $self->run_system_cmd_returnable( [qw(make install)] );
                },
            },
            '10' => {
                'name'    => 'mono config setup',
                'command' => sub {
                    my ($self) = @_;
                    my $conf = '/usr/local/apache/conf/mod_mono.conf';

                    if ( open my $fh, '>', $conf ) {
                        print {$fh} <<'END_MONO_CONF';
    <IfModule mod_mono.c>
        AddType application/x-asp-net .aspx
        AddType application/x-asp-net .asmx
        AddType application/x-asp-net .ashx
        AddType application/x-asp-net .asax
        AddType application/x-asp-net .ascx
        AddType application/x-asp-net .soap
        AddType application/x-asp-net .rem
        AddType application/x-asp-net .axd
        AddType application/x-asp-net .cs
        AddType application/x-asp-net .config
        AddType application/x-asp-net .Config
        AddType application/x-asp-net .dll
        DirectoryIndex index.aspx
        DirectoryIndex Default.aspx
        DirectoryIndex default.aspx
    </IfModule>

END_MONO_CONF

                        close $fh;
                        chmod 0644, $conf;
                        chown 0, 0, $conf;
                    }
                    else {
                        return ( 0, q{Could not open '[_1]' for reading: [_2]}, $conf, $! );
                    }

                    return ( 1, 'ok' );
                },
            },
            '11' => {
                'name'    => 'mono httpd.conf loadmodule',
                'command' => sub {
                    my ($self) = @_;
                    return $self->ensure_loadmodule_in_httpdconf( 'mono', 'mod_mono.so' );
                },
            },
            '12' => {
                'name'    => 'mono httpd.conf include',
                'command' => sub {
                    my ($self) = @_;
                    return $self->include_directive_handler(
                        {
                            'include_path' => '/usr/local/apache/conf/mod_mono.conf',
                            'addmodule'    => 'mod_mono',
                            'loadmodule'   => 'mono_module',
                        }
                    );
                },
            },
            '13' => {
                'name'    => 'old script cleanup',
                'command' => sub {
                    unlink '/usr/local/cpanel/scripts/addasp' if -e '/usr/local/cpanel/scripts/addasp';
                    return ( 1, 'ok' );
                },
            },
        },
    };
}

1;
