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

fn usage {
	echo 'usage: G1 query directory ...'
	exit
}

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))}'
}

# for Kirara-2.1
fn qiddir {
	# usage: 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 mtest {
	# extract files (from args) that all exps in $query matchs
	# $query is
	#   'qe0 qe1 ... qen'
	# usage: mtest $query file ...
	mq=() p=() r=() h=() {
	ifs=$ifs^'&' {mq=`{echo -n $1}}
	#echo '#mq' $#mq $mq >[1=2] # DBG
	shift
	ifs=$nl {r=$*}	# file ...
	#echo '#r' $#r $r >[1=2] # DBG
	for(q in $mq){
		# if fgrep has an option to replace ":" (separator) to $tab
		# we could have more efficialt algorith
		h=()
		ifs=$nl {p=`{echo $q | eKR_trans $kirara1/trans}}
		#echo '#p' $#p $p >[1=2] #DEB
		#echo '#r' $#r $r >[1=2] #DEB
		for(f in $r){
			if(fgrep -is $p $f)
				h=($h $f)
		}
		r=$h
		#echo '#r' $#r $r >[1=2] # DBG
	}
	#echo '#r' $#r $r >[1=2] #DBG
	#echo `{join '|' $r}	>[1=2]	#DBG
	ifs=() {r=`{join $tab $r}}	# for the files with space
	echo -n	$r
	}
}

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'|$)'

linewise=no
if(~ $1 *' '*)
	linewise=yes

ifs=$nl {qe=`{echo -n $1 | tolower}}
# $qe is original query expression, keep it!

# $1 is a query expression. the query expression (QE) is:
# QE := orlist
# QE := orlist "&" 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 above rule
# example of $1
# 'foo'
# 'foo&bar*'
# 'foo bar*'
# 'foo|bar* baz*hoge'
# 'foo&bar* baz*h?ge'		<= not allowed

ifs=$ifs0^'&' {mq=`{echo $qe}}
# $mq is list of query expression
#echo '#mq' $#mq $mq >[1=2] # DBG
shift

for(d in $*){
	if(! test -e $d){
		echo $d 'not exist'
		exit
	}
	if(! test -d $d)
		usage
	db=`{dbpath $d}
	#echo '#db' $db >[1=2]	#DBG
	# format of XXXdb/$qd/ind is
	# word "\t" filename
	# I want output of
	# filename:word		# this is the form of grep
	if(~ $#db 0){
		echo '#' $d 'not in database'
		exit
	}

	@{cd $d	# BEGIN cd
	q=`{qid .}
	# current G1 is designed for my mixed environment
	# this must be fix in near future
	if(test -e $db/qdir)	# Kirara-2.2
		qd=qdir/$q
	if(test -e $db/dind)	# Kirara-2.1
		qd=dind/`{qiddir $q}
	#echo '#qd' $qd >[1=2]  # dind/000000000000/b2/1c or qdir/000000000000b21c

	ind=$db/$qd/ind.gz
	if(test -e $ind){
		#echo '#mq(1)' $mq(1) >[1=2]	# DBG
		ifs=() {u=`{gunzip -c $ind |\
			fgrep -gf1 -F $tab $mq(1) | awk '{print $2}' | sort -u}}
		#echo '#u' $u	# DBG
		xmq=$mq(2-)
		for(q in $xmq){
			#echo '#q' $q >[1=2]	# DBG
			ifs=() {v=`{gunzip -c $ind |\
				fgrep -gf1 -F $tab $q | awk '{print $2}' | sort -u}}
			#echo '#v' $v >[1=2]	# DBG
			ifs=() {u=`{comm -12 <{echo $u} <{echo $v}}}
			#echo '#u' $u	# DBG
			if(~ $#u 0)
				exit
		}
		#echo '#u' $u # DBG
		ifs=$nl {t=`{echo $u}}
	}
	if not
		ifs=$nl {t=`{istext -dh .}}		# for the path with space
	# then $t is a list of files separated by $nl
	# files may include spaces
	#echo '#t' $#t $t >[1=2] # DBG
	if(~ $#t 0)
		exit

	if(~ $linewise yes){
		ifs=$nl {p=`{echo -n $mq(1) | eKR_trans $kirara1/trans}}
		#echo '#p' $#p $p >[1=2] # DBG
		ifs=() {t=`{fgrep -in $nonword0$p^$nonword1 $d$t /dev/null}}
		#echo '#t' $#t $t >[1=2] # DBG
		xmq=$mq(2-)
		#echo '#xmq' $#xmq $xmq >[1=2] # DBG
		for(q in $xmq){
			ifs=$nl {p=`{echo -n $q | eKR_trans $kirara1/trans}}
			#echo '#p' $#p $p >[1=2] # DBG
			ifs=() {t=`{echo -n $t | fgrep -i $nonword0$p$nonword1}}
			#echo '#t' $#t $t >[1=2] # DBG
		}
		#echo '#t' $#t $t >[1=2] # DBG
		if(! ~ $#t 0)
			echo -n $t
	}
	if not{
		ifs=$tab$nl {u=`{mtest $qe $t}}
		if(! ~ $#u 0){
			#echo '#u' $#u $u >[1=2] # DBG
			# $u is the list of files that matchs $qe
			ifs=$nl {p=`{echo -n $qe | tr '&' '|' | eKR_trans $kirara1/trans}}
			fgrep -in $nonword0$p$nonword1 $d$u /dev/null
		}
	}
	}	# END cd
}
