#!/bin/bash
cd papermc
urlPrefix=https://papermc.io/api/v2/projects/paper
if [ ${MC_VERSION} = latest ]; then
  MC_VERSION=$(wget -qO - $urlPrefix | jq -r '.versions[-1]') # "-r" is needed because the output has quotes otherwise
fi
urlPrefix=${urlPrefix}/versions/${MC_VERSION}
if [ ${PAPER_BUILD} = latest ]; then
  PAPER_BUILD=$(wget -qO - $urlPrefix | jq '.builds[-1]')
fi
JAR_NAME=papermc-${MC_VERSION}-${PAPER_BUILD}.jar
if [ ! -e ${JAR_NAME} ]; then
  rm -f *.jar
  wget ${urlPrefix}/builds/${PAPER_BUILD}/downloads/paper-${MC_VERSION}-${PAPER_BUILD}.jar -O ${JAR_NAME}
  if [ ! -e eula.txt ]; then
    java -jar ${JAR_NAME}
    sed -i 's/false/true/g' eula.txt
  fi
fi

#exec java -server -Xms${MC_RAM} -Xmx${MC_RAM} ${JAVA_OPTS} -jar ${JAR_NAME} nogui
exec java -Xms${MC_RAM} -Xmx${MC_RAM} -XX:+UseG1GC \
  -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 \
  -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC \
  -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 \
  -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 \
  -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 \
  -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 \
  -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 \
  ${JAVA_OPTS} -jar ${JAR_NAME} nogui

