#!/bin/sh
# lucegene-index.sh

## usage: 
## lucegene-index.sh -p dbs/lucegene/go.properties
## see also ant index.xml, more complex, less programable
ERROR=0
## set higher if mem available for this piggy indexer
maxmem="-Xmx250M"
jars="lucegene.jar lucene.jar readseq.jar pdfbox.jar log4j.jar"

find_MY_HOME()
{
  PRG="$0"
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG=`dirname "$PRG"`/"$link"
    fi
  done
  MY_HOME=`dirname "$PRG"`/..
  MY_HOME=`cd "$MY_HOME" && pwd`
}

set_classpath() 
{
  if [ -f "$LUCEGENE_LIB/lucegene.jar" ] ; then lib="$LUCEGENE_LIB/" ;
elif [ -f "$MY_HOME/lib/lucegene.jar" ] ; then lib="$MY_HOME/lib/" ;
elif [ -f "$MY_HOME/lib/java/lucegene.jar" ] ; then lib="$MY_HOME/lib/java/" ;
elif [ -f "$MY_HOME/WEB-INF/lib/lucegene.jar" ] ; then lib="$MY_HOME/WEB-INF/lib" ;
elif [ -f "$MY_HOME/common/java/lib/lucegene.jar" ] ; then lib="$MY_HOME/common/java/lib" ;
else
  lib="$ARGOS_ROOT/common/java/lib"
  if [ ! -f "$lib/lucegene.jar" ] ; then  
    echo "The ARGOS_ROOT environment variable is not defined"
    echo "It should point to directory with common/java/lib/lucegene.jar"
    ERROR=6 ; exit $ERROR
  fi
fi

if [ ! -d "$INDEX_ROOT" ] ; then  
  echo "The INDEX_ROOT path is not good at $INDEX_ROOT"
  echo "It should point to directory with lucene indices"
 # ERROR=5 ; exit $ERROR
fi

if [ ! -f "$PROP_ROOT/lucegene.properties" ] ; then  
  echo "The PROP_ROOT path is not good at $PROP_ROOT"
  echo "It should point to directory with lucegene.properties"
 # ERROR=5 ; exit $ERROR
fi

# JAVA_CP=".:$PROP_ROOT:$lib/lucegene.jar:$lib/lucene.jar:$lib/readseq.jar"
JAVA_CP=".:$PROP_ROOT/classes:$PROP_ROOT"
## add $PROP_ROOT/classes to hide all the .class files?
for jar in $jars  
{
  JAVA_CP="${JAVA_CP}:$lib/$jar"
}

JAVA_FLAGS="-Xms90M $maxmem"
JAVA_APP="org.eugenes.index.LuceneBaseIndexer"
# ^^ this will change to INDEX_CLASS from properties file

}

compile_indexers()
{
  # local jsrc jclass
  JAVA_SRC=`find $PROP_ROOT -name '*.java'`
  mkdir "$PROP_ROOT/classes" 1>/dev/null 2>&1
  tester=`which test`
  for jsrc in $JAVA_SRC
  {
    #jclass=`echo $jsrc | sed -e's/.java/.class/' -e's,$PROP_ROOT,$PROP_ROOT/classes,'`
    jclass=`echo $jsrc | perl -pe's,([^/.]+).java$,classes/$1.class,'`
    if [ ! -f $jclass ] ; then
      echo "javac $jsrc"
      javac -classpath $JAVA_CP -d "$PROP_ROOT/classes" -sourcepath $PROP_ROOT $jsrc
    elif $tester $jsrc -nt $jclass ; then
      echo "javac $jsrc"
      javac -classpath $JAVA_CP -d "$PROP_ROOT/classes" -sourcepath $PROP_ROOT $jsrc
    fi
  }
}


usage()
{
  cat  1>&2 <<HERE
Usage: lucegene-index.sh -p dbs/lucegene/go.properties
Options:
    -data  DATA_ROOT    lucene data directory
    -index INDEX_ROOT   lucene indices directory
    -lib   LIB_NAME     lucene library
    -prop  PROP_FILE    index properties
    -debug     debug output
    -test      show files to be indexed (dont index)
 Use '-run -debug' to output values/field/record (with tokenizer debug support)
HERE

  ERROR=2
  exit $ERROR
}

#------------------------------------

find_MY_HOME

## suck in env values ??
if [ -f "$MY_HOME/bin/argos-env" ] ; then  
  . `$MY_HOME/bin/argos-env sh gmod.conf`
fi

INDEX_ROOT="$MY_HOME/indices/lucene"
PROP_ROOT="$MY_HOME/dbs/lucegene"
if [ ! -f "$PROP_ROOT/lucegene.properties" ] ; then  
  PROP_ROOT="$MY_HOME/conf"
fi

##inargs=$@
appargs="LUCEGENE_ROOT=$MY_HOME"
dotest=0; dorun=0; didindex=0; diddata=0; didprop=0; didlib=0; debug=0;

## make -test default w/o a -run option

# args="$args PROP_FILE=${PROP_ROOT}/${LIB_NAME}.properties"
# args="$args INDEX_ROOT=${INDEX_ROOT}"
# args="$args INDEX_PATH=${INDEX_ROOT}/${LIB_NAME}"
# args="$args DATA_ROOT=${DATA_ROOT}"

for arg in "$@"
{
  case "$arg" in
      
    "-deb"*) debug=1; appargs="$appargs debug=1" ;;
    "-r"*) dorun=1; dotest=0 ;;
    "-t"*) dorun=0; dotest=1; appargs="$appargs noindex=1"  ;;
    "-d"*) getarg="data" ;;
    "-i"*) getarg="index" ;;
    "-l"*) getarg="lib" ;; #? useless, need props file - or make from PROP_ROOT
    "-p"*) getarg="props" ;;
    "-h"*) usage ;;

    *) 
        if [ "$getarg" = "index" ] ; then getarg=""; didindex=1; appargs="$appargs INDEX_PATH=$arg"; 
      elif [ "$getarg" = "props" ] ; then getarg=""; didprop=1; appargs="$appargs PROP_FILE=$arg"; 
      elif [ "$getarg" = "data" ] ; then getarg=""; diddata=1; appargs="$appargs DATA_ROOT=$arg"; 
      elif [ "$getarg" = "lib" ] ; then getarg=""; didlib=1;
        if [ $didindex = 0 ]; then appargs="$appargs INDEX_PATH=$INDEX_ROOT/$arg"; fi
        if [ $didprop = 0 ]; then appargs="$appargs PROP_FILE=$PROP_ROOT/${arg}.properties"; fi
      else appargs="$appargs LIB_NAME=$arg"; 
      fi 
      ;;
      
  esac
}


if [ $didindex = 0 -a $didlib = 0 -a $diddata = 0 -a $didprop = 0 ]; then usage ; fi

set_classpath

compile_indexers

# make test default
if [ $dorun = 0 -a $dotest = 0 ]; then appargs="$appargs noindex=1" ; fi

if [ $debug = 1 ]; then echo java $JAVA_FLAGS -cp $JAVA_CP $JAVA_APP $appargs ; fi

java $JAVA_FLAGS -cp $JAVA_CP $JAVA_APP $appargs 

ERROR=$?
exit $ERROR

#------------

# MY_HOME="/bio/biodb/flybase" ; export MY_HOME
# PROP_ROOT="${MY_HOME}/dbs/lucegene"; export PROP_ROOT
# JAVA_CP=".:${PROP_ROOT}:${MY_HOME}/common/java/lib/lucegene.jar:${MY_HOME}/common/java/lib/lucene.jar:${MY_HOME}/common/java/lib/readseq.jar"
