if [ "$JAVA_HOME" = "" ]; then
	JAVA_HOME="/usr/java/jdk1.8.0_111"
fi

if [ "$LBS_HOME" = "" ]; then
	LBS_HOME="/opt"
fi

DATE_STRING="$(date +'%Y-%m-%d')"
JAVACMD="$JAVA_HOME/bin/java"
CLASS_HOME=""
SERVICE_CLASS="com.umltech.syn.core.SynServiceMain"
 
export LC_ALL=zh_CN

oldCP=$CLASSPATH

unset CLASSPATH
for i in ${LBS_HOME}/umltech-syn-service/lib/*.jar ; do
  if [ "$CLASSPATH" != "" ]; then
    CLASSPATH=${CLASSPATH}:$i
  else
    CLASSPATH=$i
  fi
done
LOGBACK=${LBS_HOME}/umltech-syn-service/logback.xml

if [ "$CLASS_HOME" != "" ]; then
  for i in ${CLASS_HOME}/* ; do
    if [ "$CLASSPATH" != "" ]; then
      CLASSPATH=${CLASSPATH}:$i
    else

      CLASSPATH=$i
    fi
  done
fi

if [ "$oldCP" != "" ]; then
    CLASSPATH=${CLASSPATH}:${oldCP}
fi

JVM_LOG_DIR="${LBS_HOME}/synservice/logs/"
JVM_LOG="${JVM_LOG_DIR}jvm.${DATE_STRING}.log"

if [ -d "$JVM_LOG_DIR" ] 
then 
    echo "JVM_LOG_DIR=${JVM_LOG_DIR}"    
else
    mkdir $JVM_LOG_DIR
fi

if [ -f "$JVM_LOG" ] 
then
   echo "JVM_LOG=${JVM_LOG}"
else    
   touch $JVM_LOG
fi

echo ${JAVACMD} ${LOGBACK}  -XX:+PrintGC -XX:+PrintGCDateStamps -Xloggc:$JVM_LOG  -cp $CLASSPATH ${SERVICE_CLASS} -start

case "$1" in
	start)
		if [ `whoami` = "root" ]; then
			${JAVACMD} -Dlogback.configurationFile=$LOGBACK -Xmx1512m -Xms1512m -Xmn784m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSMaxAbortablePrecleanTime=50 -XX:+CMSClassUnloadingEnabled -XX:+PrintGC -XX:+PrintGCDateStamps -Xloggc:$JVM_LOG -cp $CLASSPATH ${SERVICE_CLASS} -d start & 
		fi
		;;
	stop)
            P_ID=`jps | grep SynServiceMain |cut -d ' ' -f 1`
	if [ -z $P_ID ];then
            	echo "SynService not running!"                 
    	else
            	kill -9 $P_ID 
    	fi
            ;;
   restart)
            ${0} stop 
            sleep 1
	${0} start
            ;;	
	*)
		echo "Usage: $0 {start}"
esac

exit 0

