#!/bin/bash
# 
# The Taming Text command script
#
# Environment Variables
#
#   TT_JAVA_HOME   The java implementation to use.  Overrides JAVA_HOME.
#
#   TT_HEAPSIZE    The maximum amount of heap to use, in MB. 
#                      Default is 1000.
#
#   TT_OPTS        Extra Java runtime options.
#
#   TT_CONF_DIR    The location of the program short-name to class name
#                      mappings and the default properties files
#                      defaults to "$TT_HOME/conf"
#

#
#/**
# * Licensed to the Apache Software Foundation (ASF) under one or more
# * contributor license agreements.  See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The ASF licenses this file to You under the Apache License, Version 2.0
# * (the "License"); you may not use this file except in compliance with
# * the License.  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

# resolve links - $0 may be a softlink
THIS="$0"
while [ -h "$THIS" ]; do
  ls=`ls -ld "$THIS"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    THIS="$link"
  else
    THIS=`dirname "$THIS"`/"$link"
  fi
done

# some directories
THIS_DIR=`dirname "$THIS"`
TT_HOME=`cd "$THIS_DIR/.." ; pwd`

# some Java parameters
if [ "$TT_JAVA_HOME" != "" ]; then
  #echo "run java in $TT_JAVA_HOME"
  JAVA_HOME=$TT_JAVA_HOME
fi
  
if [ "$JAVA_HOME" = "" ]; then
  echo "Error: JAVA_HOME is not set."
  exit 1
fi

JAVA=$JAVA_HOME/bin/java
JAVA_HEAP_MAX=-Xmx1000m 

# check envvars which might override default args
if [ "$TT_HEAPSIZE" != "" ]; then
  #echo "run with heapsize $TT_HEAPSIZE"
  JAVA_HEAP_MAX="-Xmx""$TT_HEAPSIZE""m"
  #echo $JAVA_HEAP_MAX
fi

if [ "x$TT_CONF_DIR" = "x" ]; then
  TT_CONF_DIR=$TT_HOME/conf
fi

# CLASSPATH initially contains $TT_CONF_DIR, or defaults to $TT_HOME/conf
CLASSPATH=${CLASSPATH}:$TT_CONF_DIR
CLASSPATH=${CLASSPATH}:$JAVA_HOME/lib/tools.jar

# so that filenames w/ spaces are handled correctly in loops below
IFS=

if [ -d $TT_HOME/lib ] ; then
  # running in release context
  for f in $TT_HOME/lib/taming-text-*.jar
  do
    CLASSPATH=${CLASSPATH}:$f;
  done
 
  # add slf4j jars first 
  for f in $TT_HOME/lib/dependency/*slf4j*.jar
  do
    CLASSPATH=${CLASSPATH}:$f;
  done
  
  for f in $TT_HOME/lib/dependency/*.jar
  do
    CLASSPATH=${CLASSPATH}:$f;
  done
fi

if [ -d $TT_HOME/target/classes ] ; then
  # running in development context
  CLASSPATH=${CLASSPATH}:$TT_HOME/target/classes

  # add slf4j jars fierst
  for f in $TT_HOME/target/dependency/*slf4j*.jar
  do
    CLASSPATH=${CLASSPATH}:$f;
  done

  for f in $TT_HOME/target/dependency/*.jar
  do
    CLASSPATH=${CLASSPATH}:$f;
  done
fi

# cygwin path translation
if $cygwin; then
  CLASSPATH=`cygpath -p -w "$CLASSPATH"`
fi

# restore ordinary behaviour
unset IFS

if [ "x$JAVA_LIBRARY_PATH" != "x" ] ; then
  TT_OPTS="$TT_OPTS -Djava.library.path=$JAVA_LIBRARY_PATH"
fi

if $cygwin; then
  TT_HOME=`cygpath -w "$TT_HOME"`
fi
TT_OPTS="$TT_OPTS -Dmodel.dir=$TT_HOME/opennlp-models -Dwordnet.dir=$TT_HOME/WordNet-3.0"

CLASS=com.tamingtext.util.TamingTextDriver
exec "$JAVA" $JAVA_HEAP_MAX $TT_OPTS -classpath "$CLASSPATH" $CLASS "$@"

