#!/bin/sh
# -------------------------------------------------------------------
# Environmental variables:
#
# JETS3T_HOME  Points to the home directory of a JetS3t distribution.
#
# JAVA_HOME    The home directory of the Java Runtime Environment or 
#              Java Development Kit to use. 
# -------------------------------------------------------------------

: ${JETS3T_HOME:="/usr/share/jets3t"}
: ${JAVA_HOME:-""}

# Check the JETS3T_HOME directory

if [ -z "$JETS3T_HOME" ]; then
  # Try to find the home directory, assuming this script is in $JETS3T_HOME/bin
  SCPT_DIR=`dirname $0`
  if [ "$SCPT_DIR" = "." ]; then
    JETS3T_HOME=..
  else    
    JETS3T_HOME=`dirname $SCPT_DIR`
  fi
fi

if [ ! -d $JETS3T_HOME/jars ]; then
  echo "Please set the environment variable JETS3T_HOME"
  exit 1
fi

# Check the JAVA_HOME directory
if [ -z "$JAVA_HOME" ]; then
  # Test whether the 'java' program is available in the system path.
  java -version 2> /dev/null
  if [ $? -gt 0 ]; then
    export JAVA_HOME="/usr/lib/jvm/java"
  else
    EXEC=java
  fi
else
  EXEC=$JAVA_HOME/bin/java
fi

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

# Include configurations directory in classpath
CP=$CLASSPATH:$JETS3T_HOME/configs

# Include resources directory in classpath
CP=$CP:$JETS3T_HOME/resources

# System libraries path
: ${JAVA_LIBS:="/usr/share/java"}

# Include libraries in classpath
CP=$CP:$JAVA_LIBS/jets3t/jets3t.jar
CP=$CP:$JAVA_LIBS/commons-logging.jar
CP=$CP:$JAVA_LIBS/commons-codec.jar
CP=$CP:$JAVA_LIBS/httpcomponents/httpclient.jar
CP=$CP:$JAVA_LIBS/httpcomponents/httpcore.jar
CP=$CP:$JAVA_LIBS/log4j12-1.2.17.jar
CP=$CP:$JAVA_LIBS/bcprov.jar
CP=$CP:$JAVA_LIBS/java-xmlbuilder.jar
CP=$CP:$JAVA_LIBS/base64.jar
CP=$CP:$JAVA_LIBS/BareBonesBrowserLaunch.jar
CP=$CP:$JAVA_LIBS/jackson/jackson-core-asl.jar
CP=$CP:$JAVA_LIBS/jackson/jackson-mapper-asl.jar
CP=$CP:$JAVA_LIBS/mx4j/mx4j.jar
CP=$CP:$JAVA_LIBS/javamail/mail.jar
CP=$CP:$JAVA_LIBS/avalon-framework-api.jar
CP=$CP:$JAVA_LIBS/avalon-logkit.jar
CP=$CP:$JAVA_LIBS/glassfish-servlet-api.jar
CP=$CP:$JAVA_LIBS/jms.jar

# Convert classpath for cygwin bash
case "`uname -s`" in
    CYGWIN*)
        CYGWIN_CP=""
        for cp_path in $(echo $CP | tr ':' '\n'); do
            CWIN_PATH=$(cygpath -w -a "$cp_path")
            CYGWIN_CP="$CYGWIN_CP;$CWIN_PATH"
        done
        CP=$CYGWIN_CP
esac

# OutOfMemory errors? Increase the memory available by changing -Xmx256M
MAXMEM=-Xmx256M

# Run application with special arguments on OS X (Darwin) systems.
if [ `uname -s` = "Darwin" ]
then
  "$EXEC" $MAXMEM -classpath "$CP" -Xdock:name="JetS3t Cockpit-Lite" org.jets3t.apps.cockpitlite.CockpitLite
 else
  "$EXEC" $MAXMEM -classpath "$CP" org.jets3t.apps.cockpitlite.CockpitLite
fi
