#!/usr/bin/perl

#use strict;
use POSIX;
use DBI;


$confirm_new_rob = 0;

$loc = "KIP.ROB_Test";


my $logfile = shift or die "No log file given.";
my $pdffile = shift or die "No pdf file given.";
my $tgzfile = shift or die "No tgz file given.";

open IN, "<$logfile";

while (<IN>) {

    if ( /^Name:\s*F(\d+)_?T(\d\w)_?V(\d+)_?N(\d+)/ ) {
	$form = $1;
	$type = $2;
	$ver  = $3;
	$id   = $4;

    }

    if ( /^Name:\s*([1234][AB])_(\d*)/ ) {
	$form = 0;
	$type = $1;
	$ver  = 2;
	$id   = $2;

    }

    if ( /^Date:\s*(.*)$/ ) {
	$date = $1;
    }
	
    if ( /^Result:\s*(\d*)$/ ) {
	$result = $1;
    }
	
    if ( /^SCSN:\s*(.*)$/ ) {
	$scsnerr = $1;
    }
	
    if ( /^TRAP:\s*(.*)$/ ) {
	$traperr = $1;
    }
	
    if ( /^PASA:\s*(.*)$/ ) {
	$pasaerr = $1;
    }
	
    if ( /^NI:\s*(.*)$/ ) {
	$nierr = $1;
    }
	
    if ( /^ORI:\s*(.*)$/ ) {
	$orierr = $1;
    }
	
    if ( /^Tester:\s*(.*)$/ ) {
	$person = $1;
    }
	
    if ( /^Comment:\s*(.*)$/ ) {
	$comment = $1;

	while(<IN>) {
	    $comment .= $_;
	}
    }
	
    


}

print "Name: F$form T$type V$ver N$id\n";
print "Date: $date\n";
print "Tester: $person\n";
print "Result: $result\n";
print "SCSN: $scsnerr\n";
print "TRAP: $traperr\n";
print "PASA: $pasaerr\n";
print "NI: $nierr\n";
print "ORI: $orierr\n";
print "Comment: $comment\n";

$pdf_loid = -1;
$tgz_loid = -1;


undef ($scsnerr) if ($scsnerr =~ /^\s*$/);
undef ($traperr) if ($traperr =~ /^\s*$/);
undef ($pasaerr) if ($pasaerr =~ /^\s*$/);
undef ($nierr)   if ($nierr   =~ /^\s*$/);
undef ($orierr)  if ($orierr  =~ /^\s*$/);



my $dbh = DBI->connect("DBI:Pg:dbname=gateDB;host=alice.physi.uni-heidelberg.de",
		       "alice", 'alice!@#',
		       {'RaiseError' => 1});

$dbh->begin_work();

print "$id'n";

$sth = $dbh->prepare("SELECT * FROM rob WHERE rob_id=$id;");
$sth->execute();

if ( ! $sth->fetchrow_array() ) {

    if ($confirm_new_rob) {
	print "ROB not found, do you want to insert it [Y/n]? ";
	$yn = <STDIN>;
	chop $yn;

	if ($yn ne 'y' and $yn ne 'Y' and $yn ne '') {
	    print "\n...then I'm out of here!\n";
	    exit;
	}
    }


    if (!defined $person) {
	print "Enter your initials: ";
	$person = <STDIN>;
	chop $person;
    }

    if (!defined $loc) {
	print "Your location [SMint]: ";
	$loc = <STDIN>; chop $loc;
	
	$loc = 'SMint' if ($loc eq '');
    }

    $sth = $dbh->prepare("INSERT INTO rob(form,type,ver,rob_id,person,loc_id) ".
			 "VALUES (?,?,?,?,?,(SELECT loc_id FROM loc ".
			 "                   WHERE name=?));");
    $sth->execute($form,$type,$ver,$id,$person,$loc);
    
}


$pdf_loid = upload_file($dbh,$pdffile,"","application/pdf");
$tgz_loid = upload_file($dbh,$tgzfile,"","application/gzip");

$sth = $dbh->prepare("INSERT INTO rob_test ".
		     "VALUES (?,?,?,?,?,?,?,?,?,".
		     "        (SELECT file_id FROM file WHERE loid=?),".
		     "        (SELECT file_id FROM file WHERE loid=?));");

$sth->execute($id,$date,$result,
	      $scsnerr,$traperr,$pasaerr,$nierr,$orierr,
	      $comment, $pdf_loid, $tgz_loid);


$sth = $dbh->prepare("UPDATE rob SET status=? WHERE rob_id=?");
$sth->execute($result,$id);


$dbh->commit;


$dbh->disconnect;




sub upload_file () {

    my $dbh      = $_[0];
    my $filename = $_[1];
    my $dbname   = $_[2];
    my $mimetype = $_[3];

    my $nbytes;

    open INFILE, "<$filename";
    binmode INFILE;

    my $buf;
    my $len = read INFILE,$buf,100000000;

    close INFILE;


    my $lobjId = $dbh->func($dbh->{pg_INV_WRITE}, 'lo_creat');
    my $lobj_fd = $dbh->func($lobjId, $dbh->{pg_INV_WRITE} , 'lo_open');
    my $nbytes = $dbh->func($lobj_fd, $buf, $len, 'lo_write');

    my $sth = $dbh->prepare("INSERT INTO file(name,mime_type,loid) ".
			 "VALUES (?,?,?);");
    $sth->execute($filename, $mimetype, $lobjId );

    return $lobjId;


};
