#!/bin/rc
rfork e
# usage: kdict [-d $dic] $query

wordlen=2

dir=$home/dic	# your dictionary directory
eijiro=eiji-138.txt
ryaku=ryaku138.txt
ei98=eijiro98.txt
war=tolstoy-war_and_peace.txt
pre_war='■'

if(~ $#* 0){
	echo kdict '''query here''' ' |p  # Eijiro'
	echo kdict -d ryaku '''query here''' ' |p 	# Ryakujiro'
	echo kdict -d war '''query here''' ' |p 	# Tolstoy: War and peace'
	exit
}

dic=eijiro	# default dictionary name
if(~ $1 -d){
	shift
	dic=$1
	shift
}

path=(/bin/kirara /bin)

tab='	'
sp=' '
nl='
'

fn chkpattern {
	p=$1 e=() {
	# check the pattern
	if(echo $p | grep -s '[!@#$%^-=+\`~:;"{},./''[\]]'){
		echo 'unallowed symbol in the pattern'
		exit
	}
	e=''
	for(x in `{seq 1 $wordlen})
		e=$e'[^*?|]'
	if(echo $p | grep -sv '(^| |&)'$e){
		echo 'pattern must begin with two or more regular letters'
		exit
	}
	}
}

tr0='!/,~?'
tr1='$$ ^.'
chkpattern $1

ifs=$nl {q=`{echo $1 | tr $tr0 $tr1 | tolower | sed 's/\*/.*/g'}}

#echo lkdict -d $dir $q $dic $dir/$$dic	# DBG


if(~ $#(pre_$dic) 0)
	lkdict -d $dir $q $dic $dir/$$dic
if not
	lkdict -H '■' -d $dir $q $dic $dir/$$dic
