#!/bin/bash 
# program define
# version 1.0  
# written by Andrew V. Newman
# last modified Fri Mar  5 07:27:17 MST 2004

# Shell script to bring up google based definitions for any compound or simple word 

# Checks to see if you have a word in mind or just typing in the command. If the latter,
# it will give you a short usage and example before exiting with error code 1. 
if [ ${#} -lt "1" ]; then
   echo "Usage: `basename $0` 'TRM' "
   echo "   where TRM is the word that you would like defined"
   echo "   Examples:"
   echo "     `basename $0` Fedora"
   echo "     `basename $0` shell script"
   echo "    or:"
   echo "     `basename $0` google"
   exit 1
fi

# Use 'resize' to correctly set the environmental variables for your terminal 
# (this may not work for all terminals). 
  eval `resize -u` 

# Set the lynx program to your current term width (doesn't do it by 
# default if being piped directly into another file), and turn off the 
# default justification (can make output UGLY). 
  LYNX="/usr/bin/lynx -justify=0 -width=$COLUMNS"

# Set your 'more' or 'less' favorite pager.
  PAGER=/bin/more
  PAGER=/usr/bin/less

# Sets a URL BASE assuming multiple variables as a compound word.  The
# way WORD is defined, it will replace all the blank spaces with the URL
# equivalent of a blank '%20'. 
  WORD=` echo ${@} | sed  's/\ /\%20/g'`

# Define the google URL to search for the definition. 
  URL=http://www.google.com/search?q=define:$WORD 

# Call up the google search in lynx and dump the output to a temp file
# and all stderr to /dev/null . 
  $LYNX $URL -dump 2> /dev/null >define.tmp
  
# Displays definition after stripping off unnecessary text before and 
# after the definitions (the first sed command only prints lines 
# between the line containing 'Definition' and the line containing 
# '_____', inclusive.  Then it pipes it through a second sed command 
# which replaces the 'lynx' URL numbers with 'WEB:', before piping it 
# through a third sed command that wipes out that last line.  Finally
# the information is piped to a pager command which lets you page 
# through the text on-screen with the space bar. 
  sed -n '/[dD]efinition/,/_____/p' define.tmp | \
     sed '/\[[0-9][0-9]\]/s//   WEB: /' |\
     sed '/____/d' | $PAGER 

# Remove temporary file. 
  rm define.tmp
# Exit cleanly. 
  exit 0