#!/usr/bin/perl

# Prontus CMS
# http://www.prontus.cl
# by Altavoz.net
#
# licensed under LGPL license.
# http://www.prontus.cl/license.html
# ---------------------------------------------------------------
# script para incializar el arreglo de includes para la instancia perl de apache
# es necesario que se defina el path de cgi-cpn en la configuracion de apache:
# PerlOptions +Parent
# PerlSwitches -I/var/www/html/cgi-cpn
# PerlRequire /var/www/html/cgi-cpn/prontus_apache_startup.pl

use strict;
use warnings 'all';
no warnings 'once';

BEGIN {
    # para usar libreria de json compatible con threads de apache
    $ENV{'PERL_JSON_BACKEND'} = 'Cpanel::JSON::XS,JSON::PP';

    my $cgi_path = '';
    foreach my $path (@INC) {
        if (-f "$path/prontus_varglb.pm" ) {
            $cgi_path = $path;
            last;
        }
    }
    my @entries;
    if ($cgi_path ne '' && -d $cgi_path) {
        opendir(my $DIR, $cgi_path);
        @entries = readdir($DIR);
        closedir $DIR;
    }

    foreach my $entry (@entries) {
        next if ($entry =~ /^[\._]/);
        next if ($entry eq 'prontus_error_log' or $entry eq 'prontus_temp');
        if (-d "$cgi_path/$entry") {
            unshift(@INC, "$cgi_path/$entry");
        }
    }
}

my $cgi_path = '';
foreach my $path (@INC) {
    if (-f "$path/prontus_varglb.pm" ) {
        $cgi_path = $path;
        last;
    }
}

$cgi_path =~ s/\/[^\/]+$//;
my $documentRoot = $cgi_path;

# Busca prontuses y utliza el primero que encuentra (busca en orden alfabetico).
my $prontusId = '';
opendir(my $dh, $documentRoot);
my @entries = readdir($dh);
closedir $dh;

foreach my $entry (sort @entries) {
    next if ($entry =~ /^[\._]/);
    if ((-f "$documentRoot/$entry/cpan/$entry-var.cfg") && (-f "$documentRoot/$entry/cpan/$entry-bd.cfg")) {
        $prontusId = $entry;
        last;
    }
}

$My::prontusId    = $prontusId;
$My::f5dir        = '/prontus_links';
$My::documentRoot = $documentRoot;

$Prontus::modperl = 1;

use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::SubRequest ();
use Apache2::Const;
use APR::Table ();

use Digest::MD5 qw(md5_hex);
use lib_friendly;

1;
