#!/bin/bash

# source variables
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PWD=`pwd`
. "$PWD/env.sh"

PARAM=$@

# make some checks
JAVA_BINARY=$JAVA_HOME/bin/java
if [[ ! -d "$JAVA_HOME" ]]; then
    echo "Could not find $JAVA_BINARY set in env.sh. Trying to find the correct one..."
    JAVA_VERSION=$(java -version 2>&1 | sed 's/^\(java version "\(.*\)\.\(.*\)\..*"\)\|\(openjdk version "\(.*\)\.\(.*\)\.\(.*\)" .*\)$/\3\5/; 1q')
    if [ "$JAVA_VERSION" -lt 7 -o "$JAVA_VERSION" -gt 8 ]; then
        echo "The current Java version ($JAVA_VERSION) does not match the requirements (>= Java 7, <= Java 8)."
        exit
    fi
    JAVA_BINARY=`which java`
    echo "Using Java from $JAVA_BINARY"
fi
# disable AWT requiring X11 and the correct DISPLAY variable setting; failing to do so
# can result in trouble generating QR codes in the back-end
ADDITIONAL_JAVA_ARGS="$ADDITIONAL_JAVA_ARGS -Djava.awt.headless=true -Xms$MEMORY -Dosgi.debug=$PWD/configuration/debug.properties"
PLATFORM=`uname`
SED_ARGS="-i"
if [[ $PLATFORM == "Darwin" ]]; then
    SED_ARGS="-i \"\""
fi

if [[ $PLATFORM != "Darwin" ]]; then
    TELNET_ACTIVE=`netstat -tlnp 2>/dev/null | grep ":$SERVER_PORT"`
else
    TELNET_ACTIVE=`netstat -an 2>/dev/null | grep ".$SERVER_PORT"`
fi

if [[ $TELNET_ACTIVE != "" ]]; then
        echo "There is something already running on port $SERVER_PORT. Perhaps your server that has not been stopped?"
        exit
fi

scriptdir=`dirname "$0"`
LIB_DIR="$PWD/native-libraries"

if [[ ! -d "$PWD/logs" ]]; then
    mkdir logs
fi

if [[ ! -d "$PWD/tmp" ]]; then
    mkdir tmp
fi

echo "Script directory is $scriptdir and LIB_DIR is $LIB_DIR"

# Make really sure that parameters from config.ini are not used
sed $SED_ARGS "/mongo.host/d" ./configuration/config.ini
sed $SED_ARGS "/mongo.port/d" ./configuration/config.ini
sed $SED_ARGS "/expedition.udp.port/d" ./configuration/config.ini
sed $SED_ARGS "/replication.exchangeName/d" ./configuration/config.ini
sed $SED_ARGS "/replication.exchangeHost/d" ./configuration/config.ini

# Ensure that Jetty Port is set correctly
sed $SED_ARGS "s/^.*jetty.http.port.*$/<Set name=\"port\"><Property name=\"jetty.http.port\" deprecated=\"jetty.port\" default=\"$SERVER_PORT\"\/><\/Set>/g" ./configuration/jetty/etc/jetty-http.xml

# Update monitoring with the right ports
sed $SED_ARGS "s/127.0.0.1:[0-9][0-9][0-9][0-9]\//127.0.0.1:$SERVER_PORT\//g" ./configuration/monitoring.properties

# Inject information for system
HEAD_DATE=$(date "+%Y%m%d%H%M")
if [ "${MONGODB_URI}" != "" ]; then
  MONGODB_URI_FOR_SED=$( echo "$MONGODB_URI" | sed -e 's/^mongodb:\/\///' -e 's/^[^:]*:[^@]*@//' -e 's/\//\\\//g' -e 's/&/\\&/g' )
else
  MONGODB_URI_FOR_SED="$MONGODB_HOST:$MONGODB_PORT\/$MONGODB_NAME"
fi
sed $SED_ARGS "s/System:.*$/System: $MONGODB_URI_FOR_SED-$EXPEDITION_PORT-$REPLICATION_HOST:$REPLICATION_PORT\/$REPLICATION_CHANNEL Started: $HEAD_DATE/g" ./configuration/jetty/version.txt
VERSION_JSON_CONTENTS=$( cat ./configuration/jetty/version.json )
echo "${VERSION_JSON_CONTENTS}" | jq --argjson start_time_millis $( date +%s )000 '. + {start_time_millis: $start_time_millis} + {port: '${SERVER_PORT}'}' >./configuration/jetty/version.json

# Apply app parameters from env.sh
if [ "$MONGODB_URI" = "" ]; then
  APP_PARAMETERS="-Dmongo.host=$MONGODB_HOST -Dmongo.port=$MONGODB_PORT -Dmongo.dbName=$MONGODB_NAME"
else
  APP_PARAMETERS="-Dmongo.uri=$MONGODB_URI"
fi
APP_PARAMETERS="$APP_PARAMETERS -Dexpedition.udp.port=$EXPEDITION_PORT -Dreplication.exchangeHost=$REPLICATION_HOST -Dreplication.exchangePort=$REPLICATION_PORT -Dreplication.exchangeName=$REPLICATION_CHANNEL"

# Apply parameters for automatic replication start
REPLICATION_PARAMETERS="-Dreplicate.on.start=$REPLICATE_ON_START -Dreplicate.master.servlet.host=$REPLICATE_MASTER_SERVLET_HOST -Dreplicate.master.servlet.port=$REPLICATE_MASTER_SERVLET_PORT -Dreplicate.master.queue.host=$REPLICATE_MASTER_QUEUE_HOST -Dreplicate.master.queue.port=$REPLICATE_MASTER_QUEUE_PORT -Dreplicate.master.exchange.name=$REPLICATE_MASTER_EXCHANGE_NAME -Dreplicate.master.username=$REPLICATE_MASTER_USERNAME -Dreplicate.master.password=$REPLICATE_MASTER_PASSWORD -Dreplicate.master.bearer_token=$REPLICATE_MASTER_BEARER_TOKEN"

EQUINOX_LAUNCHER=$( find plugins -name 'org.eclipse.equinox.launcher_*.jar' )
COMMON_JAVA_ARGS="-D$SERVER_NAME $ADDITIONAL_JAVA_ARGS -Dcom.sap.sailing.server.name=$SERVER_NAME $APP_PARAMETERS $REPLICATION_PARAMETERS -Djava.io.tmpdir=$PWD/tmp -Dfile.encoding=cp1252 -Djetty.home=$scriptdir/configuration/jetty -Djava.util.logging.config.file=$scriptdir/configuration/logging.properties -Djava.library.path=$LIB_DIR -Dorg.eclipse.jetty.annotations.maxWait=120 -Djava.naming.factory.url.pkgs=org.eclipse.jetty.jndi -Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory -Dgwt.rpc.version=9 -Xmx$MEMORY -jar ${EQUINOX_LAUNCHER} -configuration ./ -clean -console ${TELNET_PORT}"
if [[ $PARAM == "" ]] || [[ $PARAM == "no-update" ]]; then
    nohup "$JAVA_BINARY" ${COMMON_JAVA_ARGS} 2>/dev/null >/dev/null &
    JAVA_PID=$!
    echo "Starting server... (PID: $JAVA_PID)"
    echo "SERVER_NAME: $SERVER_NAME"
    echo "SERVER_PORT: $SERVER_PORT"
    echo "MEMORY: $MEMORY"
    echo "TELNET_PORT: $TELNET_PORT"
    echo "MONGODB_URI: $MONGODB_URI"
    echo "MONGODB_HOST: $MONGODB_HOST"
    echo "MONGODB_PORT: $MONGODB_PORT"
    echo "MONGODB_NAME: $MONGODB_NAME"
    echo "EXPEDITION_PORT: $EXPEDITION_PORT"
    echo "REPLICATION_CHANNEL: $REPLICATION_CHANNEL"

    echo $JAVA_PID > server.pid
    if [[ ! -z "$ON_AMAZON" ]]; then
        # give instances on amazon more time to start because
        # they may be influenced by the server's boot process
        echo "Be aware of the fact that starting amazon instances takes up to two minutes!"
        sleep 15
        echo "Your instance should now be ready - waiting some more minutes before checking state..."
        sleep 120
    else
        sleep 10
    fi

    if [[ $PLATFORM != "Darwin" ]]; then
        SERVER_ACTIVE=`netstat -tlnp 2>/dev/null | grep ":$SERVER_PORT"`
        NETSTAT_INFO=`netstat -ntpl 2>/dev/null`
    else
        SERVER_ACTIVE=`netstat -an 2>/dev/null | grep ":$SERVER_PORT"`
        NETSTAT_INFO=""
    fi

    PROCESS_INFO=`ps auxwww`
    if [[ $PLATFORM != "Darwin" ]]; then
        if [[ $SERVER_PORT == "" ]] || [[ $SERVER_ACTIVE == "" ]]; then
            echo "Something went wrong with startup. Please check the logs..."
            if which mail; then
                if [[ ${SERVER_STARTUP_NOTIFY} != "" ]]; then
                    echo "Startup of java server ${SERVER_NAME} on ${INSTANCE_ID} failed - check the logs ($SERVER_ACTIVE)\n\n$NETSTAT\n$PROCESS_INFO" | mail -r noreply@sapsailing.com -s "Server $INSTANCE_ID startup failed!" ${SERVER_STARTUP_NOTIFY}
                fi
            fi
            exit
        fi
    fi
    echo "Started $SERVER_NAME - use telnet localhost $TELNET_PORT to connect to the OSGi console. Logs should be found in logs/sailing0.log.0"
    if which mail; then
        if [[ ${SERVER_STARTUP_NOTIFY} != "" ]]; then
            if [[ ! -z "$ON_AMAZON" ]]; then
                echo "Go to http://$INSTANCE_DNS:$SERVER_PORT to reach your instance!" | mail -r noreply@sapsailing.com -s "Server $SERVER_NAME on $INSTANCE_ID started up!" $SERVER_STARTUP_NOTIFY
            else
                echo "OK" | mail -r noreply@sapsailing.com -s "Server $SERVER_NAME on $INSTANCE_ID started up!" ${SERVER_STARTUP_NOTIFY}
            fi
        fi
    fi
elif [[ $PARAM == "debug" ]]; then
    echo "Starting remote debugging session on port 8000 - make sure this port is open and reachable from the outside"
    JFR_OPTIONS="-XX:+FlightRecorder -Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.rmi.port=7091 -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost"
    "$JAVA_BINARY" -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=*:8000,suspend=y $JFR_OPTIONS ${COMMON_JAVA_ARGS}
elif [[ $PARAM == "fg" ]]; then
    exec "$JAVA_BINARY" ${COMMON_JAVA_ARGS}
elif [[ $PARAM == "docker" ]]; then
    . generateMailProperties.sh
    exec "$JAVA_BINARY" ${COMMON_JAVA_ARGS}
fi


