#!/bin/rc
# G2 should work for both old and new Kirara2
rfork e
#kirara2=/n/other/kirara2
if(~ $#kirara2 0){
	echo set '$kirara2'
	exit
}
kirara=$kirara2
path=(/bin/kirara /bin)

fn usage {
	echo 'usage: G2 query file ...'
	exit
}

fn fsize {
	# usage: fsize [-t] file
	# -t: show size type instead of numeric
	# don't use ls. we need TAB separation
	# look also mkkind
	if(~ $1 -t){
		shift
		lr -lQd 0 $1 |\
		awk -F $tab '{
		  if($4 == 0){print "zero"; next;}
		  if($4 < 100000) {print "tiny"; next;}
		  if($4 < 1000000) {print "small"; next;}
		  if($4 < 10000000) {print "medium"; next}
		  print "large"}'
	}
	if not
		lr -lQd 0 $1 | awk '{print $4}'
}

fn qid {
	# usage: qid $path
	# get qid of the path
	# qid can be used instead of md5sum.
	# using qid is better than md5sum in that it simplifies our program.
	# input must be valid path
	# usage: qid path ...
	# be careful not to append "\n" to output
	ls -qd $1 | awk '{printf("%s", substr($1,2))}'
}

fn qiddir {
	# usahe: qiddir $qid
	# qid to dir
	# example
	# qiddir 000000000006e81d	-> 000000000006/e8/1d
	echo $1 |\
	awk '
	function dir(qid) {n=length(qid); d=substr(qid, 1, n-4); m=substr(qid,n-3,2);r=substr(qid,n-1);
		return d "/" m "/" r;}
	{	print dir($1) }'
}

fn dbpath {
	# usage: dbpath $tardir
	t=() target=() {
	#echo $1 >[1=2]	#DBG
	for(d in $kirara/?*db){
		t=`{grep '^['$nl$tab']*target' $d/db.conf}
		eval $t
		ifs=() {target=`{join $nl $target/}}
		#echo -n $target >[1=2]	#DBG
		t=`{ffm -mf1 -F $tab <{echo -n $target} <{echo $1}}
		if(~ $t ?*)
			echo $d
	}
	}
}

fn gfind_bak {
	# usage: gfind $query $path ...
	# $query is
	#   'qe0 qe1 ... qen'
	# find using grep
	mq=() p=() r=() t=() {
	#echo gfind $#1 $1 >[1=2]	#DBG
	mq=`{echo -n $1}
	#echo '#mq' $#mq $mq >[1=2] # DBG
	shift
	ifs=$nl {r=$*}	# $path ...
	#echo '#r' $#r $r >[1=2] # DBG
	ifs=$nl {p=`{echo $mq(1) | eKR_trans $kirara2/trans}}
	#echo '#p' $#p $p >[1=2] 	#DBG
	if(~ $#mq 1)
		fgrep -ni $nonword0$p^$nonword1 $r /dev/null
	if not {
		ifs=() {t=`{fgrep -ni $nonword0$p^$nonword1 $r /dev/null}}
		#echo '#t' $t >[1=2]	# DBG
		if(~ $#t 0)
			exit
		mq=$mq(2-)
		for(q in $mq){
			#echo '#q' $#q $q >[1=2]	#DBG
			ifs=$nl {p=`{echo $q | eKR_trans $kirara2/trans}}
			#echo '#p' $#p $p >[1=2]	#DBG
			ifs=() {t=`{echo -n $nl$t | fgrep -i $nonword0$p$nonword1}}
			#echo '#t' $#t $t >[1=2]	#DBG
			if(~ $#t 0)
				exit
		}
		echo -n $t
	}
	}
}

fn gfind {
	# usage: gfind $query $path
	# $query is
	#   'qe0 qe1 ... qen'
	# find using grep
	mq=() p=() r=() t=() {
	#echo gfind $#1 $1 >[1=2]	#DBG
	mq=`{echo -n $1}
	#echo '#mq' $#mq $mq >[1=2] # DBG
	shift
	ifs=$nl {r=$*}	# $path ...		#????
	#echo '#r' $#r $r >[1=2] # DBG
	ifs=$nl {p=`{echo $mq(1) | eKR_trans $kirara2/trans}}
	#echo '#p' $#p $p >[1=2] 	#DBG
	if(~ $#mq 1)
		fgrep -ni $nonword0$p^$nonword1 $r /dev/null
	if not {
		ifs=() {t=`{fgrep -ni $nonword0$p^$nonword1 $r /dev/null}}
		#echo '#t' $t >[1=2]	# DBG
		if(~ $#t 0)
			exit
		mq=$mq(2-)
		for(q in $mq){
			#echo '#q' $#q $q >[1=2]	#DBG
			ifs=$nl {p=`{echo $q | eKR_trans $kirara2/trans}}
			#echo '#p' $#p $p >[1=2]	#DBG
			ifs=() {t=`{echo -n $nl$t | fgrep -i $nonword0$p$nonword1}}
			#echo '#t' $#t $t >[1=2]	#DBG
			if(~ $#t 0)
				exit
		}
		echo -n $t
	}
	}
}


fn ifind_bak {
	# usage: ifind $query $path
	# find using ind.gz
	db=() p=() q=() qi=`{qid $2} d=() f=() {
	#echo '#qi' $#qi $qi >[1=2] #DBG
	#db=`{ls -d $kirara/?*db}
	#echo '#db' $#db $db >[1=2] #DBG
	for(d in $kirara/?*db){
		# current G1 is designed for my mixed environment
		# this must be fix in near future
		if(test -e $d/qdir)	# Kirara-2.2
			qd=qdir/$qi
		if(test -e $d/find)	# Kirara-2.1
			qd=find/`{qiddir $qi}
		#echo '#qd' $qd >[1=2]  # find/000000000000/b2/1c or qdir/000000000000b21c
		#echo '#$d/$qd' $d/$qd >[1=2] #DBG
		if(test -e $d/$qd)
			p=$d/$qd
	}
	#echo '#p' $#p $p >[1=2] #DBG
	f=`{ls -p $p}
	#echo $#f $f >[1=2] #DBG

	# translate query to the format acceptable by lkdict
	ifs=$nl {q=`{echo $1 |\
		sed 's/\&/ /g;s/ /)$ (/g;s/\*/.*/g;s/\?/./g;s/^.*$/(&)$/g'}}
	#echo '#q' $#q $q >[1=2] #DBG

	if(~ ind.gz $f){ # now we don't have this case because not good (ind.gz is large!)
		#echo '#' lkdict -nH $2: -S ': ' $q^'$' - $2 >[1=2] #DBG
		gunzip -c $p/ind.gz | lkdict -nH $2: -S ': ' $q - $2
		exit
	}

	if(~ dict:X $f){
		# this case includes even if we have "indx:X"
		#echo '#' lkdict -nH $2: -S ': ' -d $p $q X $2 >[1=2]	#DBG
		lkdict -nH $2: -S ': ' -d $p $q^'$' X $2
	}
	}
}

fn ifind {
	# usage: ifind $query $dir
	# find using ind.gz
	f=() p=() q=() {
	echo $2 >[1=2]	#DBG
	f=`{ls -p $2}
	echo $f >[1=2]	#DBG

	# translate query to the format acceptable by lkdict
	ifs=$nl {q=`{echo $1 |\
		sed 's/\&/ /g;s/ /)$ (/g;s/\*/.*/g;s/\?/./g;s/^.*$/(&)$/g'}}
	#echo '#q' $#q $q >[1=2] #DBG
	if(~ ind.gz $f){ # now we don't have this case because not good (ind.gz is large!)
		#echo '#' lkdict -nH $2: -S ': ' $q^'$' - $2 >[1=2] #DBG
		gunzip -c $p/ind.gz | lkdict -nH $2: -S ': ' $q - $2
		exit
	}
	if(~ dict:X $f){
		# this case includes even if we have "indx:X"
		#echo '#' lkdict -nH $2: -S ': ' -d $p $q X $2 >[1=2]	#DBG
		lkdict -nH $2: -S ': ' -d $p $q^'$' X $2
	}
	}
}

fn find {
	p=$1 size=() d=() q=() {
	#echo '#p' $#p $p >[1=2] 
	shift
	#echo '#*' $#* $* >[1=2] 
	for(f in $*){
		d=`{dbpath $f}
		q=`{qid $f}
		if(test -e $d/qdir)	# Kirara-2.2
			qd=qdir/$q
		if(test -e $d/find)	# Kirara-2.1
			qd=find/`{qiddir $q}
		#echo '#qd' $qd >[1=2]  # find/000000000000/b2/1c or qdir/000000000000b21c
		#echo '#$d/$qd' $d/$qd >[1=2] #DBG
		f=$d/$qd/dict:X
		if(test -d $f)
			ifind $p $f
		if not
			gfind $p $*
	}
	}
}

fn find_bak {
	p=$1 size=() d=() q=() {
	#echo '#p' $#p $p >[1=2] 
	shift
	#echo '#*' $#* $* >[1=2] 
	for(f in $*){
		size=`{fsize -t $f}
		switch($size){
		case zero	# do nothing
			;
		case tiny small	# use fgrep
			gfind $p $f
		case medium large # use lkdict
			ifind $p $f
		}
	}
	}
}

fn join {
	# usage: join $c arg ...
	c=() r=() {
	c=$1
	shift
	r=$1
	shift
	for(a in $*)
		r=$r$c$a
	echo $r
	}
}

while(~ $1 -*){
	switch($1){
	case -*
		usage
		exit
	}
}

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

wd=`{pwd}
#nonword='[^0-9A-Za-z_]'		# word boundary (obsolete)
# now we support non-ascii
# need $sp and $tab
# note that - in [ ] is range operator
nonword='['$sp$tab'!-/:-@[-`{-~]'		# word boundary
nonword0='('$nonword'|^)'
nonword1='('$nonword'|$)'

ifs=$nl {p=`{echo $1 | tolower}}
#echo $p #DBG

# $1 is a query expression. the query expression (QE) is:
# QE := orlist
# QE := orlist " " orlist " " ...
# orlist := term "|" term "|" ...
# wcard := "*" | "?"
# term := word | word wcard | word wcard word | ...
# kfind should pass $1 to G in accordance with the abive rule
# example of $1
# 'foo'
# 'foo bar*'
# 'foo&bar*'
# 'foo|bar* baz*h?ge'
# 'foo&bar* baz*h?ge'		<= not allowed

shift
find $p $*
