#!/bin/sh

#
#   Projet       : DilibPro
#   Module       : Index
#   Commande     : IndexBuildUsual
#   Author       : Ducloy
#   Date         : 15 July 1994
#

#. $DILIB_CONFIG

USAGE="usage: IndexBuildUsual -h hfdRadical [-k keyWordPath ] [-i indexCode] [-w workSpace] [-s maxTermToSort] -r [tableReject]"
#
#    option analysis. founding K_OPTION (-d option)  -- obsolete
#                              K_OPTION (-k option)
#                              H_OPTION (-h option)
#                          and I_OPTION (-i option)
#

set -e

#
#        implicit values
#


K_OPTION=doc/kw/e
I_OPTION=kw
W_OPTION=$DILIB_WORKSPACE
S_OPTION=500
r_OPTION=""
R_OPTION=""

while getopts b:d:h:i:k:r:s:w:R: c
do
  case $c in
   b) H_OPTION=$OPTARG;;
   h) H_OPTION=$OPTARG;;
   k) K_OPTION=$OPTARG;;
   d) K_OPTION=$OPTARG;;
   i) I_OPTION=$OPTARG;;
   r) r_OPTION="$r_OPTION -t $OPTARG";;
   R) R_OPTION="$R_OPTION -t $OPTARG";;
   s) S_OPTION=$OPTARG;;
   w) W_OPTION=$OPTARG;;
   \?) echo -$c $OPTARG unknowed option
       echo $USAGE
       exit 2;;
  esac
done

BASE_PATH=`DamGetPath -Arn $H_OPTION`

echo $BASE_PATH

echo "---- begin building index <"$I_OPTION"> of base "$H_OPTION

rm -rf $H_OPTION.$I_OPTION.i.*

HfdCat "$BASE_PATH.bib.hfd"             \
       | SgmlSelect -s $K_OPTION -p @S -p @1        \
       | (
          if test "$r_OPTION"
            then
                StrSearchKey -x $r_OPTION
            else
                cat
         fi
          )   |  (
         if test "$R_OPTION"
            then
                StrSearchKey -r $R_OPTION
            else
                cat
         fi
          ) |  sort -u   -T $W_OPTION       \
       | IndexFastBuildRec                          \
       | HfdBuild -h "$BASE_PATH.$I_OPTION.i"

IndexBuildHid -h "$BASE_PATH.$I_OPTION.i" -e kw -p $K_OPTION

if test $S_OPTION != 0
then
HfdCat $BASE_PATH.$I_OPTION.i.hfd        |
       SgmlFast -c l                     |
       SgmlSelect -g idx/f# -p @g1 -p @2 |
       sort -rn      -T $W_OPTION        |
       SxmlCut 1                         |
       head -$S_OPTION > $BASE_PATH.$I_OPTION.i.sort
fi

touch $BASE_PATH.$I_OPTION.i.hcs

echo "---- end   building index " $I_OPTION
SgmlSelect -s hfd/nrec# -p "---- ( @s1 records in index file) " < $BASE_PATH.$I_OPTION.i.hcs

#

       