#! /bin/sh
#
#	This script indexes wims server resources (modules, sheets).
#	It should be run each time a module is added or modified.
#

# base directory.
test=`dirname $0`
if [ -z "$test" ]; then WIMS_HOME=..; else WIMS_HOME=$test/..; fi
cd $WIMS_HOME
WIMS_HOME=`pwd`
export mlist slist dicsort_separator dicsort_grouping
export w_translator_unknown w_dictionary w_translator_switch translator_input
LANG=C

. bin/dircptest

BASES=$WIMS_HOME/public_html/bases
MODULES=$WIMS_HOME/public_html/modules
translator=$WIMS_HOME/public_html/bin/translator
dicsort=$WIMS_HOME/bin/dicsort
SLIB=$WIMS_HOME/public_html/scripts/slib
SWAC=$WIMS_HOME/public_html/scripts/data/swac
DATA=$WIMS_HOME/public_html/scripts/data
ICONES=$WIMS_HOME/public_html/gifs/themes
MODEL=$WIMS_HOME/public_html/scripts/model

# MAX_LINELEN
linelen=15000

cd $MODULES
mlist=`find [EHUGRL]* tool data contrib -name INDEX 2>/dev/null\
  | sed 's!/INDEX$!!g' | sort`
mlist2=`find local com help -follow -name INDEX 2>/dev/null\
  | sed 's!/INDEX$!!g' | sort`
mlist="$mlist
$mlist2"

cd $BASES/sheet
slist="`find . -name '*.def' | sed 's!^./!!g;s!.def$!!g' | sort`"
rm -f index/*
cd $BASES
rm -fR site2
$dircp site site2
cd site2
rm -f [A-Z].?? lists/[A-Z].??
mkdir -p lists

cd $WIMS_HOME
bin/modind || exit

cd $BASES/site2 || exit
rm -f mkindex.log
dicsort_separator=':'
dicsort_grouping=' '
for f in [A-Z].??
do
  $dicsort $f >>mkindex.log
  mv $f.sorted $f
done
cd $BASES/site2

dicsort_grouping=
for f in title description addr serial language author version weight.??
do
  $dicsort $f >>mkindex.log
  mv $f.sorted $f
done
n=`grep -c . addr`
rm -f $WIMS_HOME/log/modupdate
echo Indexed $n modules.

w_translator_switch="leavelines file"
w_translator_unknown=leave
w_dictionary=serial
translator_input=popular
$translator >popular.1
$dicsort popular.1 >>mkindex.log
grep '^[0-9]' popular.1.sorted >popular.1

awk -F: '{print $1}' title | uniq >lists/total

cd lists
cats=`ls [A-Z].??`
cd ..

for t in $cats
do
 w_translator_unknown=leave
 w_dictionary=pop/dic
 w_translator_switch="leaveline file"
 translator_input=lists/$t
 $translator | sort -n -r -k2 2>/dev/null |\
   awk 'BEGIN {a=0}; {a+=length()+1};
	a >= '$linelen' {exit};
	{print}' >pop/$t
done

# Robot lists
langs=`ls A.* | sed 's/^A\.//g'`
w_translator_switch="leaveline file"
w_dictionary=popular
w_translator_unknown=leave
translator_input=lists/robot.phtml
$translator | sort -nr >lists/robot.sorted

for l in $langs
do
 awk -F, '$2=="'$l'" {print "!href module="$3" "$4"\n, "$5" <br>"}' lists/robot.sorted >lists/robot-$l.phtml
done
rm -f lists/robot.*

cd $BASES
if [ -d site2 ]; then
 mv site site.old
 mv site2 site
 [ -d site ] && rm -fR site.old
fi

# OEF exercises
if [ "$1" = "withoef" ]; then
  echo Listing OEF exercices by order of time...
  TMP=$WIMS_HOME/tmp/oefs
  tmp1=${TMP}1; tmp2=${TMP}2; tmp3=${TMP}3

  mkdir -p $BASES/site/oef
  cd $BASES/site/lists
  langs=`ls O.* | sed 's/^O\.//g'`
  cd $MODULES
  newmod=`awk '$2=="added" {print $4}' Changelog | head -20 | tr -d '()'`
  for l in $langs
  do
   oefmodules=`egrep '^[A-Z]|^contrib' $BASES/site/lists/O.$l`
   rm -f $tmp1 $tmp2 $tmp3 2>/dev/null
   for m in $oefmodules
   do
    mt=`echo $m | sed 's/.[a-z][a-z]$//'`
    test=`echo "$newmod" | grep $mt`
    if [ -z "$test" ]; then out=$tmp1; else out=$tmp2; fi
    perl -e 'for (@ARGV) {@S=stat($_); print "$_ $S[9]\n"}'\
      $m/src/*.oef >> $out
   done
   for tmp in $tmp1 $tmp2; do
     if [ -s $tmp ]; then
      sort -nr -k2 $tmp | cut -d' ' -f1 >> $tmp3
     fi
   done
   if [ -s $tmp3 ]; then
    sed 's!/src/!,!g;s/\.oef$//g' $tmp3 > $BASES/site/oef/new.$l
    n=`grep -c . $tmp3`
    echo "$l: $n OEF sorted."
   fi
  done
  rm -f $tmp1 $tmp2 $tmp3 2>/dev/null
fi

# sheets
dicsort_grouping=' '
cd $BASES/sheet/index
for f in ??
do
  $dicsort $f >>mkindex.log
  mv $f.sorted $f
  cnt=`grep -c . list.$f`
  echo $cnt >count.$f
  $dicsort addr.$f >>mkindex.log
  mv addr.$f.sorted addr.$f
  $dicsort serial.$f >>mkindex.log
  mv serial.$f.sorted serial.$f
  echo Indexed $cnt $f sheets.
done

dicsort_grouping=
for f in title.* description.*
do
  $dicsort $f >>mkindex.log
  mv $f.sorted $f
done

#slib index
cd $SLIB
./mkindex >>mkindex.log 

#shtooka index
cd $SWAC
rm -f index.log 
./mkindex >>index.log 

$BASES/doc/.build-index
$DATA/qpuzzle/makepieces
$DATA/spuzzle/makepieces

cd $ICONES 
./mkindex > index.log


#model list
for i in fr en cn ca it ; do rm -f $MODEL/$i/list ; done 


