#!/usr/bin/perl

use strict;
use DB2::Admin;
use Text::FormatTable;

my $db_name = 'sample';

#
# Get the names of all database configuration params.  The config file
# contains params for all releases, so filter out non-relevant names
# by checking that the constant is defined.
#
# Real code would supply the config param names and not read them out
# form an undocumented variable.
#
my @all_params;
while (my ($param, $info) = each %{ $DB2::Admin::Constants::config_params} ) {
    next unless ($info->{Domain} eq 'Database');
    next unless (DB2::Admin::Constants::->GetInfo($param));
    push @all_params, $info->{Name};
}

#
# Get delayed database config (no db connection required)
#
my @retval = DB2::Admin::->
  GetDatabaseConfig('Param'    => \@all_params,
		    'Flag'     => 'Delayed',
		    'Database' => $db_name);

my %db_cfg;  # Param -> Value/Automatic
foreach my $entry (@retval) {
    my $name = $entry->{Name};
    my $value = $entry->{Value};
    my $automatic = $entry->{Automatic} || $entry->{Computed} || 0;
    $db_cfg{$name} = { 'Value'     => $value,
                       'Automatic' => $automatic,
                     };
}

#
# The package cache size is magic.  If set to -1,
# then it needs to be treated as automatic,
#
if ($db_cfg{pckcachesz}{Value} == 2**32 - 1) {
    $db_cfg{pckcachesz}{Value} = ($db_cfg{maxappls}{Value} < 8 ? 32 : $db_cfg{maxappls}{Value} * 4);
    $db_cfg{pckcachesz}{Automatic} = 1;
}

#
# Generate a pretty table
#
my $table = Text::FormatTable::->new('|l|l|l|');
$table->head("Parameter", "Auto", "Value");
$table->rule('-');
foreach my $param (sort keys %db_cfg) {
    $table->row($param, ($db_cfg{$param}{Automatic} ? 'Yes' : 'No'), $db_cfg{$param}{Value});
}
print $table->render();
