#!/usr/bin/env perl

use warnings;
use strict;
use Carp;

# USAGE: ipp_darkstats.pl --dbname (name) --det_id (id)

###  Get list of dark imfile results

# define the dettool command
my $command = "$dettool -processedimfile -select_state stop"; # Command to run
$command .= " -det_id $det_id";
$command .= " -dbname $dbname" if defined $dbname;

# run the dettool command and catch the output
my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    run(command => $command, verbose => 1);
unless ($success) {
    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    &my_die("Unable to perform dettool: $error_code", $det_id, $error_code);
}

# parse the output into a list
my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    &my_die("Unable to parse metadata config doc", $det_id, $PS_EXIT_PROG_ERROR);
my $files = parse_md_list($metadata) or
    &my_die("Unable to parse metadata list", $cam_id, $PS_EXIT_PROG_ERROR);

# XXX finish this off:

# - get the exp_time as well from dettool
# - build an array of bg & exptime for each cell
# - fit the trend (in mana? pslib functions?)
# - write the polynomial for each cell
