#!/usr/bin/perl

use utf8;
use Encode;
binmode STDIN,":utf8";
binmode STDOUT,":utf8";

$regexp=$ARGV[0];
$regexp=decode("utf8",$regexp);

$page=$ARGV[1];
shift @ARGV;
$number_of_printed = 0;
$number_of_printed_max = 10;
#if (open(PAGE, "lynx -dump '$page'|")) {
for $page (@ARGV) {
    if (open(PAGE, "$page")) {
        binmode PAGE,":utf8";
        local $/;
        $text=<PAGE>;
        $text =~ s@http://[a-zA-Z&_.:/0-9%?=,\#+()\[\]~-]*@@g;
        $text =~ s@\n@@g;
        $text =~ s@(Mr|Mrs|viz)\.@\1POINT@g;
        $text =~ s@e\.g\.@ePOINTgPOINT@g;
        $text =~ s@i\.e\.@iPOINTePOINT@g;
        @sentences=split /[.!?]/, $text;
        for (@sentences) {
            s@iPOINTePOINT@i\.e\.@g;
            s@ePOINTgPOINT@e\.g\.@g;
            s@(Mr|Mrs|viz)POINT@\1.@g;
            s/^\s*//;
            s/\s*$//;
            s/\[[0-9]+\]//g;
            s/\s+/ /g;
            if (/\b$regexp\b/ and $number_of_printed < $number_of_printed_max ) {
                $mark_start = "\e[35;1m";
                $mark_stop = "\e[0m";
                s/\b($regexp)\b/$mark_start$1$mark_stop/;
                print "$_.\n\n";
                $number_of_printed++;
            }
            if ($number_of_printed >= $number_of_printed_max) {
                exit(0);
            };
        }
    }
}
