#!/bin/rc
rfork e

usage='usage: kfind1 [-kG] pattern
-G: don''t output G and the pattern
-k: keep pattern in output'

#kirara1=/n/other/kirara1
if(~ $#kirara1 0){
	echo set '$kirara1' >[1=2]
	exit
}
. $kirara1/kirara.conf	# need for $wordlen
path=(/bin/kirara /bin)
# don't include '.' to the path

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

ifs0=$sp$tab$nl
ifs=$ifs0

reg=no
keep=no	# keep pat in output
nog=no

#	a generic routine to regulate arguments
#	NB: options that are followed by string is not supported
a=()
while(~ $1 -*){
	switch($1){
	case -
	echo $usage >[1=2]
	exit
	case -?
	a=($a $1)
	shift
	case -??*
	a=($a `{echo $1 | sed s/-//|sed 's/[a-zA-Z]/-& /g'})
	shift
	}
}

fn chkpattern {
	p=$1 e=() {
	# check the pattern
	if(echo $p | grep -s '[!-%''()+-/:->@[-^`{}~]'){
		echo 'unallowed symbol in the pattern' >[1=2]
		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' >[1=2]
		exit
	}
	}
}

fn xind {
	# usage: xind $patt
	# $patt is regexp with '|'.
	# decompose by '|'
	# and extract first two chars from them and search mtoc
	# don't echo to stdout for debug
	p=() q=() r=() s=(){
	ifs='|' {r=`{echo $1}}	# decompose by '|'
	for(s in $r){
		p=`{echo $s | awk -v 'wordlen='$wordlen '{print substr($0,1,wordlen)}'}
		# mtoc is space separated
		q=`{fgrep -gf1 -F $sp $p mtoc}
		if(! ~ $#q 0)
			ftrim -s $q(2) -e $q(3) main
	}
	}
}

fn find {
	p=() q=$* r=() {
	#pwd >[1=2]	#DBG. eg. /n/other/kirara1/sysdb
	# DBG: example of $1 is ('foo|bar' 'ali*e|bo?')
	#echo '#q' $#q $q >[1=2]	#DBG
	#xind $q(1) >[1=2]	#DBG
	#xind $q(1) | fgrep -gf1 -F $tab $q(1) >[1=2]		#DBG
	xind $q(1) | fgrep -gf1 -F $tab $q(1) | awk '{print $2}' | sort -u > _kf_c
	if(! test -s _kf_c)
		exit
	q=$q(2-)
	for(r in $q){
		#xind $r | fgrep -gf1 -F $tab $r | awk '{print $2}' | sort -u | comm -12 _kf_c - >[1=2]	#DBG
		xind $r | fgrep -gf1 -F $tab $r | awk '{print $2}' | sort -u | comm -12 _kf_c - > _kf_c1
		#echo  '#r' $#r $r >[1=2]	#DBG
		#wc _kf_c1 >[1=2]	#DBG
		mv _kf_c1 _kf_c
		if(! test -s _kf_c)
			exit
	}
	cat _kf_c
	#rm _kf_* >[2] /dev/null
	}
}

#pwd >[1=2]	#DBG

*=($a $*)

while(~ $1 -*){
	switch($1){
	case -G
	nog=yes
	shift
	case -k
	keep=yes
	shift
	case -*
	echo $usage >[1=2]
	exit
	}
}

if(! ~ $#* 1){
	echo $usage >[1=2]
	exit
}

chkpattern $1
ifs=$nl {p=`{echo $1 | tolower}}
p=`{echo $p | tr '&' ' '}
if(~ $keep no)
	ifs=$nl {q=`{echo $1 | tr '&' '|'}}
if not
	q=$1
#echo '#p' $#p $p >[1=2]	#DBG
#echo '#q' $#q $q >[1=2]	#DBG
for(db in $kirara1/*db){
	#echo $db >[1=2] #DBG
	if(~ $nog yes)
		@{cd $db && find $p | KR_trans qd | sort -u}
	if not{
		@{cd $db && find $p | KR_trans -F $tab qd | sort -u |\
		awk -F $tab -v 'query='$q '
		function quote(s){ gsub("''","''''", s); return "''" s "''" }
		/[ |&[\]*?;$^={}@#()`<>'']/ {print "G1","''" query "''",quote($0); next}
		{print "G1","''" query "''",$0}
		'}
	}
}
