#!/usr/bin/perl
#
# ring-queue -- remctl script display ring queue status
#
# Written by Bill MacAllister <bill@ca-zephyr.org>
# Copyright (c) 2024 Bill MacAllister <bill@ca-zephyr.org>

use strict;
use DBI;
use Getopt::Long;
use Pod::Usage;
use Rings::Common;

my $opt_debug;
my $opt_help;
my $opt_manual;

##############################################################################
# Subroutines
##############################################################################

sub display_action_queue {
    print("\n");
    print("Action Queue Status:\n");
    my $sel = 'select * from picture_action_queue';
    dbg($sel) if $opt_debug;
    my $sth = $DBH->prepare($sel);
    $sth->execute();
    if ($sth->err) {
        sql_die($sel, $sth->err, $sth->errstr);
    }

    my $cnt = 0;
    while (my $row = $sth->fetchrow_hashref('NAME_lc')) {
        printf(
            "%7d %16s %16s %s\n",
            $row->{pid},
            $row->{action},
            $row->{status},
            $row->{error_text}
        );
        $cnt++;
    }
    return;
}

sub display_upload_queue {
    print("\n");
    print("Upload Queue Status:\n");
    my $sel = "select * from picture_upload_queue";
    dbg($sel) if $opt_debug;
    my $sth = $DBH->prepare($sel);
    $sth->execute();
    if ($sth->err) {
        sql_die($sel, $sth->err, $sth->errstr);
    }

    my $cnt = 0;
    while (my $row = $sth->fetchrow_hashref('NAME_lc')) {
        printf(
            "%32s %16s %s\n",
            $row->{path},
            $row->{status},
            $row->{error_text}
        );
        $cnt++;
    }
    return;
}

##############################################################################
# Main routine
##############################################################################

# -- get options
GetOptions(
    'debug'  => \$opt_debug,
    'help'   => \$opt_help,
    'manual' => \$opt_manual
);

if ($opt_debug) {
    print("DEBUG: output started\n");
}

# help the poor souls out
if ($opt_manual) {
    pod2usage(-verbose => 2);
}
if (scalar(@ARGV) == 0 || $ARGV[0] eq 'help') {
    $opt_help = 1;
}
if ($opt_help) {
    pod2usage(-verbose => 0);
}

my $id = shift;
dbg("id = $id\n") if $opt_debug;

my %id_list = get_id_list();
if (!$id_list{$id}) {
    print("ERROR: unknown ID $id\n");
    print("INFO: Valid IDs:\n");
    for my $i (sort keys %id_list) {
        print("    $i\n");
        exit 1;
    }
}
my $ring_conf = "/etc/rings/${id}.conf";
if ($opt_debug) {
    print("ring_conf = $ring_conf\n");
}
get_config($ring_conf);

db_connect();

display_action_queue();

display_upload_queue();

exit;

__END__

=head1 NAME

ring-queue - display the rings processing queue status

=head1 SYNOPSIS

ring-queue <ring id> [--help] [--manual]

=head1 DESCRIPTION

This script queues the rings database displaying processing queue
entries.

=head1 ARGUMENTS

=over 4

=item <ring id>

Display the processing queue status for the <ring id> ring.

=back

=head1 OPTIONS

=over 4

=item --help

Display usage information.

=item --manual

Display the man page for this script.

=back

=head1 COPYRIGHT

Copyright (c) 2024 Bill MacAllister <bill@ca-zephyr.org>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=head1 AUTHORS

Bill MacAllister <bill@ca-zephyr.org>

=cut

__END_OF_DOCS__
