#############################################################################
# Program Name : lib.pl
# Purpose      : Perl Library functions, which can be called from perl prg
#
# Date          Author          Description
# 05/23/2001    Ramesh Bala     Initital Version
# 01/25/2005    Blake McNulty   Modified for J975SC/OAS.
#############################################################################

$homedir = $ENV{'HOMEDIR'};

$archdir = $ENV{'ARCHDIR'};
$workdir = $ENV{'WORKDIR'};
$bindir  = $ENV{'BINDIR'};
$cfgdir  = $ENV{'CFGDIR'};
$sqldir  = $ENV{'SQLDIR'};
$datadir = $ENV{'DATADIR'};
$logdir  = $ENV{'LOGDIR'};


sub execute_sql_stmt {

   if (-e $sqldir . $infile) {
      }
   else {
      $errmsg = "$infile Data File Not Found";
      print "\nError: $errmsg : " . $datadir . $infile . "\n";
      exit (-1);
      }

   $sqlfile = $logdir.$infile.".log";
   $sqlout  = $logdir.$infile.".out";
#  print "\n\n SQLFILE:",$sqlfile,":";

   my($sql_stmt) = $_[0];

    open (SQLFILE,">$sqlfile") || die "Can't open outfile '$sqlfile' \n";
    print SQLFILE ("whenever sqlerror exit failure\n");
    print SQLFILE ("whenever oserror exit failure\n");
    print SQLFILE ("$sql_stmt\n");
    print SQLFILE ("exit;\n");
    close SQLFILE;

    $sql_stmt = "echo '@ $sqlfile' | $cfgdir/sqllogin > $sqlout";
    $rtn = system("$sql_stmt");

    if ($? != 0) {
       print ("\nError : subroutine execute_sql_stmt failed\n");
       return(-1);
    }
#    unlink("$logdir/sqlrun.log");
    return (0);
}


sub execute_sqlldr_stmt {

if (-e $datadir . $infile) 
 {
   #make a copy into archive dir
   $fileext = &get_time();

   $cp_stmt = 'cp '. $datadir . $infile . ' ' . $archdir . $infile . $fileext;
   system($cp_stmt);
   if ($? != 0) 
    {
     $errmsg = "'$tbname' Data File Archive Copy Failed";
     print ("\nError : $errmsg ");
     exit (-1);
     }
 }
else
 {
   $errmsg = "$tbname Data File Not Found";
   print "\nError: $errmsg : " . $datadir . $infile . "\n";
   exit (-1);
 }

  my($table_name) = $_[0];
  my($sqlpwd) = &get_pwd;

  my($data_file) = "$datadir/".$table_name.".dat";
  my($ctl_file) = "$sqldir/".$table_name.".ctl";
  my($log_file) = "$logdir/".$table_name.".log";
  my($bad_file) = "$logdir/".$table_name.".bad";

  my($sqlldr_stmt)="sqlldr $sqlpwd CONTROL=$ctl_file DATA=$data_file LOG=$log_file BAD=$bad_file DIRECT=TRUE ERRORS=1 SILENT=FEEDBACK";
  $log_msg = "executing: ". $sys_stmt . "\n";
  `echo $log_msg >> $logdir/sql.log`;
  unlink("$bad_file");

  # Load the file using Oracle UNIX SQLLDR utility
  system("$sqlldr_stmt");

  if ($? != 0) {
    print ("\n Failure Point:$? execute_sqlldr_stmt $table_name failed.\n");
    return (1);
  } else {
    if (-e $bad_file) {
      return (2);
    } else {
       return(0);
    }
  }
}
  
sub get_pwd {
    $pwd = "$cfgdir/sqllogin";

    open(PWD,$pwd);
    while ($pwd !~ /sqlplus/i) {
       $pwd = <PWD>;
    }

    $pwd = lc($pwd);
    $pwd =~ s/^[ \t]+//;
    $pwd =~ s/[ \t]+\n$/\n/;
    $pwd =~ s/[ \t]+/ /g;
    @array = split (/ /,$pwd);

    $count = 0;

    while ($array[$count] !~ /sqlplus/i) {
       $count++;
    }

    $pwd = $array[$count + 1];
    chomp ($pwd);
 
    close(PWD);
    return ($pwd);
}

sub get_time
{
    local (*time_array) =@_;
    @time_array = localtime(time);
    $time_array[4]++;

    $year = $time_array[5] + 1900;
    $time_result = "$year-$time_array[4]-$time_array[3]:$time_array[2]:$time_array[1]:$time_array[0]";
    return $time_result;
}

