#!/usr/bin/perl -w

# Usage: load.attendees2005.pl <INFIL> <OUTFIL> <starting-serial-number>
#
# Example:  load.attendees2005.pl attendees2005.txt attendees2005.sql 4480
#
# David Harris, Version of 30 Jan 2006 23:06 PST

# ------------------------------------------------

# Check the number of arguments on the command line:
if ($#ARGV+1 != 3) {
  die "load.attendees2005.pl needs <INFIL> <OUTFIL> <starting-serial-number> as arguments";
};
# Check existence of the textfile and email listing file:
(open INFIL, "<$ARGV[0]") or die "load.attendees2005.pl INFIL file $ARGV[0] not found.\n";
(open OUTFIL, ">$ARGV[1]") or die "load.attendees2005.pl OUTFIL file not made.\n";
$startsernums = $ARGV[2];
chomp $startsernums;
$personsernum = $startsernums;  # SET to beginning value by 3rd argument
$personsernum-- ;
#Read through INFIL file:
while ($lineread = <INFIL>) {
  chomp $lineread;
  ($personname,$email,$country,$stateprovetc) = split(/\|/, $lineread);
  $personname =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $personname =~ s/"//g;  #Deal with quotes around names.
  $email =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $email =~ s/"//g;  #Deal with quotes around names.
  if ($stateprovetc eq '') { $stateprovetc = 'NULL' };
  
  $personsernum++ ;
  print OUTFIL "INSERT INTO `person`
(`personsernum`,`personname`,`country`,`email`)
VALUES ($personsernum,\'$personname\',$country,\'$email\');\n" ;
  print OUTFIL "INSERT INTO `personfacts` (`personsernum`,`factname`,`factvalue1`,`factvalue2`)
VALUES ($personsernum,\'attendee2005\',\'$stateprovetc\',$country);\n" ;    
}
close OUTFIL;
close INFIL;
exit;