#!/usr/bin/perl
#usewarnings;
#strict;


# This script parses the TWDA

my $debug = 0;

# get the variables
my $filetoparse = $ARGV[0];
my $filetowrite = $ARGV[1];

# set default input if necessary
if (!defined $filetoparse) {
  $filetoparse = "twda.txt";
}

# set default output if necessary
if (!defined $filetowrite) {
  $filetowrite = "card_frequency.txt";
}

open (inputfile, $filetoparse) or die ("\n Could not open $filetoparse \n $! \n");
open (outputfile, ">$filetowrite") or die ("\n Could not create $filetowrite \n $! \n");

my $text;
my $frequency;
my $cardName;
my $tempName;
my @badWords;
my %cards;
while ($text = <inputfile>){
  # ensure we don't double count
  $frequency = 999;
  $cardName = "bogus4";

  # regex: whitespace, some digits, possibly an x, more
  # whitespace, then the rest
  if ( $text =~ /^\s*[xX*]?\s?(\d+)\s?[xX*]?\s*(.*)$/ ){
    $frequency = $1;
    $tempName = $2;

    # 1 word library card, or crypt with tab or 2 spaces
    if ( $tempName =~ /^(\S+)(\t.*|\s\s.*)?$/ ){
      $cardName = $1;
    }
    # 2 word library card, or crypt with tab or 2 spaces
    elsif ( $tempName =~ /^(\S+\s\S+)(\t.*|\s\s.*)?$/ ){
      $cardName = $1;
    }
    # 3 word library card, or crypt with tab or 2 spaces
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+)(\t.*|\s\s.*)?$/ ){
      $cardName = $1;
    }
    # 4 word library card, or crypt with tab or 2 spaces
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+)(\t.*|\s\s.*)?$/ ){
      $cardName = $1;
    }
    # 5 word library card, or crypt with tab or 2 spaces
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+\s\S+)(\t.*|\s\s.*)?$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, just take the first 5 words
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+\s\S+).*$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, just take the first 4 words
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+).*$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, just take the first 3 words
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+).*$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, just take the first 2 words
    elsif ( $tempName =~ /^(\S+\s\S+).*$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, just take the first 1 words
    elsif ( $tempName =~ /^(\S+).*$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, bogus
    else {
      $cardName = "bogus1";
    }
  }
  # ends in a number with an x
  elsif ( $text =~ /^\s*(.*?)\s*[xX*]?\s?\s?(\d+)[xX*]?\s*$/ ){
    $frequency = $2;
    $tempName = $1;

    # 1 word card
    if ( $tempName =~ /^(\S+)$/ ){
      $cardName = $1;
    }
    # 2 word library card
    elsif ( $tempName =~ /^(\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 3 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 4 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 5 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, bogus
    else {
      $cardName = "bogus2";
    }
  }
  # ends in a number in parens or brackets
  elsif ( $text =~ /^\s*(.*?)\s*[[(]\s?[xX*]?(\d+)[xX*]?[\])]\s*$/ ){
    $frequency = $2;
    $tempName = $1;

    # 1 word card
    if ( $tempName =~ /^(\S+)$/ ){
      $cardName = $1;
    }
    # 2 word library card
    elsif ( $tempName =~ /^(\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 3 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 4 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # 5 word library card
    elsif ( $tempName =~ /^(\S+\s\S+\s\S+\s\S+\s\S+)$/ ){
      $cardName = $1;
    }
    # If nothing matched yet, bogus
    else {
      $cardName = "bogus3";
    }
  }
  # Exactly 5 words on a line
  elsif ( $text =~ /^(\s*\S+\s\S+\s\S+\s\S+\s\S+)$/ ){
    $cardName = $1;
    $frequency = 1;
  }
  # Exactly 4 words on a line
  elsif ( $text =~ /^(\s*\S+\s\S+\s\S+\s\S+)$/ ){
    $cardName = $1;
    $frequency = 1;
  }
  # Exactly 3 words on a line
  elsif ( $text =~ /^(\s*\S+\s\S+\s\S+)$/ ){
    $cardName = $1;
    $frequency = 1;
  }
  # Exactly 2 words on a line
  elsif ( $text =~ /^(\s*\S+\s\S+)$/ ){
    $cardName = $1;
    $frequency = 1;
  }
  # Exactly 1 word on a line
  elsif ( $text =~ /^(\s*\S+)$/ ){
    $cardName = $1;
    $frequency = 1;
  }

  # safety check
  if ($frequency < 100) {
    # set everything to lower case
    $cardName = lc $cardName;
    # fix stupid apostrophes
    $cardName =~ s/`/'/g;

    # special checks
    if ($cardName =~ /(second tradition)|(2nd tradition)/) {
      $cardName = "second tradition: domain, the";
    }
    elsif ($cardName =~ /(fifth tradition)|(fifth tradition)/) {
      $cardName = "fifth tradition: hospitality, the";
    }
    elsif ($cardName =~ /(wake with evening's)|(wwef)/) {
      $cardName = "wake with evening's freshness";
    }
    elsif ($cardName =~ /krc/) {
      $cardName = "kine resources contested";
    }

    $cards{$cardName} += $frequency;
  }

}

@badWords = ("action", "ally", "bogus", "cards", "combat", "deck", "equipment", "event", "grand total", "joueurs", "library", "master", "methuselah", "minion", "number", "participants", "people", "players", "political", "r . f", "r.f", "reaction", "retainer", "total", "modifiers", "=", "------");

# print out the results
foreach $cardName (sort (keys(%cards))) {
  $safe = 1;
  foreach $badWord (@badWords) {
    if ($cardName =~ /^$badWord/ ) {
      $safe = 0;
    }
  }
  if ($safe) {
    print outputfile ($cards{$cardName} ."\t". $cardName."\n");
  }
}

close (inputfile);
close (outputfile);

print "\n $filetowrite created from $filetoparse \n";
