#!/usr/bin/perl
#
# This script generates 80,000 random records in the kohabenchmark database for
# the purposes of comparing two different marc storage schemas.  It requires
# the presence of a word list for populating the data.  Mine is in
# /usr/share/dict/words.  Change that if necessary.  You'll also need to change
# your userid and password for the dbi->connect line.

use DBI;

my $dbh=DBI->connect("dbi:mysql:kohabenchmark", 'youruserid', 'yourpassword');
@subfields = ( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n');


open (W, "/usr/share/dict/words");
while (<W>) {
    chomp;
    push @words, $_;
}

my $tagcounter=0;
my $subfieldcounter=0;
srand($$|time);
for ($bibid=1; $bibid<80000; $bibid++) {
    my $numtags=int(rand(10)+5);
    my $localtagcounter=0;
    for ($i=1; $i<$numtags; $i++) {
	$localtagcounter++;
	$tagcounter++;
	my $tag=$i*40+100;
	my $numsubfields=int(rand(10)+1);
	my $subfieldsused;
	my $localsubfieldcounter=0;
	for ($j=1; $j<=$numsubfields; $j++) {
	    my $code='';
	    until ($code) { 
		my $codepicker=int(rand($#subfields));
		if ($subfieldsused->{$subfields[$codepicker]}==0) {
		    $subfieldsused->{$subfields[$codepicker]}=1;
		    $code=$subfields[$codepicker];
		}
	    }
	    $subfieldcounter++;
	    $localsubfieldcounter++;
	    my $word=$words[int(rand($#words))];
	    my $sth=$dbh->prepare("insert into marc_subfield_table (subfieldid, tagid, tag, bibid, subfieldorder, subfieldcode, subfieldvalue) values (?,?,?,?,?,?,?)");
	    my $error=1;
	    while ($error) {
		$sth->execute($subfieldcounter, $tagcounter, $tag, $bibid, $localsubfieldcounter, $code, $word);
		$error=$dbh->err;
		if ($error) {
		    sleep 1;
		    print "ERROR: $error\n";
		}
		$sth->finish;
	    }
	    my $error=1;
	    my $sth=$dbh->prepare("insert into marc_field_table_sergey (bibid, tagid, tag) values (?, ?, ?)");
	    while ($error) {
		$sth->execute($bibid, $localtagcounter, $tag);
		$error=$dbh->err;
		if ($error) {
		    sleep 1;
		    print "ERROR: $error\n";
		}
		$fieldid=$dbh->{'mysql_insertid'};
		$sth->finish;
	    }
	    $error=1;
	    $sth=$dbh->prepare("insert into marc_subfield_table_sergey (fieldid, subfieldorder, subfieldcode, subfieldvalue) values (?, ?, ?, ?)");
	    while ($error) {
		$sth->execute($fieldid, $localsubfieldcounter, $code, $word);
		$error=$dbh->err;
		if ($error) {
		    sleep 1;
		    print "ERROR: $error\n";
		}
		$sth->finish;
	    }
	}
    }
}
