#!/usr/bin/perl

# ---------------------------------------------------------------
# Prontus CMS
# http://www.prontus.cl
# by Altavoz.net
#
# licensed under LGPL license.
# http://www.prontus.cl/license.html
# ---------------------------------------------------------------
# ---------------------------------------------------------------
# PROPOSITO .
# -----------
# Listar archivos pertenecientes a un Prontus y articulo en un almacenamiento S3
#
use strict;
use warnings;
use utf8;

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

use Net::Amazon::S3;
use JSON;
require HTTP::Headers;

# Captura STDERR
use lib_stdlog;
&lib_stdlog::set_stdlog();
use prontus_varglb; &prontus_varglb::init();
use mime_types;
use glib_fildir_03;
use lib_prontus;
use lib_artic;
use Artic;
use lib_waitlock;
use lib_s3;

use Data::Dumper;

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 $ts = '';
    if (defined($ARGV[1]) && $ARGV[1] =~ /^\d{14}$/) {
        $ts = $ARGV[1];
    } elsif ($ARGV[1]) {
        print "Parámetro [$ARGV[1]] no es válido\n";
        exit;
    }

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

    if (!lib_s3::check_config_s3()) {
        print STDERR "S3 no habilitado y/o configurado\n";
        exit;
    }

    my ($result, $msg_err) = &lib_prontus::get_url("https://".$prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'S3_ENDPOINT'}, 10);
    if ($msg_err) {
        print STDERR "Error al conectar con endpoint S3 [$msg_err]\n";
        exit;
    }

    lib_s3::connect_s3();
    my $bucket = lib_s3::connect_bucket();

    my @prefixes;

    if ($ts ne '') {
        my $dst_base = $prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'DIR_SITE'} . "$prontus_varglb::DIR_EXMEDIA/" . substr($ts, 0, 8) . "$prontus_varglb::DIR_MMEDIA/";
        $dst_base = substr($dst_base, 1);
        push(@prefixes, $dst_base);
        $dst_base = $prontus_varglb::CONFIG{$prontus_varglb::PRONTUS_ID}{'DIR_SITE'} . "$prontus_varglb::DIR_ASOCFILE/" . substr($ts, 0, 8) . "/$ts/";
        $dst_base = substr($dst_base, 1);
        push(@prefixes, $dst_base);
    } else {
        push(@prefixes, $prontus_varglb::PRONTUS_ID);
    }

    my $total_size = 0;
    foreach my $prefix (@prefixes) {
        my @s3_files = lib_s3::list_files_pattern($bucket, $prefix);
        foreach my $remote (@s3_files) {
            if ($ts eq '' || $remote->key =~ /$ts/) {
                print $remote->key . " - ". $remote->uri . " - ". lib_prontus::bytes2kb($remote->size) ."\n";
                $total_size += $remote->size;
            }
        }
    }
    print "Total size : ", lib_prontus::bytes2kb($total_size) , " - $total_size\n";
}
