#!/bin/sh
##
## Setup a supervised Hyper Estraier estmaster service for Chimera Search
## Copyright (c) 2008-2011 SATOH Fumiyasu @ OSS Technology, Inc.
##               <http://www.osstech.co.jp/>
##
## License: GNU General Public License version 2 or later
## Date: 2011-06-06, since 2008-02-08
##

set -u
umask 0027

pdie()
{
  echo "$0: ERROR: $1" 1>&2
  exit 1
}

## Options
## ======================================================================

est_master_cmd="/opt/osstech/bin/estmaster"
est_master_svconf_cmd="/opt/osstech/bin/estmaster-svconf"

type "$est_master_cmd" >/dev/null 2>&1 || pdie "Command not found: $est_master_cmd"
type "$est_master_svconf_cmd" >/dev/null 2>&1 || pdie "Command not found: $est_master_svconf_cmd"

## ======================================================================

"$est_master_svconf_cmd" ${1+"$@"} || exit 1

sv_dir="$1"; shift
est_rootdir="$1"; shift

## Adjust environment for Chimera Search
## ----------------------------------------------------------------------

## Increase OPENFILELIMIT to 2048 because Chimera Search uses 20 indexes
## by default and estmaster uses about 85 file descriptors per index.
echo "2048" >"$sv_dir/env/OPENFILELIMIT"

## Adjust attrindex for Chimera Search
## ----------------------------------------------------------------------

perl -pi.chimera.orig \
  -e 's/^(scalepred:\s*).*$/${1}4/;' \
  -e 's/^(uireplace|uiextattr):/#$1:/;' \
  -e '$_="" if /^(##* *attribute indexes.*|attrindex:)/;' \
  "$est_rootdir/_conf" \
  || exit 1

cat <<EOT_ATTRINDEX >>"$est_rootdir/_conf" || exit 1
# attribute indexes (attribute name and data type)
attrindex: @uri{{!}}str
attrindex: @name{{!}}str
attrindex: @title{{!}}str
attrindex: @type{{!}}str
attrindex: @owner{{!}}str
attrindex: @author{{!}}str
attrindex: @size{{!}}num
attrindex: @mdate{{!}}seq
#attrindex: @cdate{{!}}seq
EOT_ATTRINDEX

