#!/usr/bin/perl
#home.adelphi.edu/~ch21221/z.pl

print "content-type:text/html; charset=utf-8\n\n";
use strict;
use warnings;
use CGI qw/:standard/;

my $search = param("input")||"";

my $mfile = "/home/ch21221/public_html/movies.txt";
my $sfile = "/home/ch21221/public_html/streams.txt";
my $afile = "/home/ch21221/public_html/assoclist.txt";

open(my $MOV, '<', $mfile) or die "Could not open file '$mfile' $!";
open(my $STR, '<', $sfile) or die "Could not open file '$sfile' $1";
open(my $ASC, '<', $afile) or die "Could not open file '$afile' $1";

print <<ENDHTML;
<html> <head> <title>Movie Search</title> </head> <body>
 
<a href="movies.txt">movies.txt</a>
<br>
<a href="streams.txt">streams.txt</a>
<br>
<a href="assoclist.txt">assoclist.txt</a>
<br>
    <form method="POST" action="z.pl">
        Enter a search word:<br>
        <input type="text" name="input">
        <br><br>
        <input type="submit" value="OK">
    </form>
ENDHTML

search();


print qq(</body>);
print qq(</html>);
sub search {
    my $key = "";

    while (my $mline = <$MOV>) {
        chomp($mline);
        (my $num, my $movie, my $genre)=split(/\|/,$mline);

        if($movie =~ m/$search/) {
            $key = $num;

            seek $ASC, 0, 0;
            while (my $aline = <$ASC>) {
                chomp($aline);
                (my $movkey, my $strkey)=split(/ /,$aline);

                seek $STR, 0, 0;
                if($movkey eq $key) {
                    while (my $sline = <$STR>) {
                        chomp($sline);
                        (my $let, my $stream, my $price)=split(/\|/, $sline);

                        if($strkey eq $let) {
                            print "$movie is available on $stream<br/>";
                        }
                    }
                }
            }
        }
    }
    
}
