#!/usr/bin/env bash

##############################################################################
##
##  stock-service-deploy start up script for UN*X
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and STOCK_SERVICE_DEPLOY_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

APP_NAME="stock-service-deploy"
APP_BASE_NAME=`basename "$0"`

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
    echo "$*"
}

die ( ) {
    echo
    echo "$*"
    echo
    exit 1
}

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
  CYGWIN* )
    cygwin=true
    ;;
  Darwin* )
    darwin=true
    ;;
  MINGW* )
    msys=true
    ;;
esac

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/.." >&-
APP_HOME="`pwd -P`"
cd "$SAVED" >&-

CLASSPATH=$APP_HOME/lib/stock-deploy-0.1.0.jar:$APP_HOME/lib/stock-service-common-1.1.7.gey-SNAPSHOT.jar:$APP_HOME/lib/stock-service-biz-1.1.7.gey-SNAPSHOT.jar:$APP_HOME/lib/stock-service-api-1.1.7.gey-SNAPSHOT.jar:$APP_HOME/lib/zkclient-0.5.jar:$APP_HOME/lib/spring-boot-starter-web-1.2.2.RELEASE.jar:$APP_HOME/lib/spring-boot-starter-jdbc-1.2.2.RELEASE.jar:$APP_HOME/lib/spring-boot-starter-test-1.2.2.RELEASE.jar:$APP_HOME/lib/aspectjweaver-1.8.9.jar:$APP_HOME/lib/disconf-client-2.6.33.jar:$APP_HOME/lib/mybatis-3.3.1.jar:$APP_HOME/lib/mybatis-spring-1.2.3.jar:$APP_HOME/lib/mysql-connector-java-5.1.9.jar:$APP_HOME/lib/spring-context-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-context-support-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-orm-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-beans-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-core-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-web-4.1.5.RELEASE.jar:$APP_HOME/lib/commons-dbcp-1.4.jar:$APP_HOME/lib/commons-io-2.4.jar:$APP_HOME/lib/commons-collections4-4.1.jar:$APP_HOME/lib/commons-lang3-3.4.jar:$APP_HOME/lib/log4j-over-slf4j-1.7.5.jar:$APP_HOME/lib/slf4j-api-1.7.5.jar:$APP_HOME/lib/cglib-2.2.2.jar:$APP_HOME/lib/guava-18.0.jar:$APP_HOME/lib/wolf-1.2.1-SNAPSHOT.jar:$APP_HOME/lib/dubbo-2.5.3.jar:$APP_HOME/lib/elastic-job-core-1.0.6.jar:$APP_HOME/lib/elastic-job-spring-1.0.6.jar:$APP_HOME/lib/javassist-3.20.0-GA.jar:$APP_HOME/lib/asm-all-3.3.1.jar:$APP_HOME/lib/jboss-logging-3.3.0.Final.jar:$APP_HOME/lib/validation-api-1.1.0.Final.jar:$APP_HOME/lib/resteasy-jaxrs-3.0.7.Final.jar:$APP_HOME/lib/resteasy-client-3.0.7.Final.jar:$APP_HOME/lib/resteasy-jackson-provider-3.0.7.Final.jar:$APP_HOME/lib/resteasy-netty-3.0.7.Final.jar:$APP_HOME/lib/idmaker-service-api-1.1.6-SNAPSHOT.jar:$APP_HOME/lib/dcommons-base-0.0.4-SNAPSHOT.jar:$APP_HOME/lib/log4j-1.2.17.jar:$APP_HOME/lib/zookeeper-3.4.8.jar:$APP_HOME/lib/spring-boot-starter-1.2.6.RELEASE.jar:$APP_HOME/lib/spring-boot-starter-tomcat-1.2.6.RELEASE.jar:$APP_HOME/lib/jackson-databind-2.4.6.jar:$APP_HOME/lib/hibernate-validator-5.1.3.Final.jar:$APP_HOME/lib/spring-webmvc-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-jdbc-4.1.5.RELEASE.jar:$APP_HOME/lib/tomcat-jdbc-8.0.26.jar:$APP_HOME/lib/spring-tx-4.1.5.RELEASE.jar:$APP_HOME/lib/junit-4.12.jar:$APP_HOME/lib/mockito-core-1.10.19.jar:$APP_HOME/lib/hamcrest-core-1.3.jar:$APP_HOME/lib/hamcrest-library-1.3.jar:$APP_HOME/lib/spring-test-4.1.5.RELEASE.jar:$APP_HOME/lib/disconf-core-2.6.33.jar:$APP_HOME/lib/gson-2.3.1.jar:$APP_HOME/lib/commons-lang-2.6.jar:$APP_HOME/lib/reflections-0.9.9-RC1.jar:$APP_HOME/lib/aspectjtools-1.8.6.jar:$APP_HOME/lib/spring-aop-4.1.5.RELEASE.jar:$APP_HOME/lib/spring-expression-4.1.5.RELEASE.jar:$APP_HOME/lib/commons-logging-1.1.2.jar:$APP_HOME/lib/commons-pool-1.6.jar:$APP_HOME/lib/asm-3.3.1.jar:$APP_HOME/lib/fastjson-1.2.6.jar:$APP_HOME/lib/activation-1.1.1.jar:$APP_HOME/lib/antlr-3.4.jar:$APP_HOME/lib/aopalliance-1.0.jar:$APP_HOME/lib/bsh-2.0b1.jar:$APP_HOME/lib/commons-math3-3.6.1.jar:$APP_HOME/lib/druid-1.0.18.jar:$APP_HOME/lib/commons-beanutils-1.8.3.jar:$APP_HOME/lib/commons-cli-1.2.jar:$APP_HOME/lib/commons-codec-1.6.jar:$APP_HOME/lib/commons-collections-3.2.jar:$APP_HOME/lib/xmemcached-1.4.3.jar:$APP_HOME/lib/joda-time-2.9.3.jar:$APP_HOME/lib/json-lib-2.4-jdk15.jar:$APP_HOME/lib/logback-core-1.0.13.jar:$APP_HOME/lib/logback-classic-1.0.13.jar:$APP_HOME/lib/velocity-1.7.jar:$APP_HOME/lib/velocity-tools-2.0.jar:$APP_HOME/lib/jedis-2.8.1.jar:$APP_HOME/lib/httpclient-4.3.6.jar:$APP_HOME/lib/httpasyncclient-4.0.2.jar:$APP_HOME/lib/javax.servlet-api-3.1.0.jar:$APP_HOME/lib/netty-3.2.5.Final.jar:$APP_HOME/lib/lombok-1.16.4.jar:$APP_HOME/lib/quartz-2.2.1.jar:$APP_HOME/lib/curator-framework-2.10.0.jar:$APP_HOME/lib/curator-client-2.10.0.jar:$APP_HOME/lib/curator-recipes-2.10.0.jar:$APP_HOME/lib/curator-test-2.10.0.jar:$APP_HOME/lib/jaxrs-api-3.0.7.Final.jar:$APP_HOME/lib/jboss-annotations-api_1.1_spec-1.0.1.Final.jar:$APP_HOME/lib/jcip-annotations-1.0.jar:$APP_HOME/lib/jackson-core-asl-1.9.12.jar:$APP_HOME/lib/jackson-mapper-asl-1.9.12.jar:$APP_HOME/lib/jackson-jaxrs-1.9.12.jar:$APP_HOME/lib/jackson-xc-1.9.12.jar:$APP_HOME/lib/jline-0.9.94.jar:$APP_HOME/lib/spring-boot-1.2.6.RELEASE.jar:$APP_HOME/lib/spring-boot-autoconfigure-1.2.6.RELEASE.jar:$APP_HOME/lib/spring-boot-starter-logging-1.2.6.RELEASE.jar:$APP_HOME/lib/snakeyaml-1.14.jar:$APP_HOME/lib/tomcat-embed-core-8.0.26.jar:$APP_HOME/lib/tomcat-embed-el-8.0.26.jar:$APP_HOME/lib/tomcat-embed-logging-juli-8.0.26.jar:$APP_HOME/lib/tomcat-embed-websocket-8.0.26.jar:$APP_HOME/lib/jackson-annotations-2.4.6.jar:$APP_HOME/lib/jackson-core-2.4.6.jar:$APP_HOME/lib/classmate-1.0.0.jar:$APP_HOME/lib/tomcat-juli-8.0.26.jar:$APP_HOME/lib/objenesis-2.1.jar:$APP_HOME/lib/dom4j-1.6.1.jar:$APP_HOME/lib/antlr-runtime-3.4.jar:$APP_HOME/lib/ST4-4.0.4.jar:$APP_HOME/lib/ezmorph-1.0.6.jar:$APP_HOME/lib/commons-digester-2.1.jar:$APP_HOME/lib/commons-chain-1.1.jar:$APP_HOME/lib/commons-validator-1.3.1.jar:$APP_HOME/lib/oro-2.0.8.jar:$APP_HOME/lib/sslext-1.2-0.jar:$APP_HOME/lib/struts-core-1.3.8.jar:$APP_HOME/lib/struts-taglib-1.3.8.jar:$APP_HOME/lib/struts-tiles-1.3.8.jar:$APP_HOME/lib/commons-pool2-2.2.jar:$APP_HOME/lib/httpcore-4.3.3.jar:$APP_HOME/lib/httpcore-nio-4.3.2.jar:$APP_HOME/lib/c3p0-0.9.1.2.jar:$APP_HOME/lib/commons-math-2.2.jar:$APP_HOME/lib/jcl-over-slf4j-1.7.12.jar:$APP_HOME/lib/jul-to-slf4j-1.7.12.jar:$APP_HOME/lib/stringtemplate-3.2.1.jar:$APP_HOME/lib/antlr-2.7.7.jar:$APP_HOME/lib/netty-3.7.0.Final.jar:$APP_HOME/lib/xml-apis-1.0.b2.jar

# Determine the Java command to use to start the JVM.
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
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ] ; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
            MAX_FD="$MAX_FD_LIMIT"
        fi
        ulimit -n $MAX_FD
        if [ $? -ne 0 ] ; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
    JAVACMD=`cygpath --unix "$JAVACMD"`

    # We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
    SEP=""
    for dir in $ROOTDIRSRAW ; do
        ROOTDIRS="$ROOTDIRS$SEP$dir"
        SEP="|"
    done
    OURCYGPATTERN="(^($ROOTDIRS))"
    # Add a user-defined pattern to the cygpath arguments
    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
    fi
    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    i=0
    for arg in "$@" ; do
        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option

        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
        else
            eval `echo args$i`="\"$arg\""
        fi
        i=$((i+1))
    done
    case $i in
        (0) set -- ;;
        (1) set -- "$args0" ;;
        (2) set -- "$args0" "$args1" ;;
        (3) set -- "$args0" "$args1" "$args2" ;;
        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
    esac
fi

# Split up the JVM_OPTS And STOCK_SERVICE_DEPLOY_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
    JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $STOCK_SERVICE_DEPLOY_OPTS


exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" cn.com.duiba.Application "$@"
