#!/usr/bin/perl 

#Зчитування словника 

my %dict = ();
open(DIC, "dict.txt"); 
while ($line = <DIC>)
{
 ($orig, $trans) = split (/=/, $line,2);
  $orig =~ s/^\s+//;
  $orig =~ s/\s+$//;
  $trans =~ s/^\s+//;
  $trans =~ s/\s+$//; 
  $dict{ $orig } = $trans;  
}
close(DIC);

open(FC, "person.txt");

while ($line = <FC>)
{
 if ($line =~ m/^\{\{/)
{
 print "skipping\n";
} 
else 
{
 if ($line =~ m/^\}\}/)
 {
  print "skipping\n";
 } 
 else
 {
  $line =~ s/^\s+//;
  ($tag, $value) = split(/=/,$line,2);
  $tag =~ s/^\|//;
  $tag =~ s/^\s+//;
  $tag =~ s/\s+$//; 
  $value =~ s/^\s+//;
  $value =~ s/\s+$//;
  print "$tag $value \n";
  $name = $value if ($tag eq "name");
  $image = $value if ($tag eq "image");
  $image_size = $value if ($tag eq "image_size");
  $caption = $value if ($tag eq "caption");
  $birth_name = $value if ($tag eq "birth_name");
  $alt = $value if ($tag eq "other_names");
  $residence = $value if ($tag eq "residence");
  $birth_date = $value if ($tag eq "birth_date");
  $birth_place = $value if ($tag eq "birth_place");
  $death_date = $value if ($tag eq "death_date");
  $death_place = $value if ($tag eq "death_place");
  $death_cause = $value if ($tag eq "death_cause");
  $nationality = $value if ($tag eq "nationality");
  $ethnicity = $value if ($tag eq "ethnicity");
  $citizenship = $value if ($tag eq "citizenship");
  $known_for = $value if ($tag eq "known_for");
  $occupation = $value if ($tag eq "occupation");
  $title = $value if ($tag eq "title");
  $salary = $value if ($tag eq "salary");
  $term = $value if ($tag eq "term");
  $predecessor = $value if ($tag eq "predecessor");
  $successor = $value if ($tag eq "successor"); 
  $party = $value if ($tag eq "party"); 
  $religion = $value if ($tag eq "religion"); 
  $husband = $value if ($tag eq "spouse");
  $wife = $value if ($tag eq "spouse");
  $children = $value if ($tag eq "children");
  $relatives = $value if ($tag eq "relations");  
  $awards =   $value if ($tag eq "awards");
  $website = $value if ($tag eq "website");    
  $signature = $value if ($tag eq "signature");
  $notes = $value if ($tag eq "footnotes");  
}
}  
} 
close(FC);

# Обробка дат 
@months = ("січня", "лютого", "березня", "квітня", "травня", "червня", "липня", "серпня", "вересня", "жовтня", "листопада", "грудня");
if (($birth_date =~ m/birth date/) || ($birth_date =~ m/Birth date/)  )
{
 ($tag, $year, $month, $day) = split(/\|/,$birth_date,5);
 $mn = @months[$month-1]; 
 $birth_date = "[[$day $mn]] [[$year]]"; 
}

if (($death_date =~ m/death date/) || ($death_date =~ m/Death date/))
{
 ($tag, $year, $month, $day) = split(/\|/,$death_date,5);
 $mn = @months[$month-1]; 
 $death_date = "[[$day $mn]] [[$year]]"; 
}

# Переклад посилань 
while ( my ($key, $value) = each(%dict) ) {
	$birth_place =~ s/$key/$value/;
	$death_place =~ s/$key/$value/;
    }


print<<EndCard
{{Особа
| ім'я                 = $name
| жінка                = $lady
| місце_проживання     = $residence
| інші_імена           = $alt
| зображення           = $image
| розмір_зображення    = $image_size
| підпис_зображення    = $caption
| ім'я_при_народженні  = $birth_name
| дата_народження      = $birth_date
| місце_народження     = $birth_place
| дата_смерті          = $death_date
| місце_смерті         = $death_place
| причина_смерті       = $death_cause
| громадянство         = $citizenship
| національність       = $nationality
| відомий_(відома)     = $known_for
| рід_діяльності       = $occupation
| титул                = $title 
| військове звання     = $rank
| платня               = $salary
| термін               = $term
| попередник           = $predecessor
| наступник            = $successor
| партія               = $party
| головував_(-ла)      = $voted_for
| віросповідування     = $religion
| чоловік              = $husband
| дружина              = $wife
| діти                 = $children
| родичі               = $relatives
| підпис               = $signature
| нагороди             = $awards
| сторінка_в_інтернеті = $website
| примітки             = $notes
| employer             = $employer
| зріст                = $height
| вага                 = $weight
| герб                 = $coat_of_arms
| підпис_герба         = $coa_caption
}}
EndCard
;