#! /bin/sh
#This is script for management of dictionary. It works with bash script "mova".
#ver. 4.0 (C) E.S.Cymbalyuk 1999. Under GNU GPL. No Warranty.
#The latest versions see on www.chat.ru/~mueller_dic or www.geocities.org/mueller_dic\
exec wish "$0" "$@"

#Default Directory for "share/mova/.movarc", if absent "~/.movarc"
set DIR /usr/local/
set OPT -W
set DIARY .mova
global u
set u 1.0
#catch {encoding system identity}
#catch {encoding system iso8859-1}
catch {encoding system koi8-r}

switch $argc 0 {} 1 {set OPT_ARG [lindex $argv 0]} 2 {
set OPT_ARG [lindex $argv 0]
set DIC_ARG [lindex $argv 1]
tk appname tk$DIC_ARG} 3 {
set OPT_ARG [lindex $argv 0]
set DIC_ARG [lindex $argv 1]
set word [lindex $argv 2]} default {
set argc 0
puts "You used excrescent parameters with movaTK."}

catch {set OPT $OPT_ARG}

proc GetOptions {} {
global DIR DIC DIR_TMP DIC_ARG FONT_FIND FONT_TEXT FONT_D FONT_DG FONT_IPA
if {([catch {set rcfile [open "~/.movarc_${DIC_ARG}" "r"]} Error] &&
[catch {set rcfile [open "${DIR}share/mova/.movarc_${DIC_ARG}" "r"]}  Error] &&
[catch {set rcfile [open "~/.movarc" "r"]} Error] &&
[catch {set rcfile [open "${DIR}share/mova/.movarc" "r"]} Error]) != 0} {
set FONT_FIND -*-*-bold-r-*-*-17-*-*-*-*-*-koi8-r
set FONT_TEXT -*-*-medium-r-*-*-17-*-*-*-*-*-koi8-r
set FONT_D -*-*-medium-o-*-*-17-*-*-*-*-*-koi8-r
set FONT_DG -*-*-bold-o-*-*-17-*-*-*-*-*-koi8-r
set FONT_IPA -*-silsophiaipa-*-r-*-17-*-*-*-*-*-*-*
set DIR /usr/local/
set DIR_TMP /tmp/
set DIC Mueller7GPL.koi
} else {
gets $rcfile FONT_FIND
gets $rcfile FONT_TEXT
gets $rcfile FONT_D
gets $rcfile FONT_DG
gets $rcfile FONT_IPA
gets $rcfile DIR
gets $rcfile DIR_TMP
gets $rcfile DIC
close $rcfile}
catch {set DIC $DIC_ARG}
wm title . "$DIC   MOVA-4.0 E.S.Cymbalyuk GPL"
wm focusmodel . passive}

proc WordLight_base {} {
global word
if {$word==""} {return}
set w  1.0
while {[set w [.word search -count len -nocase -- $word $w end]]!=""} {
.word tag add searched_word $w "$w + $len char"
set w [.word index "$w +$len char"]}}

proc WordLight {} {
global word
if {$word==""} {return}
set a 1.0
set b 1.0
set c 1.0
set d 1.0
set e 1.0
set f 1.0
set g 1.0
while {[set a [.text search -count len -regexp {[a-zA-Z]+} $a end]]!=""} {
.text tag add english_word $a "$a + $len char"
set a [.text index "$a +$len char"]}
while {[set g [.text search -count len -regexp {^.+  } $g end]]!=""} {
.text tag add entry_word $g "$g + $len char"
set g [.text index "$g +$len char"]}
while {[set b [.text search -count len -nocase -- $word $b end]]!=""} {
.text tag add searched_word $b "$b + $len char"
set b [.text index "$b +$len char"]}
while {[set d [.text search -count len -regexp {\_[^ ]+[\.:]} $d end]]!=""} {
.text tag add duty_word $d "$d + $len char"
set d [.text index "$d +$len char"]}
while {[set e [.text search -count len -regexp {\_[a-zA-Z-]+[\.:]} $e end]]!=""} {
.text tag add duty_grammar $e "$e + $len char"
set e [.text index "$e +$len char"]}
while {[set f [.text search {_} $f end]]!=""} {
.text tag add underscore $f "$f +1 char"
set f [.text index "$f +1 char"]}
while {[set c [.text search -count len -regexp {\[[^\ ]+\]} $c end]]!=""} {
.text tag add phonetic $c "$c + $len char"
set c [.text index "$c +$len char"]}}

proc Mova {arg} {
global word argv0 DIC DIR
if {[catch {set word [selection get]} error] != 0} {
bell -displayof .
puts "Let us mark a word by the mouse and then run:\n$argv0 OPT DICTIONARY\nWhere OPT is \"-W\" or \"-B\" or \"-S\" or \"-T\". DICTIONARY - name of
 dictionary (if the one was't set, you will use default dictionary from \"~/.movarc\"). Let us run \"mova\" for explanation of OPT.\n"
} else {
set word [string tolower [string trimleft $word "       \"\(\{"]]
.word insert 0.0 "$word\n\n"
.word tag delete searched_word
.word tag configure searched_word -foreground brown
WordLight_base
if {$word != ""} {exec ${DIR}bin/movaMTK $arg $DIC $word &}}}

proc Call_hash_dic {arg hash_word} {
global word index_length index_dic DIR DIC DIR_TMP
set in [open "${DIR}share/dict/$DIC" "r"]
set out [open "$DIR_TMP${DIC}M.tmp" "w"]
seek $in $index_dic($hash_word) start
fcopy $in $out -size $index_length($hash_word)
if {[catch {set fd [open "|${DIR}bin/mova $arg $word -d $DIR_TMP${DIC}M.tmp" "r"]} error] != 0} {
puts "Can't run ${DIR}bin/mova"
.text insert 0.0 "Can't run ${DIR}bin/mova\n"
} else {
.text insert end "[read $fd]"
close $fd}
close $in
close $out
catch {file delete "$DIR_TMP${DIC}M.tmp"}}

proc Call_dic {arg} {
global word DIR DIC
 if {[catch {set fd [open "|${DIR}bin/mova $arg $word -d ${DIR}share/dict/$DIC" "r"]} error] != 0} {
puts "Can't run ${DIR}bin/mova"
.text insert 0.0 "Can't run ${DIR}bin/mova\n"
} else {
.text insert end "[read $fd]"
close $fd}}

proc Read_hash {} {
global index_length index_dic DIC DIR
if {[catch {set hash_file [open "${DIR}share/dict/$DIC.hash" "r"]} error] == 0} {
set old_offset 0
while {[eof $hash_file]==0} {
gets $hash_file line
set hash_dic [string index $line 0][string index $line 1]
set offset [string trimleft $line $hash_dic]
set index_dic($hash_dic) $old_offset
set index_length($hash_dic) [expr $offset - $old_offset]
set old_offset $offset}
close $hash_file}}

proc Change_dic {} {
global index_length index_dic FONT_FIND
GetOptions
.entry configure -font $FONT_FIND
.word configure -font $FONT_FIND
catch {unset index_dic}
catch {unset index_length}
Read_hash}

proc PutDiary {} {
global word DIARY
if {[catch {set fd [open "~/$DIARY" "r"]} Error] != 0} {
.text insert end "no ~/$DIARY"
} else {
.text delete 0.0 end
.text insert end "[read $fd]"
set word " "
WordLight
.button_upper.save configure -text "Rewrite \"$DIARY\""
.button_upper.add configure -text "Add to \"$DIARY\""}}


GetOptions
switch $argc 0 - 1 - 2 {
entry .entry -background #fafa33 -state normal -width 30 -textvariable word -font $FONT_FIND
bind .entry <Return> ".entry selection range 0 end; Mova {-W}"
bind .entry <Shift-Return> ".entry selection range 0 end; Mova {-B}"
bind .entry <Control-Return> ".entry selection range 0 end; Mova {-S}"
bind .entry <Alt-Return> ".entry selection range 0 end; Mova {-T}"
text .word -state normal -width 30 -height 15 -wrap word -font $FONT_FIND
.word tag delete searched_word
.word tag configure searched_word -foreground brown
WordLight_base
frame .button
button .button.mova -text "First Word" -command {Mova {-W}}
button .button.mova_part -text "Beginning of First Word" -command {Mova {-B}}
button .button.mova_in_string -text "Tandem of Words" -command {Mova {-S}}
button .button.mova_part_in_string -text "Tandem of Symbols" -command {Mova {-T}}

if {[catch {set fd [open "|/bin/ls -1 ${DIR}share/dict/" "r"]} error] != 0} {
puts "Can't run /bin/ls"
} else {
set Files "[read $fd]"
close $fd
foreach files $Files {if {[ string match *.hash $files ] == 0} {lappend Dictionaries $files}}}

frame .button_upper
set m .button_upper.dic.m
menubutton .button_upper.dic -relief flat -background #22fafa -text "Choose Dictionary" -underline 7 -menu $m
menu $m
foreach dic $Dictionaries {$m add radiobutton -indicator 0 -label "$dic" -variable DIC_ARG -value $dic -command {Change_dic}}

pack .button_upper -side top -fill x
pack .button_upper.dic -side left -expand yes -fill both
pack .entry -side top -fill x
pack .word -side left -fill y
pack .button -side right -expand yes -fill both
pack  .button.mova .button.mova_part .button.mova_in_string .button.mova_part_in_string  -side top -expand yes -fill both

Read_hash
Mova $OPT
bind all <Escape> {destroy .}
} 3 {
set Diaries {.mova}
if {[catch {set fd [open "|/bin/ls -a -1 " "r"]} error] != 0} {
puts "Can't run /bin/ls"
} else {
set Files "[read $fd]"
close $fd
foreach files $Files {if {[ string match .mova_* $files ] != 0} {lappend Diaries $files}}}

text .text -background #fafafa -state normal -width 50 -height 20 -font $FONT_TEXT -yscrollcommand ".scroll.scroll set"

frame .scroll
scrollbar .scroll.scroll -width .4c -command ".text yview"
button .scroll.bottom -relief raised -text "" -command {
if {[catch {set wo [selection get]} error] != 0} {bell -displayof .
} else {set word $wo}
.text tag delete find_word
if {[set u [.text search -count len -nocase -- $word $u ]]!=""} {
.text tag add find_word $u "$u + $len char"
set u [.text index "$u +$len char"]
.text tag configure find_word -background yellow
.text see $u}}
button .scroll.top -relief raised -text "" -command {
if {[catch {set wo [selection get]} error] != 0} {bell -displayof .
} else {set word $wo}
.text tag delete find_word
if {[set u [.text search -backwards -count len -nocase -- $word $u ]]!=""} {
.text tag add find_word $u "$u + $len char"
.text tag configure find_word -background yellow
.text see $u}}

frame .button_upper
button .button_upper.help -relief flat -background #22fafa -text "Help" -command {
.text delete 0.0 end
if {[catch {set fd [open "${DIR}share/mova/Usage_koi.txt" "r"]} Error] != 0} {
.text insert end "no ${DIR}share/mova/Usage_koi.txt"
} else {
.text insert end "[read $fd]"
set word " "}}

button .button_upper.new -relief flat -background #22fafa -text "New Diary" -command {
if {[catch {set DIARY [selection get]} error] != 0} {set word " "
 bell -displayof .
.text insert 0.0 "Let us mark a name of diary by the mouse and then press this button.\n\n"
} else {
set DIARY .mova_$DIARY
.button_upper.save configure -text "Rewrite \"$DIARY\""
.button_upper.add configure -text "Add to \"$DIARY\""}}

button .button_upper.save -relief flat -background #22fafa -text "Rewrite \"$DIARY\"" -command {
set fd [open "~/$DIARY" "w"]
puts $fd [.text get 0.0 end]
close $fd
set a 0
foreach fil $Diaries {if {[string match $DIARY $fil] != 0} {set a 1}}
if {$a == 0} {
lappend Diaries $DIARY
.button_upper.read.r add radiobutton -indicator 0 -label "$DIARY" -variable DIARY -value $DIARY -command {PutDiary}}}

button .button_upper.add -relief flat -background #22fafa -text "Add to \"$DIARY\"" -command {
set fd [open "~/$DIARY" "a"]
puts $fd [.text get 0.0 end]
close $fd
set a 0
foreach fil $Diaries {if {[string match $DIARY $fil] != 0} {set a 1}}
if {$a == 0} {
lappend Diaries $DIARY
.button_upper.read.r add radiobutton -indicator 0 -label "$DIARY" -variable DIARY -value $DIARY -command {PutDiary}}}

set r .button_upper.read.r
menubutton .button_upper.read -relief flat -background #22fafa -text "Read Diary" -underline 0 -menu $r
menu $r
foreach dia $Diaries {$r add radiobutton -indicator 0 -label "$dia" -variable DIARY -value $dia -command {PutDiary}}

pack .button_upper -side top -fill x
pack .button_upper.help .button_upper.new .button_upper.add .button_upper.save .button_upper.read -side left -expand yes -fill both
pack .scroll.top -side top -fill y -expand no
pack .scroll.scroll -side top -fill both -expand yes
pack .scroll.bottom -side top -fill y -expand no
pack .text -side left -fill x -expand yes
pack .scroll -side left -fill y -expand no
.text tag configure english_word -foreground blue
.text tag configure entry_word -font $FONT_FIND -foreground blue
.text tag configure duty_word -font $FONT_D -foreground darkgreen
.text tag configure duty_grammar -font $FONT_DG
.text tag configure searched_word -foreground brown
.text tag configure phonetic -font $FONT_IPA -foreground red
.text tag configure underscore -foreground white

set hash_word [string index $word 0][string index $word 1]
if {([catch {set offset_dic $index_dic($hash_word)} error] != 0) || [string match $arg "-S"] || [string match $arg "-T"]} {
Call_dic $OPT}
if {([catch {set offset_dic $index_dic($hash_word)} error] == 0) && ([string match $arg "-W"] || [string match $arg "-B"])} {
Call_hash_dic $OPT $hash_word}

WordLight
focus .scroll.scroll
bind all <Escape> {destroy .}}
#End
