#!/d2/monty/bin/wish -f
##########################################################################
##  N.E.C. America                                                      ##
##  Satellite Software Division                                         ##
##                                                                      ##
##                                                                      ##
##                                                                      ##
##			Tool to search the dictionaries for a string        		##
##        Created Tue Nov 29 15:32:42 CST 1994 by Monty Scroggins       ##
##								                                    	##
##########################################################################

## Standard error handling
proc tkerror {err} {
    global errorInfo
    puts stdout "$errorInfo"
}
##
##
## Set all of the search files to either on or off, and toggle the string
## in the button from All to None or vice versa
##
proc setall {} {
    global ABB AST BIO CAR CHA CHI COM ETC FAM FAS FEM JUN BIB MAL MOV MYT NUM\
      OTH PHR PLA SCI SHA SPO SUR YID OLD TFLAG
    upvar #0 TOGGLE TOGGLE
    if {$TOGGLE == "All"} {
	set TOGGLE "None"
	set TFLAG 1
    } {
	set TOGGLE "All"
	set TFLAG 0
    }
    set LIST {ABB AST BIO CAR CHA CHI COM ETC FAM FAS FEM JUN BIB MAL MOV MYT\
      NUM OTH PHR PLA SCI SHA SPO SUR YID OLD}
    foreach SELECT $LIST {
	set $SELECT $TFLAG
    }
}
##
##
## Process to actually perform the search and build the output file in 
## "/usr/tmp".  Once the file is built, it is displayed via the last
## switch option "END"  Clunky way to do things, but it does work..
##
##
proc search_it {} {
    global ABB AST BIO CAR CHA CHI COM ETC FAM FAS FEM JUN BIB MAL MOV MYT NUM\
      OTH PHR PLA SCI SHA SPO SUR YID OLD END DICDIR GCMD
    upvar #0 STR STR
    if {$STR==""} {
	.status_line config -text "No Search String!"
	update idletasks
	return
    }
    upvar #0 CST CST
    if {$CST=="0"} {
	set GCMD "/bin/fgrep -i "
    } {
	set GCMD "/bin/fgrep "
    }
    # Clear out any crap that might be in the list
    .main.resultbox delete 0 end
    .result_label config -text "0 Match(es)"
    exec echo "Search Results " >/usr/tmp/search.out
    upvar #0 STR STR
    set LIST {ABB AST BIO CAR CHA CHI COM ETC FAM FAS FEM JUN BIB MAL MOV MYT\
      NUM OTH PHR PLA SCI SHA SPO SUR YID OLD END}
    set CNT 0
    ##
    ## Replace the "/d2/monty/doc/dict part of the following pathnames with 
    ## the your path to the dictionary files.  This is a really clunky thing
    ## to have to do - but for some reason fgrep would not work with a variable
    ## for the path/file name!.. 
    ##
    foreach SELECT $LIST {
	switch $SELECT {
	"ABB" {
		if {$ABB == 1} {
		    .status_line config -text "Searching Abbreviations.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/abbr\
		      >>/usr/tmp/search.out}
		    countit
		}
	    }
	"AST" {
		if {$AST == 1} {
		    .status_line config -text "Searching Asteroids.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/asteroids >>/usr/tmp/search.out}
		      countit
		}
	    }
	"BIO" {
		if {$BIO == 1} {
		    .status_line config -text "Searching Biology.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/biology\
		      >>/usr/tmp/search.out}
		    countit
		}
	    }
	"CAR" {
		if {$CAR == 1} {
		    .status_line config -text "Searching Cartoons.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/cartoon\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"CHA" {
		if {$CHA == 1} {
		    .status_line config -text "Searching Characters.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/chars\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"CHI" {
		if {$CHI == 1} {
		    .status_line config -text "Searching Chinese.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/chinese\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"COM" {
		if {$COM == 1} {
		    .status_line config -text "Searching Passwords.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/passwords >>/usr/tmp/search.out}
		      countit
		}
	    }
	"ETC" {
		if {$ETC == 1} {
		    .status_line config -text "Searching Etc-Hosts.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/etc-hosts >>/usr/tmp/search.out}
		      countit
		}
	    }
	"FAM" {
		if {$FAM == 1} {
		    .status_line config -text "Searching Famous.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/famous\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"FAS" {
		if {$FAS == 1} {
		    .status_line config -text "Searching Fast-Names.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/fast-names >>/usr/tmp/search.out}
		      countit
		}
	    }
	"FEM" {
		if {$FEM == 1} {
		    .status_line config -text "Searching Female-Names.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/female-names >>/usr/tmp/search.out}
		      countit
		}
	    }
	"JUN" {
		if {$JUN == 1} {
		    .status_line config -text "Searching Junk.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/junk\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"BIB" {
		if {$BIB == 1} {
		    .status_line config -text "Searching King James Bible.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/kjbible\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"MAL" {
		if {$MAL == 1} {
		    .status_line config -text "Searching Male Names.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/male-names >>/usr/tmp/search.out}
		      countit
		}
	    }
	"MOV" {
		if {$MOV == 1} {
		    .status_line config -text "Searching Movies.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/movies\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"MYT" {
		if {$MYT == 1} {
		    .status_line config -text "Searching Myths-Legends.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/myths-legends >>/usr/tmp/search.out}
		      countit
		}
	    }
	"NUM" {
		if {$NUM == 1} {
		    .status_line config -text "Searching Numbers.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/numbers\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"OTH" {
		if {$OTH == 1} {
		    .status_line config -text "Searching Other-Names.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/other-names >>/usr/tmp/search.out}
		      countit
		}
	    }
	"PHR" {
		if {$PHR == 1} {
		    .status_line config -text "Searching Phrases.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/phrases\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"PLA" {
		if {$PLA == 1} {
		    .status_line config -text "Searching Places.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/places\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"SCI" {
		if {$SCI == 1} {
		    .status_line config -text "Searching Science-Fiction.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/science-fiction >>/usr/tmp/search.out}
		      countit
		}
	    }
	"SHA" {
		if {$SHA == 1} {
		    .status_line config -text "Searching Shakespeare.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict//bin/shakespeare >>/usr/tmp/search.out}
		      countit
		}
	    }
	"SPO" {
		if {$SPO == 1} {
		    .status_line config -text "Searching Sports.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/sports\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"SUR" {
		if {$SUR == 1} {
		    .status_line config -text "Searching Surnames.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR\
		      /d2/monty/doc/dict/surnames >>/usr/tmp/search.out}
		      countit
		}
	    }
	"YID" {
		if {$YID == 1} {
		    .status_line config -text "Searching Yiddish.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /d2/monty/doc/dict/yiddish\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"OLD" {
		if {$OLD == 1} {
		    .status_line config -text "Searching Look Data File.."
		    update idletasks
		    catch {exec /bin/ksh $GCMD $STR /usr/share/lib/dict/words\
		      >>/usr/tmp/search.out}
		      countit
		}
	    }
	"END" {
		.status_line config -text "Building Display.."
		update idletasks
		after 500 {
	    set FD [open /usr/tmp/search.out "r"]
	    if {[gets $FD line] == -1} then {
	        catch {close $FD}
	    }
	    while {[gets $FD line] >= 0} {
	        .main.resultbox insert end $line
	    }
	    close $FD
	    .status_line config -text "Ready.."
	    }
	  }
	}
  }
}
proc countit {} {
		set CNT -1
	    set FD [open /usr/tmp/search.out "r"]
	    while {[gets $FD line] >= 0} {
	        incr CNT
	        }
	    close $FD
	    if {$CNT < 0 } {set CNT 0}
	    .result_label config -text "$CNT Match(es)"
}
##
##  Main menu display attributes.  
##
##
## pick a few colors and fonts to use throughout.
set BOLDFONT "7x13bold"
set MAINCOLOR "lightsteelblue3"
set SECONDCOLOR "lightblue2"
set STR ""
set FCNT 0
wm title . "Word 1.0   Monty Scroggins 11-30-94"
wm geometry . 404x436+100+100
global TOGGLE DICDIR GCMD

##
## Set the initial text in the all/none button to "All"
##
set TOGGLE "All"
##
## Set the initial case sensitivity to "off"
##
set TFLAG 0
##
## Set the initial state of the "look data file" to be on.  This is the major
## word file for the look command on the suns
##
set OLD 1
## outside border
frame .main -bg $MAINCOLOR -relief ridge -bd 2
frame .main.cbbox -bg $MAINCOLOR -relief ridge -bd 2
frame .main.cbbox2 -bg $MAINCOLOR -relief ridge -bd 2
frame .main.cbbox3 -bg $MAINCOLOR -relief ridge -bd 2
frame .main.buttbox -bg $MAINCOLOR -relief flat -bd 2
##
## The Results box with scrollbar..
##
listbox .main.resultbox -bg $SECONDCOLOR -relief groove -bd 2 -yscrollcommand\
  ".main.scrollbar set"
scrollbar .main.scrollbar -bg $SECONDCOLOR -relief groove -bd 2 -command\
  ".main.resultbox yview"

entry .main.ent_txt -width 20 -relief ridge -bg $SECONDCOLOR -fg black\
  -font $BOLDFONT -bd 2 -textvariable STR

##
## The bank of checkbuttons..
##
checkbutton .main.mbcs1 -variable CST -text "Case Sensitive" -relief raised\
  -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb1 -variable ABB -text "Abbreviations   "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb2 -variable AST -text "Asteroids       "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb3 -variable BIO -text "Biology         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb4 -variable CAR -text "Cartoon         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb5 -variable CHA -text "Characters      "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb6 -variable CHI -text "Chinese         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb7 -variable COM -text "Passwords       "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb8 -variable ETC -text "Etc Hosts       "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox.mb9 -variable FAM -text "Famous          "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb10 -variable FAS -text "Fast Names      "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb11 -variable FEM -text "Female Names    "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb12 -variable JUN -text "Junk            "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb13 -variable BIB -text "Bible           "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb14 -variable MAL -text "Male Names      "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb15 -variable MOV -text "Movies          "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb16 -variable MYT -text "Myths Legends   "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb17 -variable NUM -text "Numbers         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox2.mb18 -variable OTH -text "Other Names     "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb19 -variable PHR -text "Phrases         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb20 -variable PLA -text "Places          "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb21 -variable SCI -text "Sci Fiction     "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb22 -variable SHA -text "Shakespeare     "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb23 -variable SPO -text "Sports          "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb24 -variable SUR -text "Surnames        "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb25 -variable YID -text "Yiddish         "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR
checkbutton .main.cbbox3.mb26 -variable OLD -text "Look Data File  "\
  -relief raised -borderwidth 1 -font $BOLDFONT -bg $SECONDCOLOR

button .main.cbbox3.b26 -relief raised -command "setall" -borderwidth 1\
  -font $BOLDFONT -bg $SECONDCOLOR -textvariable TOGGLE

label .main.lb_key -text "Search String :" -font $BOLDFONT -bg $MAINCOLOR
label .result_label -font $BOLDFONT -bg $MAINCOLOR -text "0 Match(es)" -bd 2
label .status_line -font $BOLDFONT -bg $MAINCOLOR -fg black

button .main.buttbox.cancel -text "Cancel" -font $BOLDFONT -relief raised\
  -width 12 -bd 2 -bg azure2 -fg black -command "destroy ."
button .main.buttbox.execute -text "Search!" -font $BOLDFONT -relief raised\
  -width 12 -bd 2 -bg azure2 -fg black -command "search_it"

##
##  Display the whole form
##

pack .main -fill both -expand yes
pack .main.buttbox -side bottom -fill x -padx 2

place .main.lb_key -in .main -x 4 -y 5
place .main.ent_txt -in .main -x 114 -y 5
place .main.mbcs1 -in .main -x 276 -y 5

place .main.cbbox -x 4 -y 30
place .main.cbbox2 -x 134 -y 30
place .main.cbbox3 -x 264 -y 30
pack .main.cbbox.mb1 .main.cbbox.mb2 .main.cbbox.mb3 .main.cbbox.mb4\
  .main.cbbox.mb5 .main.cbbox.mb6 .main.cbbox.mb7 .main.cbbox.mb8\
  .main.cbbox.mb9 -side top -fill x

pack .main.cbbox2.mb10 .main.cbbox2.mb11 .main.cbbox2.mb12 .main.cbbox2.mb13\
  .main.cbbox2.mb14 .main.cbbox2.mb15 .main.cbbox2.mb16 .main.cbbox2.mb17\
  .main.cbbox2.mb18 -side top -fill x

pack .main.cbbox3.mb19 .main.cbbox3.mb20 .main.cbbox3.mb21 .main.cbbox3.mb22\
  .main.cbbox3.mb23 .main.cbbox3.mb24 .main.cbbox3.mb25 .main.cbbox3.mb26\
  .main.cbbox3.b26 -side top -fill x

place .main.resultbox -x 4 -y 230 -width 368
pack .main.buttbox.cancel .main.buttbox.execute -side right
place .result_label -in .main -x 155 -y 208
place .main.scrollbar -x 376 -y 230 -height 174
place .status_line -x 7 -y 410 -width 207
##
## Setup control-c and control-q to kill the form.
## and Return to start the search
##
bind . <Control-c> {
    destroy .
}
bind . <Control-q> {
    destroy .
}
bind . <Return> {
    search_it
}
bind .main.ent_txt <Control-q> {
    destroy .
}
bind .main.ent_txt <Control-c> {
    destroy .
}
bind .main.ent_txt <Return> {
    search_it
}

##
## Set the string entry field to active initially and set the status line
## to "Ready"
##
.status_line config -text "Ready.."
focus .main.ent_txt
##
## Load any command line argument into the search string field..
##
set STR "[lindex $argv 0]"
#
