#!/usr/bin/perl -w

# Usage: load.nodisplays.pl <INFIL> <OUTFIL> <starting-personnum>
#
# Example:  load.nodisplays.pl nodisplays.31Dec05.csv nodisplays.31Dec05.sql 647
#
# David Harris, Version of 1 Jan 2005 3:06 GMT

# ------------------------------------------------

# Check the number of arguments on the command line:
if ($#ARGV+1 != 3) {
  die "load.nodisplays.pl needs <INFIL> <OUTFIL> <database table name> as arguments";
};
# Check existence of the textfile and email listing file:
(open INFIL, "<$ARGV[0]") or die "load.nodisplays.pl INFIL file $ARGV[0] not found.\n";
(open OUTFIL, ">$ARGV[1]") or die "load.nodisplays.pl OUTFIL file not made.\n";
$startsernums = $ARGV[2];
chomp $startsernums;
$lastemail = '';
$personsernum = $startsernums;  # SET to beginning value by 3rd argument, EACH TIME!!!
$personsernum-- ;
#Read through INFIL file:
while ($lineread = <INFIL>) {
  chomp $lineread;
  ($oldpersonsernum,  #Will be replaced by $personsernum
   $personname,$persontitle,$profaffil,$streetaddress1,$streetaddress2,
   $cityaddress,$stateprovetc,$postalcode,$country,$email,$website,$password,
   $mayemail,$maydisplay,$datevalid,$lastupdated) = split(/\|/, $lineread);
  $personname =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $personname =~ s/"//g;  #Deal with quotes around names.
  $persontitle =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $persontitle =~ s/"//g;  #Deal with quotes around names.
  $profaffil =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $profaffil =~ s/"//g;  #Deal with quotes around names.
  $streetaddress1 =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $streetaddress1 =~ s/"//g;  #Deal with quotes around names.
  $streetaddress2 =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $streetaddress2 =~ s/"//g;  #Deal with quotes around names.
  $cityaddress =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $cityaddress =~ s/"//g;  #Deal with quotes around names.
  $stateprovetc =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $stateprovetc =~ s/"//g;  #Deal with quotes around names.
  $postalcode =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $postalcode =~ s/"//g;  #Deal with quotes around names.
  $email =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $email =~ s/"//g;  #Deal with quotes around names.
  $website =~ s/'/\\'/g;  #Deal with O'Connor etc.
  $website =~ s/"//g;  #Deal with quotes around names.

  #$personname =~ s/,/\\,/g;  #Deal with commas.
  #$personname =~ s/;/\\;/g;  #Deal with semicolons etc.
  if ($lastemail ne $email) {  #Skip duplicated emails!  Assumes email sorted list
    $personsernum++ ;
    # The format of the SQL INSERT line:
    #print OUTFIL "INSERT INTO `$tablename` (`email`) VALUES (\'$lineread\');\n" ;
    print OUTFIL "INSERT INTO `PERSON`
        VALUES (\'$personsernum\',\'$personname\',\'$persontitle\',
          \'$profaffil\',\'$streetaddress1\',\'$streetaddress2\',
          \'$cityaddress\',\'$stateprovetc\',\'$postalcode\',
          \'$country\',\'$email\',\'$website\',\'$password\',
          \'$mayemail\',\'$maydisplay\',\'$datevalid\',\'$lastupdated\');\n" ;

    print OUTFIL "INSERT INTO `PERSONFACTS` (`personsernum`,`factname`,`factvalue1`,`factvalue2`)
      VALUES (\'$personsernum\',\'isnodisplay\',\'2005\',\'NULL');\n" ;    
  }
  if ($lastemail eq $email) {
    #$personsernum-- ;
    print OUTFIL "INSERT INTO `PERSONFACTS` (`personsernum`,`factname`,`factvalue1`,`factvalue2`)
      VALUES (\'$personsernum\',\'issupporter\',\'2005\',\'NULL');\n" ;        
  }
  $lastemail = $email;
}
close OUTFIL;
close INFIL;
exit;