#!/usr/bin/perl

#use strict;
use POSIX;
use DBI;



my $logfile = shift or die "No log-file given.";
my $stressfile = shift or die "No stress-test log-file";
my $dbfile = shift or die "No db-upload log-file";
open IN, "<$logfile";



while (<IN>) {

    
    if ( /^Name:\s*([1234][AB])_(\d*)/) {
	$temp = $1;
	$number = $2;	
    }

    if ( /^Date:\s*(.*)$/ ) {
	$date = $1;
    }
	
    if ( /^Tester:\s*(.*)$/ ) {
	$tester = $1;
    }

    if (/^Result:\s*(.*)$/) {
	$result= $1;
    }  
    
    if ( /^CHINJ:\s*(.*)$/ ) {
	$chinjerr = $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;
    }
}
close $logfile;


open IN, "<$stressfile";

while (<IN>) {

    
    if ( /^Stress_test:\s*(.*)$/ ) {
	$stresserr = $1;
	
    }
}

close $stressfile;

open IN, "<$dbfile";

while (<IN>) {

    
    if ( /^Db_upload:\s*(.*)$/ ) {
	$dberr = $1; 
	
    }
}

close $dbfile;


$type=join "", "T", $temp;
print "$type\n";
print "$number\n";
print "Board: $type\n";
print "Date: $date\n";
print "Operator: $tester\n";
print "Result: $result\n";
print "CHINJ: $chinjerr\n";
print "SCSN: $scsnerr\n";
print "TRAP: $traperr\n";
print "PASA: $pasaerr\n";
print "NI: $nierr\n";
print "ORI: $orierr\n";
print "STRESS: $stresserr\n";
print "DB-upload: $dberr\n";

$dbh = DBI->connect("DBI:Pg:dbname=robtestproduction;host=alice.physi.uni-heidelberg.de",
                       "robocop", 'trdrob01')
                       or die "Connection Error: $DBI::errstr\n";
print "$DBI::errstr";


$dbh->begin_work();


if ($result eq 1){
        $state = 'good';
}
else {
        $state = 'bad';
}

$sth = $dbh->prepare("SELECT * FROM boardtests WHERE serialnumber=? AND boardtype=?;");
$sth->execute($number, $type);

if ( !$sth->fetchrow_array() ) {
    
	if ($scsnerr eq "OK"){
        	print "case1\n";
    		$sth = $dbh->prepare("INSERT INTO boardtests(serialnumber, boardtype, testdate,scsnquicktest, preinchargetest,
			 traptest, pasatest, netwintest,stresstest,orireadouttest, dbupdate , state , operator ) 
			 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,? )");
		 
    		$sth->execute($number,$type,$date, $scsnerr, $chinjerr,$traperr,$pasaerr,$nierr, $stresserr, $orierr, $dberr,$state, $tester);
		}
	else{
        	print "case2\n";
		$sth = $dbh->prepare("INSERT INTO boardtests(serialnumber, boardtype, testdate,scsnquicktest, preinchargetest,
                         traptest, pasatest, netwintest,stresstest,orireadouttest, dbupdate ,state, operator) 
			 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
		 
   	 	$sth->execute($number, $type,$date, $scsnerr, 'not done' ,'not done','not done','not done', 'not done', 'not done', 'not done', 'bad',$tester);
		}
}
else{
        if ($scsnerr eq "OK"){
        	print "case3\n";
        	$sth = $dbh->prepare("UPDATE boardtests SET testdate=? , scsnquicktest=? ,  preinchargetest=? ,
			traptest=? , pasatest=? , netwintest=? , stresstest=?,
			orireadouttest=? , dbupdate=? ,  state=? ,  operator=? 
			WHERE serialnumber=? AND  boardtype=?"); 
        	$sth->execute($date, $scsnerr, $chinjerr,$traperr,$pasaerr,$nierr, $stresserr, $orierr, $dberr,$state, $tester,$number,$type);
		}
        else{
        	print "case4\n";
		$sth = $dbh->prepare("UPDATE boardtests SET testdate=? , scsnquicktest=? ,  preinchargetest=?, 
                        traptest=? , pasatest=? ,  netwintest=? , stresstest=?,
                        orireadouttest=? ,  dbupdate=? ,  state=? ,  operator=? 
                        WHERE serialnumber=? AND  boardtype=?");  
		$sth->execute($date, $scsnerr, 'not done' ,'not done','not done','not done', 'not done', 'not done', 'not done','bad', $tester,$number,$type);
        	}

}

$dbh->commit;



$sth->finish();
$dbh->disconnect;



	



