#!/bin/rc
# usage: mkkind [-b] [-u] $dir
# -b: build
# -u: update (default)
# where $dir is the target path such as $arisawa/netlib/9fans
# where is this command executed?	let the place be $wdir
# where is the result stored?	-> $wdir
# where are working files placed?	-> $wdir
# the working dir must be $kirara/*db/qdir/*/
# mkkind should do nothing except to $wdir.
# mkkind is used in mkdb

rfork e

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

msize=3000000	# make ind.gz if (total size of text) > $msize. 	# tunable

fn dsize {
	# usage: dsize dir ...
	# total ammount of size of text files
	istext -d $* | 9xa ls -l | awk 'BEGIN{sum=0}{sum += $6}END{print sum}'
}

fn qid {
	# 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
	# term% ls -qd /usr/arisawa/adm/fossil/
	# (000000000006e927 6 80) /usr/arisawa/adm/fossil
	ls -qd $1 | awk '{printf("%s\n", substr($1,2))}'
}


fn mkqtn {
	# mkqtn produce qtn which is a table of ($qid, $time, $name)
	# $name is a name of file (not full path) in the directory.
	# only text files should be included.
	# lr sorts by name
	# "--" is a terminator of option. there are files such as "-f", "-" etc.
	@{cd $1 && istext -hd . | 9xa lr -lqQnd0 -- | awk -F $tab '$4 ~ /^-/ {printf("%s\t%s\t%s\n",$1,$8,$9)}'}
	# the output is for example
	# 00000000000c4701	1374801921	unq.c
}

fn xw {
	# usage: xw [-d] path ...
	# -d: dir
	# xw reads files created by extwords and tranform to:
	#   $word "\t" $name
	# the $name is name of file such as "unq.c"
	# note that extwords is binary safe
	opt=() t=() {
	if(~ $1 -*){
		opt=$1
		shift
	}
	if(~ $opt -d){
		# "$*" end up with "/" so ...
		ifs=$nl {t=`{lr -d 0 $*^* | grep -v '\/$|'$exclude}}
	}
	if not
		ifs=$nl {t=`{echo $* | grep -v $exclude}}

	if(! ~ $#t 0){
		extwords -ih -l $wordlen $t| \
		awk '
		/\/:$/ {name=substr($1,1,length($1)-2); next}
		/:$/ {name=substr($1,1,length($1)-1); next}
		/.*/ {print $1 "\t" name}
		'
	}
	}
}

wd=`{pwd}

opt=()
while(~ $1 -*){
	opt=$1
	shift
}

q=`{qid $1}
dq=qdir/$q
n=`{dsize $1}
if(~ $n 0){
	if(test -d $dq)
		rm -rf $dq
	exit
}

if(test $n -lt $msize){
	xw -d $1 | awk -v 'q='$q '{print $1 "\t" q}' | sort -u
	if(test -d $dq)
		rm -rf $dq
	exit
}

if(! test -d $dq){
	mkdir $dq
	opt=-b
}
cd $dq

if(~ $opt -b || ! test -e qtn){
	# then we must generate:
	# 1. qtn
	# 2. ind.gz	for large dir
	# for large dirs
	mkqtn $1 | fgrep -vf3 -F $tab $exclude > qtn
	if(! test -s qtn){
		# all filered out
		rm qtn
		cd ..
		rm $q
		exit
	}
	# qtn is for example
	# 00000000000c4701	1374801921	unq.c
	xw -d $1 > _t
	awk '{print $1}' _t | sort -u > _w
	sort -u _t | gzip > ind.gz
	# then ind is (for example)
	# Biobuf	uniq.c
	# ...
	awk -v 'q='$q '{print $1 "\t" q}' _w
	rm _t _w
	exit
}

#	don't forget mtime bug of cwfs
#	so we must compare qtn with the real world
#	when we decide if update is necessary or not.
#
#	even if we havn't the mtime bug, it's better
#	to compare new qtn and old qtn to speed up

mkqtn $1 | fgrep -vf3 -F $tab $exclude > _q
fgrep -vf3 $exclude qtn > _q0
diff _q0 _q > _d
mv _q qtn
ifs=$nl {rem=`{awk -F $tab '/^</{print $3}' _d}}
ifs=$nl {add=`{awk -F $tab '/^>/{print $3}' _d}}
rm _d _q0
# file in $exclude is removed from $add and $rem

if(~ $#rem 0 && ~ $#add 0){
	gunzip -c ind.gz
	exit
}

>_t
if(test -e ind.gz){
	gunzip ind.gz
	# ind format is
	#   word "\t" name
	#for example
	# ARGBEGIN	istext.c
	if(! ~ $#rem 0){
		# remove files in $rem
		grep -v $tab'('`{echo $rem | tr ' ' '|'}^')$' ind >> _t
	}
	for(f in $add){
		# add files in $add
		xw $1/$f >> _t
	}
	rm ind
}
if not {
	xw -d $1 >> _t
	awk '{print $1}' _t | sort -u |\
	awk -v 'q='$q '{print $1 "\t" q}'
}
# we must create ind.gz
sort -u _t | gzip > ind.gz
awk -v 'd='$q '{print $1 "\t" d}' _t | sort -u
rm _t
