#!/usr/bin/perl -w
use strict;

#(This license is from zlib of Jean-loup Gailly and Mark Adler)
#
# Copyright (C) 2002 Frederick Dean <software@fdd.com>
#
#  This software is provided 'as-is', without any express or implied
#  warranty.  In no event will the authors be held liable for any damages
#  arising from the use of this software.
#
#  Permission is granted to anyone to use this software for any purpose,
#  including commercial applications, and to alter it and redistribute it
#  freely, subject to the following restrictions:
#
#  1. The origin of this software must not be misrepresented; you must not
#     claim that you wrote the original software. If you use this software
#     in a product, an acknowledgment in the product documentation would be
#     appreciated but is not required.
#  2. Altered source versions must be plainly marked as such, and must not be
#     misrepresented as being the original software.
#  3. This notice may not be removed or altered from any source distribution.
#
# Contributor(s):    Frederick Dean <software@fdd.com>
#                    perlipc man page


#
#   The script does not handle well words with multiple entries.
#

use IO::Socket;

# headers must end with "\015\012"
# The return value is a list.  
# The first element is the response body.
# The second element is a hash ref to the headers.
sub webget
{
   my($url,$headers,$body) = @_;

   $url =~ /^http:\/\/(.*?)(\/.*)$/ or die("bad url $url\ndied");;
   my($host,$pathfile) = ($1,$2);
   my $port = 80;
   if($host =~ s/:(\d+?)$//) { # remove optional port
      $port = $1;
   }
   my $EOL = "\015\012";
   if(!defined($headers)) {
      $headers = "";
   }
   if($host =~ /[a-zA-Z]/ && $headers !~ /^host:/mi) {
      $headers .= "Host: $host$EOL";
   }
   my $remote = IO::Socket::INET->new( Proto     => "tcp",
                                       PeerAddr  => $host,
                                       PeerPort  => "$port");
   if (!defined($remote)) {
      die "cannot connect to port $port on $host"
   }
   $remote->autoflush(1);
   if(defined($body)) {
      $headers .= "Content-Length: " . length($body) . $EOL;
      if($headers !~ /^content-type:/mi) {
         $headers .= "Content-Type: application/x-www-form-urlencoded$EOL";
      }
   }
   my $method = "GET";
   $method = "POST" if defined($body);
   print $remote "$method $pathfile HTTP/1.0$EOL" . $headers . $EOL;
   print $remote $body if(defined($body));
   my $response;
   $remote->read($response,1000000);
   close $remote;
   my($head,$body2) = split(/$EOL$EOL/,$response,2);
   #$head .= $EOL;
   return $body2 if(!wantarray());
   my %headers;
   for my $header (split(/$EOL/,$head)) {
      my($name,$value) = split(/:/,$header,2);
      next if(!defined($value));
      chomp($value);
      $headers{$name} = $value;
   };
   return($body2,\%headers);
}

my $ansiBright = "\033[1m";
my $ansiBlue = "\033[34m";
my $ansiGreen = "\033[32m";
my $ansiRed = "\033[31m";
my $ansiNormal = "\033[0m";

if($#ARGV != 0) {
   die("usage:  webster word\n");
}

my $word = $ARGV[0];

my $page = webget("http://m-w.com/cgi-bin/dictionary","Referer: http://m-w.com/\015\012","book=Dictionary&va=$word");

if( $page =~ m/(Main Entry:.*?)<\/form>/smi) {
   my $ansidef = $1;
   $ansidef =~ s/\n//gs;         # remove newlines
   $ansidef =~ s/<br>/\n/gs; 
   $ansidef =~ s/<i>/$ansiBlue/gs; 
   $ansidef =~ s/<\/i>/$ansiNormal/gs;  
   $ansidef =~ s/<b>/$ansiBright/gs; 
   $ansidef =~ s/<\/b>/$ansiNormal/gs;  
   #$ansidef =~ s/^(\w+?):/$ansiRed$1$ansiNormal:/gm;  
   $ansidef =~ s/<.*?>//gs;  # remove tags
   $ansidef =~ s/&lt;/</gs; 
   $ansidef =~ s/&gt;/>/gs; 
   $ansidef =~ s/&amp;/&/gs; 
   print $ansidef . "\n\n"; 
} elsif($page =~ m/Suggestions\s+for.*<PRE>(.*?)<\/PRE>/si) {
   my $ansialt = $1;
   $ansialt =~ s/<.*?>//gs;  # remove tags
   print $ansialt . "\n"; 
} else {
   print $page;
}




