#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#
#####################################################
#
# This script will run an inspect on the DB2 tables
# It is dependent on the setup of DB2 by the db2sqlsetup script
# requires xCAT to be installed because it uses xCAT Perl libraries
#  and xCAT commands
# The output is put in /tmp/<tablename all caps>.output
# for example /tmp/SITE.out 
#
#####################################################
BEGIN
{
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
    $::XCATDIR  = $ENV{'XCATDIR'}  ? $ENV{'XCATDIR'}  : '/etc/xcat';
}

use lib "$::XCATROOT/lib/perl";
use xCAT::Utils;
use Getopt::Long;

use strict;
use warnings;


# Use runsqlcmd to run the SQL query to read the list of tables in 
# the XCATDB schema
# Then use runsqlcmd to run the inspect on the list of tables,
#
my $tablelist;
my $help;
my $cmd;
my @tablist;
GetOptions( 'V|verbose' => \$::VERBOSE,
             't=s' => \$tablelist,
             'h|help' => \$help,);
if ($help) {
    print "DB2 Table Inspect utility.\n";
    print
      "The script is run on the command line to inspect the xcatdb DB2 database tables.\n";
    print  "Output indicates whether compression should be set on the table.\n";
    print  "Output is put in /tmp/<tablename all caps>.output. For example /tmp/SITE.out.\n";
    print "Usage:\n";
    print "\t--V - Verbose mode\n";
    print "\t--h - usage\n";
    print
      "\t--t -comma delimitated list of tables.\n             Without this flag it inspects all tables in the xcatdb database .\n";
    print "\n";
    exit 0;
}
# check to see if running DB2
my $DBname = xCAT::Utils->get_DBName;
if ($DBname ne "DB2") {
 
   print " Inspect of table only supported for DB2 database\n";
   `logger -txcat "insptbls:Only supports DB2 database"` ;
    exit 1;
  
}
if ($tablelist) {  # input list of tables 
  @tablist=split(/\,/, $tablelist);
} else { # get all tables
  $cmd="$::XCATROOT/sbin/runsqlcmd \"select tabname from syscat.tables where TABSCHEMA='XCATDB';\"";
  @tablist = xCAT::Utils->runcmd($cmd, 0);
  if ($::RUNCMD_RC != 0)
  {
   `logger -txcat " insptbls:error in select tabname from syscat.tables"` ;
   exit 1;
  }
}
# get xcatdb home directory
my $homedir = xCAT::Utils->getHomeDir("xcatdb");
my $execdir = $homedir;
$execdir .="\/sqllib\/db2dump\/";
#inspect each table
my $foundheader=0;
foreach my $table (@tablist) {
  chomp $table;
  # skip lines untils we find the header unless -t option
  if (!($tablelist)) {
    if ($foundheader==0) {
      if ( !($table =~ /TABNAME/)) {
        next;
      } else {
        $foundheader=1;
        next;
      }
    }
  }
  # skip blanks and -- lines
  $table =~ s/\s*//g;    # remove blanks
  if ($table =~ /^\s*$/) {  # skip blanks
      next;
  }
  if ($table =~ m/[^a-zA-Z0-9_]/) # skip non alphanumeric lines not underscore
  {
      next;
  }
  $table  =~ tr/a-z/A-Z/;    # convert to upper
  if ($::VERBOSE) {
    print "Inspect of table $table\n";
  }
  $cmd = " rm /tmp/$table.out";   # cleanup
  xCAT::Utils->runcmd($cmd, 0);
  if ($::RUNCMD_RC != 0)
  {
    `logger -txcat " insptbls:error $cmd"`;
  } else {
    `logger -txcat " insptbls: $cmd"`;
  }
  $cmd="$::XCATROOT/sbin/runsqlcmd \"INSPECT ROWCOMPESTIMATE TABLE NAME $table results keep $table.res;\"";
  xCAT::Utils->runcmd($cmd, 0);
  if ($::RUNCMD_RC != 0)
  {
    `logger -txcat " insptbls:error INSPECT ROWCOMPESTIMATE TABLE NAME $table"`;
  } else {
    `logger -txcat " insptbls: INSPECT ROWCOMPESTIMATE TABLE NAME $table"`;
  }
  $cmd=" \"db2inspf $execdir$table.res /tmp/$table.out;\"";
  system("su - xcatdb -c $cmd");

  if ($? > 0)    # error
  {
    `logger -txcat "insptbls:error db2inspf $table.res"`;
  } else {
    `logger -txcat " insptbls:db2inspf $table.res"`;
  }
}
exit 0;
