#!/usr/bin/perl

$bio_file = "misnorcal.txt";

open(FILE, "< $bio_file");
@lines = <FILE>;
close(FILE);

foreach $line (@lines) {
   chomp $line;
   if (($line eq "")) { next; }

   $line =~ s/'/&#39;/g;
   ($key, $lname, $fname, $dob, $birthplace, $entered_army, $discharged_army, $c1, $c2, $c3, $c4, $c5, $c6, $c7, $c8, $notes1, $notes2, $honor1, $honor2, $honor3, $oral_history, $auto, $ques, $bio, $photo, $article, $narrative) = split(/\t/, $line);
   $lname =~ s/"//g; $lname =~ s/\n//g; $lname =~s/^ //g; $lname =~ s/ $//g;
   $fname =~ s/"//g; $fname =~ s/\n//g; $fname =~ s/^ //g; $fname =~ s/ $//g;
   $birthplace =~ s/"//g;
   $notes1 =~ s/"//g; $notes1 =~ s/\n//g;
   $notes2 =~ s/"//g; $notes2 =~ s/\n//g;

   $dob = &get_date("$dob");
   $entered_army = &get_date("$entered_army");
   $discharged_army = &get_date("$discharged_army");
   $honors = &get_honors($honor1, $honor2, $honor3);

   # print "INSERT INTO veterans (veteran_id, last_name, first_name, date_of_birth, birthplace, army_entered, army_discharged, notes, honors) VALUES ('$key', '$lname', '$fname', '$dob', '$birthplace', '$entered_army', '$discharged_army', '$notes1<BR>$notes2', '$honors');\\g\n";

   $campaigns[0] = $c1; $campaigns[1] = $c2; $campaigns[2] = $c3;
   $campaigns[3] = $c4; $campaigns[4] = $c5; $campaigns[5] = $c6;
   $campaigns[6] = $c7; $campaigns[7] = $c8;

   &veteran_campaign($key, $campaigns);
}

sub get_honors {
   my ($h1, $h2, $h3) = $_;
   my $res = '';

   if ($h1 eq '' && $h2 eq '' && $h3 eq '') {
      return "";
   }

   if ($h1 != '') { $res .= $h1 . ","; }
   if ($h2 != '') { $res .= $h1 . ","; }
   if ($h3 != '') { $res .= $h1 . ","; }

   $res =~ s/,$//g;
   return $res;
}

sub get_njahs_resources {
   my ($or, $au, $qu, $bi, $ph, $ar) = $_;
   my $res = '';

   if ($or eq '1') { $res = "Oral History"; }
   if ($au eq '1') { $res .= ", Autobiography"; }
   if ($qu eq '1') { $res .= ", Questionnaire"; } 
   if ($bi eq '1') { $res .= ", Biography"; }
   if ($ph eq '1') { $res .= ", Photos"; }
   if ($ar eq '1') { $res .= ", Articles"; }

   return $res;
}

sub get_date {
   my $date = $_[0];
   my $y, $m, $d;

   ($m, $d, $y) = split(/\//, $date);

   if ($m eq '?') { $m = 01; }
   if ($y eq '?') { $y = 0000; }
   if ($d eq '?') { $d = 01; }

   return "$y-$m-$d";
}

sub veteran_campaign {
   my $v_id = $_[0];
   my $campaigns = $_[1];

   if ($campaigns[0] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782344')\\g\n";
   }
   if ($campaigns[1] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782343')\\g\n";
   }
   if ($campaigns[2] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782342')\\g\n";
   }
   if ($campaigns[3] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782345')\\g\n";
   }
   if ($campaigns[4] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782346')\\g\n";
   }
   if ($campaigns[5] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782347')\\g\n";
   }
   if ($campaigns[6] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782341')\\g\n";
   }
   if ($campaigns[7] == 1) {
      print "INSERT INTO _veteran_campaign (veteran_id, campaign_id) VALUES ('$v_id', '46782348')\\g\n";
   }
}

sub get_njahs_resources {
   my ($or, $au, $qu, $bi, $ph, $ar) = @_;
   my $res = '';

   if ($or eq '1') { $res = "Oral History, "; }
   if ($au eq '1') { $res .= "Autobiography, "; }
   if ($qu eq '1') { $res .= "Questionnaire, "; } 
   if ($bi eq '1') { $res .= "Biography, "; }
   if ($ph eq '1') { $res .= "Photos, "; }
   if ($ar eq '1') { $res .= "Articles, "; }

   $res =~ s/, $//;
   return $res;
}




