#!/usr/bin/perl
#$Id$


#use strict;
use POSIX;
use DBI;





my $file = shift or die "No filename given.";

open IN, "<$file";
binmode IN;

$len = read IN,$buf,1000000;

$ctime = (stat($file))[10];
print "CTIME: $ctime\n";
$now_string = strftime "%Y-%m-%d %H:%M:%S", gmtime($ctime);
print "now: $now_string\n";


#initialise used variables
$wafer = -1;
$trapid = -1;
$lasterid = -1;
$result = -1;
$prog_version = -1;
$board_id = -1;

### extract some information from xml-file
if ( $buf =~ /<ID>([0-9]+)<\/ID>/ ){
	$wafer = $1;
}

# problem with the following: if test stopped to early (ie high currents) no chipid
# is reported in the xml file at all,
# need to reconstruct later from wafer and position...
if ( $buf =~ /<chipid_burned>0x([0-9A-Fa-f]+)<\/chipid_burned>/ ) {
	$trapid = hex $1;
}

if ( $buf =~ /<chipid_read>0x([0-9A-Fa-f]+)<\/chipid_read>/ ) {
	$laserid = hex $1;
}

if ( $buf =~ /<RETCODE>([0-9]+)<\/RETCODE>/ ) {
	$result = $1;
}

if ( $buf =~ /<VERSION>(mcm|wafer)tester: ([0-9]+)<\/VERSION>/ ) {
	$prog_version = $2;
}

if ( $buf =~ /<BOARDID>(mcm|wafer)_tester_([0-9]+)<\/BOARDID>/ ) {
	$board_id = $2;
}

if ( $buf =~ /<TESTDATETIME>([-0-9: ]+)<\/TESTDATETIME>/ ) {
  $now_string = $1;
}

### missing: timestamp from logfile in format: YYYY-MM-DD HH:MM
### handle not found errors during file parsing
if ( $wafer < 0 ) {
   print "no wafer ID found, check file\n";
   exit 1;
}
if ($trapid < 0) {
  $waferx = -10;
  $wafery = -10;
  $wafersub = -10;
	print "no ChipID found in file, trying to get it from wafer and position\n";
	if ( $buf =~ /<waferX>([-0-9]+)<\/waferX>/ ) {
		$waferx = $1 ;
	}
	if ( $buf =~ /<waferY>([-0-9]+)<\/waferY>/ ) {
		$wafery = $1;
	}
	if ( $buf =~ /<ReticleSubSite>([-0-9]+)<\/ReticleSubSite>/ ) {
		$wafersub = $1;
	}
	if ( ($waferx < -9) || ($wafery < -9 ) || ($wafersub < -9)) {
		print "failed to get chipid from file, check file\n";
		exit 1;
	}
# calculate chipid:
	$trapid = ((36* $wafery + ($waferx+7))* 4 + $wafersub) | ($wafer << 10 ) ;
}
if ($prog_version < 0) {
	print "Failed to get test version from file, check file\n";
	exit 1;
}
if ($board_id < 0 ) {
	print "Failed to get board id from file, check file\n";
	exit 1;
}
if ($result < 0) {
	print "Failed to get test result from file, check file format\n";
}

print "now:          $now_string\n";
print "WaferID:      $wafer\n";
print "ChipID:       $trapid\n";
print "read ChipID:  $laserid\n";
print "Test result:  $result\n";
print "Test version: $prog_version\n";
print "TesterBoard Id: $board_id\n";
print "Testtime:     $now_string\n";
#exit;
# minimum of information is now available
#exit;

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

$lobjId = $dbh->func($dbh->{pg_INV_WRITE}, 'lo_creat');

print "loid: $lobjId\n";

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


$sth = $dbh->prepare("INSERT INTO file(name,mime_type,loid) VALUES (?,?,?);");
$sth->execute("filename(optional)", "text/xml", $lobjId );


$sth = $dbh->prepare("INSERT INTO wafer_test(wafer,trap_id,laser_id,result,timestamp,".
		     "                       tester_ver,tester_board_id,logfile) ".
		     "VALUES (?,?,?,?,?,?,?,(SELECT file_id FROM file WHERE loid=?))");

$sth->execute($wafer,$trapid,$laserid,$result,$now_string,$prog_version,$board_id,$lobjId);

$dbh->commit;

print "$nbytes bytes written\n";

$dbh->disconnect;

