#!/usr/bin/env perl

# list the filesets in a data store product

use strict;
use warnings;

use DBI;
use dsdbh;

my $PS_EXIT_CONFIG_ERROR = 3;
my $PS_EXIT_DATA_ERROR = 5;
my $product = shift;
die("must specify product to list") unless defined $product;

# optional fileset name. Ignore filesets up to and including $after_fileset
my $after_fileset = shift;

my $dbh = getDBHandle();

my $prod_stmt = $dbh->prepare("SELECT * FROM dsProduct WHERE prod_name = '$product'");
$prod_stmt->execute();
my $prod = $prod_stmt->fetchrow_hashref();
if (! defined $prod) {
    print STDERR "failed to find $product in product list\n";
    exit ($PS_EXIT_DATA_ERROR);
}

# build the header line
my $header = "# filesetID|time registered     |type     |";

# now add the product specific columns that are defined
# not particularly elegant. I guess I should have fetched the row as an array
# note: we don't do any formatting for these columns. The width of the string
# in the database should be padded if a nice width is desired
my $last_prod_col = -1;
if (defined $prod->{prod_col_0}) {
    $last_prod_col = 0;
    $header .= "$prod->{prod_col_0}|";
    if (defined $prod->{prod_col_1}) {
        $last_prod_col = 1;
        $header .= "$prod->{prod_col_1}|";
        if (defined $prod->{prod_col_2}) {
            $last_prod_col = 2;
            $header .= "$prod->{prod_col_2}|";
            if (defined $prod->{prod_col_3}) {
                $last_prod_col = 3;
                $header .= "$prod->{prod_col_3}|";
                if (defined $prod->{prod_col_4}) {
                    $last_prod_col = 4;
                    $header .= "$prod->{prod_col_4}|";
                    if (defined $prod->{prod_col_5}) {
                        $last_prod_col = 5;
                        $header .= "$prod->{prod_col_5}|";
                        if (defined $prod->{prod_col_6}) {
                            $last_prod_col = 6;
                            $header .= "$prod->{prod_col_6}|";
                            if (defined $prod->{prod_col_7}) {
                                $last_prod_col = 7;
                                $header .= "$prod->{prod_col_7}|";
                            }
                        }
                    }
                }
            }
        }
    }
}
    

my $prod_id = $prod->{prod_id};

my $stmt;
my $after_clause = "";
if ($after_fileset) {
    # client wants only filesets registered after $after_fileset
    # Since fileset_id is auto increment the desired filesets will have a larger fileset_id than
    # the target. Look up it's fileset_id

    $stmt = $dbh->prepare("SELECT fileset_id FROM dsFileset " .
                            "WHERE prod_id = $prod_id AND fileset_name = \'$after_fileset\'");
    $stmt->execute();
    my $fs = $stmt->fetchrow_hashref();
    if ($fs) {
        $after_clause = " AND fileset_id > $fs->{fileset_id}";
    } else {
        # $after_fileset not found
        #
        # XXX: the spec doesn't say what should happen in this case.
        # Here we follow the conductor implementation and return the whole list.
        # This may not be the right thing to do.
    }
}
        
$stmt = $dbh->prepare("SELECT * FROM dsFileset WHERE prod_id = $prod_id  $after_clause");
$stmt->execute();

# XXX: dsproductls expects a header even if there are no matching filesets
print "$header\n";

while( my $row = $stmt->fetchrow_hashref()) {

    my $daytime = $row->{reg_time};
    (my $date, my $time) = split " ", $daytime;

    my $reg_time = $date . "T" . $time . "Z";

    my $line = sprintf "%-11s|%-20s|%-9s|", $row->{fileset_name}, $reg_time, $row->{type};

    # now add the product specific columns that are defined
    # again using an array would have made this look nicer
    if ($last_prod_col >= 0) {
        $line .= "$row->{prod_col_0}|";
        if ($last_prod_col >= 1) {
            $line .= "$row->{prod_col_1}|";
            if ($last_prod_col >= 2) {
                $line .= "$row->{prod_col_2}|";
                if ($last_prod_col >= 3) {
                    $line .= "$row->{prod_col_3}|";
                    if ($last_prod_col >= 4) {
                        $line .= "$row->{prod_col_4}|";
                        if ($last_prod_col >= 5) {
                            $line .= "$row->{prod_col_5}|";
                            if ($last_prod_col >= 6) {
                                $line .= "$row->{prod_col_6}|";
                                if ($last_prod_col >= 7) {
                                    $line .= "$row->{prod_col_7}|";
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    print "$line\n";
}
