#!/usr/bin/perl

use strict;
use DBI;
use Getopt::Long;
use Sys::Hostname;

my %opts;
usage() if (! GetOptions(\%opts, "help|?", "cf|c=s@", "sid|s=s@", "user|u=s@", "pass|p=s@"));
usage() if (defined($opts{'help'}));

my @cf = @{$opts{'cf'}} if (defined($opts{'cf'}));
print "cf = @cf\n";
my @usr = @{$opts{'user'}} if (defined($opts{'user'}));
print "usr = @usr\n";
my @pwd = @{$opts{'pass'}} if (defined($opts{'pass'}));
print "pwd = @pwd\n";
my @sid = @{$opts{'sid'}} if (defined($opts{'sid'}));
print "sid = @sid\n";

$0 = 'test_monitor.apl';

my ($user, $pass, $sid) = (@usr, @pwd, @sid);

my $string = 'dbi:Oracle:' . $sid;

# Connect to the database
my $dbh = DBI->connect($string,
			$user,$pass) || die "$DBI::errstr\n";

my ($cf_curv, $cf_maxv);

eval {
	my $proc = $dbh->prepare(q{
		begin
			dp_monitor.query_controlfile(
				cf_type => :cf_type,
				cf_curv => :cf_curv,
				cf_maxv => :cf_maxv
			);
		end;
	});

	$proc->bind_param(":cf_type", @cf);
	$proc->bind_param_inout(":cf_curv", \$cf_curv, 100);
	$proc->bind_param_inout(":cf_maxv", \$cf_maxv, 100);
	$proc->execute;
};

if ( $@ ) {
	warn "Execution of stored procedure failed: $DBI::errstr\n";
}

print "cf_type = @cf, cf_curv = $cf_curv, cf_maxv = $cf_maxv\n";

#my $cf_used = $cf_curv - $cf_maxv;
#printf "%3.2f%\n", $cf_used / $cf_curv * 100.00;

$dbh->disconnect;

sub usage {
  print STDERR <<EUSAGE;

    Defaults: 
              --ts 'tablespace' 

EUSAGE
  exit(1);
}

#sleep(1);
exit
