#! /bin/sh

set -e
exec 2>&1

ENV=development
if [ -f /etc/yandex/environment.type ]; then
    ENV=$(cat /etc/yandex/environment.type)
fi

PATH=/sbin:/usr/sbin:/bin:/usr/bin
LANG=en_US.UTF-8
DESC="direct-api5"
USER=ppc
GROUP=ppc
CWD=/var/www/direct-api5
NAME=direct-api5
VERSION=`cat /var/www/direct-api5/version`
GC_LOG=/var/log/yandex/$NAME.gc.log
ERROR_LOG=/var/log/yandex/$NAME.error.log
JAVA=/usr/local/yandex-direct-jdk11/bin/java
case "$ENV" in
    production) MEM_OPTS="-Xmx24G -Xms24G";;
    *) MEM_OPTS="-Xmx4G -Xms1G";;
esac

if [[ -v DEPLOY_NODE_DC ]]; then
    DC_OPTS="-Ddirect.dc=$DEPLOY_NODE_DC"
else
    DC_OPTS=""
fi

JAVA_OPTS="$MEM_OPTS \
    -Dfile.encoding=UTF-8 \
    -Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true \
    -Dsun.net.inetaddr.ttl=60 -Dsun.net.inetaddr.negative.ttl=0 \
    -Djava.util.Arrays.useLegacyMergeSort=true \
    -XX:-UsePerfData \
    -XX:+UseG1GC \
    -XX:MaxGCPauseMillis=100 \
    -Xlog:gc*=info,safepoint=info,age*=trace:file=$GC_LOG:time,uptime,level,tags:filecount=8,filesize=100M \
    -XX:+CrashOnOutOfMemoryError \
    -XX:+HeapDumpOnOutOfMemoryError \
    -XX:-OmitStackTraceInFastThrow \
    -XX:HeapDumpPath=/var/log/yandex/direct-api5.hprof"
PROG_OPTS="--log-configs-directory /etc/direct/api5/logging-config"
CLASSPATH="/var/www/direct-api5/hotfix/$VERSION/classes/:/var/www/direct-api5/direct-api5/*:"
JAVA_ARGS="$JAVA_OPTS $DC_OPTS -cp $CLASSPATH -Djava.library.path=/var/www/direct-api5/direct-api5 ru.yandex.direct.api.v5.YandexDirectApiV5Application $PROG_OPTS"

chown ppc /var/log/yandex
# jvm can't append data to previous log, so copy logs to archive
for log in "$GC_LOG"*; do
    test -f "$log" && cat "$log" >>`dirname "$GC_LOG"`/arc.`basename "$GC_LOG"`.`date +%Y%m%d` && rm "$log"
done

if [ -f "$ERROR_LOG" ]; then
  TODAY_ERROR_LOG=$ERROR_LOG.$(date +%Y%m%d)
  if [ -f "$TODAY_ERROR_LOG" ]; then
      cat "$ERROR_LOG" >>"$TODAY_ERROR_LOG"
      rm "$ERROR_LOG"
  else
      mv "$ERROR_LOG" "$TODAY_ERROR_LOG"
  fi
fi

cd "$CWD"
exec chpst -u "$USER:$GROUP" "$JAVA" $JAVA_ARGS >> "$ERROR_LOG" 2>&1
