#!/usr/bin/perl -w

#Jeanenne Campbell
#Chris Demartin

#Link to website: /home/~je21226/public_html/perlproject/partA/final.pl/

print "content-type:text/html; charset=utf-8\n\n";
print qq (<!DOCTYPE html>);
print qq (<html>);
print qq (<head>);
print qq (<title> Perl website </title>);
print qq (</head>);
print qq (<body>);
print qq (<a href = "months.txt"> months</a>);
print qq (<br />);
print qq (<a href = "link.txt"> Associations</a>);
print qq (<br />);
print qq (<a href = "cons.txt"> Anime Conventions</a>);
print qq (<form method = "POST" action = "final.pl">);
print qq (<br />);
print qq (Search Word:<br>);
print qq (<input type = "text" name = "searchword">);
print qq (<br><br/>);
print qq (<input type = "submit" value = "OK">);
print qq (</form>);

use strict;
use warnings;
use CGI qw /:standard /;

my $searchword = param ("searchword") || "";

if ($searchword)
  {
    print qq (<br />You searched for: $searchword <br />);
    print qq (<br />);
my $file = "/home/pe16132/public_html/csc271/note/scripts/perl/jeanette/months.txt";
my $fd = open (my $fh, '<', $file);
my @keylistarray = ();

while (my $line = <$fh>)
  {
    chomp $line;

   my $key = "";
    my $month = "";
  
  if ($line =~ /.*${searchword}.*/)
      {
        ($key, $month) = split (/\./, $line);
        push @keylistarray, $key;
      }
  }
	print "the months found have keys of @keylistarray";

close ($fh);

my $splitkey = "";
my @linkkeylist = ();
my $file2 = "/home/pe16132/public_html/csc271/note/scripts/perl/jeanette/link.txt";
my $linkkey = "";
my $element = "";

foreach $element (@keylistarray)
{
  my $fileopen = open (my $fh2, '<', $file2);

  while (my $line2 = <$fh2>)
    {
      chomp $line2;

      if ($line2 =~ /^${element},/)
        {
        ($linkkey, $splitkey) = split (/\,/, $line2);
          push @linkkeylist, $splitkey;

        }
    }
  close ($fh2);
}
print "the linked keys are @linkkeylist";

my $splitcons = "";
my @conkeylist = ();
my $file3 = "/home/pe16132/public_html/csc271/note/scripts/perl/jeanette/cons.txt";
my $cons = "";
my $elementcon = "";

my $found = 0;

foreach $elementcon ( @linkkeylist)
{
  print "dealing with elementcon $elementcon";
  my $confileopen = open (my $fh3, '<', $file3);

  while (my $conline = <$fh3>)
    {
		 print "conline is $conline";
      chomp $conline;
 ($cons, $splitcons) = split (/\./, $conline);
      if ($splitcons =~ /^${elementcon}/);
        {
				print " matched key to value in file" ;
       
				print " cons is $cons and split is $splitcons";

          if($found == 0)
{
          print qq(The stores that have that game in stock are: );
          print qq(<br />);
          $found = 1;
}
          print qq(<br />);
          print qq(Cons: $cons);
          print qq(<br />);
         
        }
    }
  close ($fh3);
}
if($found == 0)
{
print qq(Please try again.);
print qq(<br />);
}

  print qq(<br />);

  }

else
  {
    print qq (<br />Please enter a search word. <br />);
  }
print qq (</body>);
print qq (</html>);

