#!/bin/bash

cd /home/minecraft

# https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start-minecraft.sh

if [ ! -e eula.txt ]; then
  if [ "$EULA" != "" ]; then
    echo "# Generated via Docker at $(date)" > eula.txt
    echo "eula=TRUE" >> eula.txt
  else
    echo "Accept the Minecraft EULA at"
    echo "  https://account.mojang.com/documents/minecraft_eula"
    echo "by adding -e EULA=TRUE"
    exit 1
  fi
fi

SERVER_PROPERTIES=server.properties

function setProp {
  local prop=$1
  local var=$2
  if [ -n "$var" ]; then
    echo "Setting $prop to $var"
    sed -i "/$prop\s*=/ c $prop=$var" server.properties
  fi
}

if [ ! -e server.properties ]; then
  echo "Creating server.properties"
  cp base.properties server.properties
  if [ -n "$WHITELIST" ]; then
    echo "Whitelist enable. You need to create the whitelist.json maunally, or disable and add users with /whitelist nick"
  fi

  setProp "motd" "$MOTD"
  setProp "allow-nether" "$ALLOW_NETHER"
  setProp "announce-player-achievements" "$ANNOUNCE_PLAYER_ACHIEVEMENTS"
  setProp "enable-command-block" "$ENABLE_COMMAND_BLOCK"
  setProp "spawn-animals" "$SPAWN_ANIMAILS"
  setProp "spawn-monsters" "$SPAWN_MONSTERS"
  setProp "spawn-npcs" "$SPAWN_NPCS"
  setProp "generate-structures" "$GENERATE_STRUCTURES"
  setProp "spawn-npcs" "$SPAWN_NPCS"
  setProp "view-distance" "$VIEW_DISTANCE"
  setProp "hardcore" "$HARDCORE"
  setProp "max-build-height" "$MAX_BUILD_HEIGHT"
  setProp "force-gamemode" "$FORCE_GAMEMODE"
  setProp "hardmax-tick-timecore" "$MAX_TICK_TIME"
  setProp "enable-query" "$ENABLE_QUERY"
  setProp "query.port" "$QUERY_PORT"
  setProp "enable-rcon" "$ENABLE_RCON"
  setProp "rcon.password" "$RCON_PASSWORD"
  setProp "rcon.port" "$RCON_PORT"
  setProp "max-players" "$MAX_PLAYERS"
  setProp "max-world-size" "$MAX_WORLD_SIZE"
  setProp "level-name" "$LEVEL"
  setProp "level-seed" "$SEED"
  setProp "pvp" "$PVP"
  setProp "generator-settings" "$GENERATOR_SETTINGS"
  setProp "online-mode" "$ONLINE_MODE"

  if [ -n "$LEVEL_TYPE" ]; then
    # uppercase
    LEVEL_TYPE=$( echo ${LEVEL_TYPE} | tr '[:lower:]' '[:upper:]' )
    echo "Setting level type to $LEVEL_TYPE"
    case $LEVEL_TYPE in
      DEFAULT|FLAT|LARGEBIOMES|AMPLIFIED|CUSTOMIZED|BIOMESOP)
        setProp "level-type" "$LEVEL_TYPE"
        ;;
      *)
        echo "Invalid LEVEL_TYPE: $LEVEL_TYPE"
        echo "Try: DEFAULT FLAT LARGEBIOMES AMPLIFIED CUSTOMZED or BIOMESOP"
        exit 1
        ;;
    esac
  fi

  if [ -n "$DIFFICULTY" ]; then
    case $DIFFICULTY in
      peaceful|0)
        DIFFICULTY=0
        ;;
      easy|1)
        DIFFICULTY=1
        ;;
      normal|2)
        DIFFICULTY=2
        ;;
      hard|3)
        DIFFICULTY=3
        ;;
      *)
        echo "DIFFICULTY must be peaceful, easy, normal, or hard."
        exit 1
        ;;
    esac
    echo "Setting difficulty to $DIFFICULTY"
    setProp "difficulty" "$DIFFICULTY"
  fi

  if [ -n "$MODE" ]; then
    echo "Setting mode"
    MODE_LC=$( echo $MODE | tr '[:upper:]' '[:lower:]' )
    case $MODE_LC in
      0|1|2|3)
        ;;
      su*)
        MODE=0
        ;;
      c*)
        MODE=1
        ;;
      a*)
        MODE=2
        ;;
      sp*)
        MODE=3
        ;;
      *)
        echo "ERROR: Invalid game mode: $MODE"
        exit 1
        ;;
    esac  
    setProp "gamemode" "$MODE"
  fi
fi

if [ -n "$OPS" -a ! -e ops.txt.converted ]; then
  echo "Setting ops"
  echo $OPS | awk -v RS=, '{print}' >> ops.txt
fi

# There whitelist code https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start-minecraft.sh#L540
# but it doesn't work.  See https://www.beastnode.com/portal/knowledgebase/6/Setting-Up-a-Whitelist-or-Make-Your-Server-Private.html
# on using /whitelist add username (to actually setup and use the whitelist.json file)   :P

if [ ! -e banned-players.json ]; then
  echo '[]' > banned-players.json
fi

if [ ! -e banned-ips.json ]; then
  echo '[]' > banned-ips.json
fi

EXTRA_ARGS=""
# Optional disable console
if [[ ${CONSOLE} = false || ${CONSOLE} = FALSE ]]; then
  EXTRA_ARGS+="--noconsole"
fi

# Optional disable GUI for headless servers
if [[ ${GUI} = false || ${GUI} = FALSE ]]; then
  EXTRA_ARGS="${EXTRA_ARGS} nogui"
fi

# put these prior JVM_OPTS at the end to give any memory settings there higher precedence
echo "Setting initial memory to ${INIT_MEMORY:-${MEMORY}} and max to ${MAX_MEMORY:-${MEMORY}}"
JVM_OPTS="-Xms${INIT_MEMORY:-${MEMORY}} -Xmx${MAX_MEMORY:-${MEMORY}} ${JVM_OPTS}"

exec java $JVM_XX_OPTS $JVM_OPTS -jar $SERVER "$@" $EXTRA_ARGS

