#!/usr/bin/perl

# ---------------------------------------------------------------
# Prontus CMS
# http://www.prontus.cl
# by Altavoz.net
#
# licensed under LGPL license.
# http://www.prontus.cl/license.html
# ---------------------------------------------------------------
# ---------------------------------------------------------------
# PROPOSITO .
# -----------
# Prueba un artículo, carga el xml e imprime el objeto resultante
#
use strict;
use warnings;
use utf8;

BEGIN {
    use FindBin qw($Bin);
    my $pathLibsProntus = $Bin;
    unshift(@INC, $pathLibsProntus);
    $pathLibsProntus =~ s/\/[^\/]+\/?$//;
    unshift(@INC,$pathLibsProntus);
}

##libs##
# Captura STDERR
use prontus_varglb;
&prontus_varglb::init();
use glib_fildir_03;
use lib_prontus;
use lib_artic;
use Artic;
##/libs##

main: {
    lib_prontus::setUtf8();
    if (scalar @ARGV < 1) {
        print "Parámetros insuficientes. Modo de uso:\n";
        print "perl $0 <prontus_id>\no\n";
        print "perl $0 <prontus_id> <ts_articulo>\n";
        exit;
    }

    $prontus_varglb::PRONTUS_ID = $ARGV[0];

    my $msg_load = &lib_prontus::load_config_by_id($prontus_varglb::PRONTUS_ID);
    if ($msg_load ne '') {
        print "Ha ocurrido un error: $msg_load\n";
        exit;
    }

    my ($BD, $msg_err_bd) = &lib_prontus::conectar_prontus_bd();
    if (!ref($BD)) {
        print "Error conectar BD: $msg_err_bd\n";
        exit;
    }

    my $sql = "SELECT ART_ID FROM ART";
    my $salida = &glib_dbi_03::ejecutar_sql_param($BD, $sql);

    if ($BD->errstr) {
        print "Error al obtener listado de articulos: [", $BD->errstr, "]\n";
        exit;
    }

    while (my $row = $salida->fetchrow_hashref) {
        my $artic_obj;

        unless($artic_obj = Artic->new(
                    'prontus_id'         => $prontus_varglb::PRONTUS_ID,
                    'public_server_name' => $prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'PUBLIC_SERVER_NAME'},
                    'cpan_server_name'   => $prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'CPAN_SERVER_NAME'},
                    'document_root'      => $prontus_varglb::DIR_SERVER,
                    'ts'                 => $row->{'ART_ID'},
                    'no_check_dirs'      => 0)) {
            print "[Error] inicializando objeto articulo: $Artic::ERR\n";
            next;
        };

        $artic_obj->write_json();

        print "$row->{'ART_ID'} -> $artic_obj->{fullpath_json}\n";
    }

    $salida->finish;
}
