#!/usr/local/bin/tclsh8.3

# Copyright 2002 Jose Nazario  <jose@monkey.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# usage for people

proc usage {} {
	puts "usage: google.tcl term1 \[term2 ...] \[!]"
	puts "\tchoose ! if you feel lucky (netscape only)"
	exit
}

# send the url to google.

proc getUrl {url} {
	set sock [socket www.google.com 80]
	puts $sock "GET $url HTTP/1.0"
	puts $sock "Connection: Keep-Alive"
	puts $sock "User-Agent: Monkey cmdline tool (OpenBSD; en)"
	puts $sock "Host: www.google.com:80"
	puts $sock "Accept-Encoding: gzip"
	puts $sock "Accept-Language: en"
	puts $sock "Accept-Charset: iso-8859-1,utf-8"
	puts $sock "\n"
	flush $sock
	set out [read $sock]
	close $sock
	return $out
}

# handle luckiness. netscape remote events from:
# 	http://wp.netscape.com/newsref/std/x-remote.html

proc doLuck {word} {
	exec netscape -remote openURL([set word])
	exit
}

set lucky 0

# process cmdline args here. if one if them is ! we use the get lucky
# redirect a netscape window to the first one. otherwise return the list
# of found urls.

if {$argc<1} {
	usage
}

set url "/search?hl=en&ie=ISO-8859-1&q="

for {set i 0} {$i < $argc} {incr i 1} {
	if {[lindex $argv $i]=="!"} {
		set lucky 1
		break
	} else {
		append url [lindex $argv $i]
		append url "+"
	}
}

# main event loop.

# execute the search
set output [getUrl $url]

# process what came back
set start 0
set num 0
foreach line [split $output "\n"] {
	# split the body and find the end of the google header. then
	# split the html into pieces, and if a field is href, we 
	# return the next field.
	if [regexp "Results" $line] {
		incr start 1
	}
	if {$start} {
		regsub -all \[\<\"\>] $line { } line 
		if [regexp "br clear" $line] {
			set start 0
		}
		foreach word $line {
			if [regexp "http://" $word] {
				if ![regexp "/url\\?q=|cache|google|dictionary.com" $word] {
					incr num 1
					regsub -all ^href= $word {} word
					if {$num > 0} {
						if !{$lucky} {
							puts "$num\t$word"
						} else {
							doLuck $word
						}
					}
				}
			}
		}
	}
}

# EOF
