# Loads files into task_step_output
# TJS 06-Mar-2001
# TM1.8 version

$ot = "LOG";
$file = undef;

use DBI;

foreach(@ARGV)
{
# Get output type
  /^(ERROR|WARNING|LOG)/ && do { $ot = $_; };

# Get file name
  -f && do { $file = $_; };

# Get Task_Step_Code
  /^\d+$/ && do { $tisc = $_; };

# Get db login information
  /^([\w\d_]+)\/([\w\d_]+)\@([\w\d_]+)$/ && do
  {
    $oun = $1;
    $opw = $2;
    $ocs = $3;
  };
  /^([\w\d_]+)\/([\w\d_]+)$/ && do
  {
    $oun = $1;
    $opw = $2;
    $ocs = $ENV{"ORACLE_SID"};
    $ocs = $ENV{"ORA_SID"} if !defined $ocs;
#    print "Oracle login.\n";
  };
  /^\/$/ && do
  {
    $oun = "/";
    $opw = "";
    $ocs = $ENV{"ORACLE_SID"};
    $ocs = $ENV{"ORA_SID"} if !defined $ocs;
#    print "Oracle login.\n";
  };
}

print "Unable to locate $ot file to load.\n" if !defined $file;
exit;

$dbh = DBI->connect("dbi:Oracle:$ocs", $oun, $opw);

if (!$dbh)
{
  print "$DBI::errstr\n";
  die "Failed to connect to database";
}
print "OK.\n";

undef $/;

die "There is no such file: $file\n" if ! -f $file;

@ARGV = ($file);

while(<>)
{
# Adjust line breaks to DOS format
# Also removes blank lines
  s/[\r\n]+/\r\n/g;

  $sql = q(insert into task_step_output
           (task_step_id, output_type, output)
           values
           (?, ?, ?));

  $sth = $dbh->prepare( $sql );
  $sth->execute($tisc, $ot, $_) || die "$DBI::errstr";
  $sth->finish;

  print "Done.\n";
}

$dbh->commit() if ($dbh->{AutoCommit} == 0);

$dbh->disconnect();

