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

## usage: 
## lucegene-search.sh -lib go -p dbs/lucegene/go.properties

ERROR=0
defaultlib=libs

# search is not as mem piggy as index - we thought :( see common wild search of fbgn acode
## this is too low for fbgn-all# maxmem="-Xmx20M"
maxmem="-Xmx90M"
#maxmem="-Xmx200M"

jars="lucegene.jar lucene.jar readseq.jar"
#  pdfbox.jar log4j.jar - for index only?



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 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="$lib/lucegene.jar:$lib/lucene.jar:$lib/readseq.jar"
## add $PROP_ROOT/classes to hide all the .class files?
JAVA_CP=".:$PROP_ROOT/classes:$PROP_ROOT"
for jar in $jars  
{
  JAVA_CP="${JAVA_CP}:$lib/$jar"
}
JAVA_FLAGS="-Xms10M $maxmem"
JAVA_APP="org.eugenes.index.LuceneSearch"

}

usage()
{
  cat  1>&2 <<HERE
Usage: $0 	
Usage: lucegene-search.sh -l go -c 'lookup docid:1'
Usage: lucegene-search.sh -c 'help'
Options:
    -command 'search command'   
    -index INDEX_ROOT   lucene indices directory
    -lib   LIB_NAME     lucene library
    -prop  PROP_FILE    search properties
    -debug     debug output
HERE

##    -field FIELD_NAME   search this field
  ERROR=2
  exit $ERROR
}

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

find_MY_HOME
#echo "home=$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=""
#appargs="LUCEGENE_ROOT=$MY_HOME"
didcmd=0; didroot=0; didlib=0; didprop=0; debug=0;
cmd=""

for arg in "$@"
{
  case "$arg" in
    "-"*) getarg="" ;;
  esac
  
  case "$arg" in
      
    "-deb"*) debug=1; appargs="$appargs debug=1" ;;
    "-i"*) getarg="index" ;;
    "-l"*) getarg="lib" ;;
    ##"-f"*) getarg="field" ;;
    "-c"*) getarg="command";;
    "-p"*) getarg="props";;
    "-h"*) usage ;;
    
    *) 
        if [ "$getarg" = "index" ] ; then getarg=""; didroot=1; appargs="$appargs INDEX_ROOT=$arg"; 
      elif [ "$getarg" = "props" ] ; then getarg=""; didprop=1; appargs="$appargs PROP_FILE=$arg"; 
      elif [ "$getarg" = "command" ] ; then  didcmd=1; 
        if [ -z "$cmd" ]; then cmd=$arg ; else cmd="${cmd};$arg"; fi 
      elif [ "$getarg" = "lib" ] ; then getarg=""; didlib=1; appargs="$appargs LIB_NAME=$arg";
        if [ $didprop = 0 ]; then appargs="$appargs PROP_FILE=$PROP_ROOT/${arg}.properties"; fi
      else appargs="$appargs $arg"; 
      fi 
      ;;
      
  esac
}

if [ $didroot = 0 -a $didlib = 0 -a $didcmd = 0 -a $didprop = 0 ]; then usage ; fi

if [ $didroot = 0 ]; then appargs="$appargs INDEX_ROOT=$INDEX_ROOT" ; fi
if [ $didlib = 0 -a $didprop = 0 ]; then appargs="$appargs LIB_NAME=$defaultlib" ; fi

if [ ! -z "$cmd" ]; then cmd="command=$cmd"; fi

set_classpath

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

java $JAVA_FLAGS -cp $JAVA_CP $JAVA_APP $appargs "$cmd"

ERROR=$?
exit $ERROR

