#!/bin/sh
# ====
#     Copyright (C) 2011-2012 OGIS-RI Co.,Ltd. All rights reserved.
#     The software in this package is published under the terms of the CPAL v1.0
#     license, a copy of which has been included with this distribution in the
#     LICENSE.txt file.
# ====
#

BASEDIR=`dirname $0`/..
BASEDIR=`(cd "$BASEDIR"; pwd)`



# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
             echo "Using Java version: $JAVA_VERSION"
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
           fi
           ;;
esac

if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=`java-config --jre-home`
  fi
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

# If a specific java binary isn't specified search for the standard 'java' binary
if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=`which java`
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

if [ -z "$REPO" ]
then
  REPO="$BASEDIR"/lib
fi

CLASSPATH=$CLASSPATH_PREFIX:"$BASEDIR"/conf:"$REPO"/spring-core-3.1.0.RELEASE.jar:"$REPO"/spring-asm-3.1.0.RELEASE.jar:"$REPO"/spring-context-3.1.0.RELEASE.jar:"$REPO"/spring-expression-3.1.0.RELEASE.jar:"$REPO"/spring-beans-3.1.0.RELEASE.jar:"$REPO"/spring-aop-3.1.0.RELEASE.jar:"$REPO"/aopalliance-1.0.jar:"$REPO"/spring-context-support-3.1.0.RELEASE.jar:"$REPO"/spring-tx-3.1.0.RELEASE.jar:"$REPO"/spring-orm-3.1.0.RELEASE.jar:"$REPO"/spring-jdbc-3.1.0.RELEASE.jar:"$REPO"/hibernate-core-4.0.0.Final.jar:"$REPO"/commons-collections-3.2.1.jar:"$REPO"/antlr-2.7.7.jar:"$REPO"/jboss-transaction-api_1.1_spec-1.0.0.Final.jar:"$REPO"/dom4j-1.6.1.jar:"$REPO"/xml-apis-1.0.b2.jar:"$REPO"/jboss-logging-3.1.0.CR2.jar:"$REPO"/classmate-0.5.4.jar:"$REPO"/jandex-1.0.3.Final.jar:"$REPO"/hibernate-commons-annotations-4.0.1.Final.jar:"$REPO"/javassist-3.12.1.GA.jar:"$REPO"/hibernate-entitymanager-4.0.0.Final.jar:"$REPO"/hibernate-validator-4.2.0.Final.jar:"$REPO"/hibernate-jpa-2.0-api-1.0.1.Final.jar:"$REPO"/validation-api-1.0.0.GA.jar:"$REPO"/hsqldb-2.2.6.jar:"$REPO"/aspectjweaver-1.6.12.jar:"$REPO"/javassist-3.15.0-GA.jar:"$REPO"/cglib-2.2.2.jar:"$REPO"/asm-3.3.1.jar:"$REPO"/commons-lang3-3.1.jar:"$REPO"/commons-io-2.0.1.jar:"$REPO"/commons-dbcp-1.4.jar:"$REPO"/commons-pool-1.5.4.jar:"$REPO"/poi-3.7.jar:"$REPO"/httpclient-4.1.2.jar:"$REPO"/httpcore-4.1.2.jar:"$REPO"/commons-codec-1.4.jar:"$REPO"/jcl-over-slf4j-1.6.4.jar:"$REPO"/slf4j-api-1.6.4.jar:"$REPO"/slf4j-log4j12-1.6.4.jar:"$REPO"/log4j-1.2.16.jar:"$REPO"/policytool-1.0.0.jar
EXTRA_JVM_ARGUMENTS="-Xms256m -Xmx512m"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  [ -n "$HOME" ] && HOME=`cygpath --path --windows "$HOME"`
  [ -n "$BASEDIR" ] && BASEDIR=`cygpath --path --windows "$BASEDIR"`
  [ -n "$REPO" ] && REPO=`cygpath --path --windows "$REPO"`
fi

exec "$JAVACMD" $JAVA_OPTS \
  $EXTRA_JVM_ARGUMENTS \
  -classpath "$CLASSPATH" \
  -Dapp.name="policytool" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dbasedir="$BASEDIR" \
  jp.co.ogis_ri.citk.policytool.AppLauncher \
  "$@"
