#!/bin/sh -f

function do_usage()
{
	echo "Usage:"
	echo "	  $0 {-d|-i} <object_path>  <index_path> [<rel_path>]"
	echo "	      -d: make index when document deleted"
	echo "	      -i: make index when document added"
	echo "        <object_path> : path to the directory where documents exist."
	echo "        <index_path>  : path to the directory where index exist."
	echo "        <rel_path>    : relative path from <index_path> to beginning of <object_path> "
	echo ""
	echo "	  $0 -f <namazu.conf> <pattern> [<out_file>]"
	echo "	      -f: find <pattern> from index according to <namazu.conf>"
	echo "        <out_file>    : output file in html"
	echo ""
}

MK_NMZ=/usr/bin/mknmz
SCH_NMZ=/usr/bin/namazu
NMZ_LOG=/tmp/bn_namazu.log
NKF_CMD=/usr/bin/nkf

date		> $NMZ_LOG
echo "$# $*"	>> $NMZ_LOG
pwd		>> $NMZ_LOG
echo ""		>> $NMZ_LOG

if [ $# -lt 3 ]; then
	do_usage
	exit -1
fi

case $1 in
'-d'|'-i')
	if [ $# = 4 ]; then
		rel_path=$4/
	fi

	object_path=$2
	index_path=$3

	if  [ $1 = '-i' ]; then
	 if  [ ! -d $index_path ]; then
		mkdir -p $index_path
	 fi
	fi
	cd $index_path
	$MK_NMZ "$object_path/" "${rel_path}$object_path"
	state='i'
	;;
'-f')
	if [ $# = 4 ]; then
		out_file=$4
	else
		out_file=`tty`
	fi
	namazu_conf=$2
#	pattern=`echo $3 | $NKF_CMD -j`
	pattern=$3

echo "pattern = $pattern"
#	$SCH_NMZ -h -o $out_file -f $namazu_conf "$pattern"
	$SCH_NMZ -h -f $namazu_conf $pattern \
	| $NKF_CMD -e | sed '/^<META/s/charset=ISO-2022-JP/charset=EUC-JP/' \
	> $out_file
	state='f'
	;;
esac


