#!/bin/sh
PORT="@profiles.server.port@"
BASEDIR=`dirname $0`/..
BASEDIR=`(cd "$BASEDIR"; pwd)`
PROJECT_NAME="@project.artifactId@";
MAIN_CLASS="$PROJECT_NAME-@project.version@.jar";
LOG_PATH="@profiles.log.path@/$PROJECT_NAME"
GC_PATH=$LOG_PATH/PROJECT_NAME"-gc.log"
HS_ERR_PATH=$LOG_PATH/PROJECT_NAME"-hs_err.log"
HEAP_DUMP_PATH=$LOG_PATH/PROJECT_NAME"-heap_dump.hprof"
TEMP_PATH=$LOG_PATH/temp/
SUCCESS=0
FAIL=9
if [ ! -n "$PORT" ]; then  
	echo $"Usage: $0 {port}"
	exit $FAIL
fi
if [ ! -d $LOG_PATH ]; 
then     
    mkdir -p $LOG_PATH; 
fi
if [ ! -d $TEMP_PATH ]; 
then     
    mkdir -p $TEMP_PATH; 
fi
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`
	echo  "Error: JAVA_HOME is $JAVACMD"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "We cannot execute $JAVACMD"
  exit $ERR_NO_JAVA
fi

if [ -e "$BASEDIR" ]
then
  JAVA_OPTS="-Xms512M -Xmx1024M -Xss256K -XX:+UseAdaptiveSizePolicy -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:GCTimeRatio=39 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:$GC_PATH -XX:+HeapDumpOnOutOfMemoryError -XX:ErrorFile=$HS_ERR_PATH -XX:HeapDumpPath=$HEAP_DUMP_PATH"
fi

CLASSPATH=$CLASSPATH_PREFIX:
EXTRA_JVM_ARGUMENTS=""

cd "$BASEDIR/boot";

echo "starting application $PROJECT_NAME......"
exec "$JAVACMD" $JAVA_OPTS \
				$EXTRA_JVM_ARGUMENTS \
				-Dapp.name="$PROJECT_NAME" \
				-Dapp.port="$PORT" \
				-Dbasedir="$BASEDIR" \
				-Djava.io.tmpdir=$TEMP_PATH \
				-Dloader.path="file://$BASEDIR/conf,file://$BASEDIR/lib" \
				-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=41086 \
				-jar $MAIN_CLASS \
				> /dev/null &
				
for i in {1..60}
do
	jcpid=`ps -ef | grep -v "grep" | grep "$MAIN_CLASS" | grep "app.port=$PORT" | sed -n '1P' | awk '{print $2}'`
	if [ $jcpid ]; then
		echo "The $PROJECT_NAME start finished, PID is $jcpid"
		exit $SUCCESS
	else
		echo "starting the application .. $i"
		sleep 1
	fi
done
echo "$PROJECT_NAME start failure!"
