#! /bin/bash

# Please set the APP_NAME variable to the name of the application before calling.
# Also, the MINMEM_VALUE variable may be set to specify the starting memory
#   allocation of the VM.

#to make sure that environment variables get exported even when a service is started using "service" command.
#Ref bug #922507
source /etc/profile

if [ "${VMWARE_JAVA_HOME}x" != "x" ] ; then
  JAVA_HOME=${VMWARE_JAVA_HOME}
else
  echo VMWARE_JAVA_HOME is not set, cannot start $APP_NAME
  echo
  exit 1
fi

# Service script absolute path
JVM_PATH="${JAVA_HOME}/bin"

if [ -e "$VMWARE_CLOUDVM_RAM_SIZE" ] ; then
# Wrapper Conf files
   # Get JVM Option string directly from cloudvm-ram-size
   # MaxHeap, MaxPerm, StackSize are set from cloudvm-ram-size
   JVM_OPTIONS=`$VMWARE_CLOUDVM_RAM_SIZE -J $APP_NAME`
else
  echo VMWARE_CLOUDVM_RAM_SIZE is not set, cannot start $APP_NAME
  echo
  exit 1
fi

if [ "${JVM_OPTIONS}" == "" ] ; then
   echo "Memory related JVM_OPTIONS is not set"
   exit 1
fi

echo Starting $APP_NAME with JVM options $JVM_OPTIONS

# Setup Hardlink for java to app.name
ln -f $JVM_PATH/java $JVM_PATH/$APP_NAME

exec $JVM_PATH/$APP_NAME ${JVM_OPTIONS} $@
