#!/bin/bash

. "/image/scripts/start-utils"
# The healthcheck runs as root, but /data is writable by the server, so only
# known settings are read from this file; it is never sourced.
MC_HEALTH_EXTRA_ARGS=()
if [ -f /data/.mc-health.env ]; then
  while read -r line; do
    case "$line" in
      DISABLE_HEALTHCHECK=true)
        DISABLE_HEALTHCHECK=true
        ;;
      --use-old-server-list-ping|--use-server-list-ping|--use-proxy)
        MC_HEALTH_EXTRA_ARGS+=("$line")
        ;;
    esac
  done < /data/.mc-health.env
fi

if isTrue "${DISABLE_HEALTHCHECK}"; then
  echo "Healthcheck disabled"
  exit 0
elif isTrue "${ENABLE_AUTOPAUSE}" && [[ "$( ps -ax -o stat,comm | grep 'java' | awk '{ print $1 }')" =~ ^T.*$ ]]; then
  echo "Java process suspended by Autopause function"
  exit 0
else
  mc-monitor status "${MC_HEALTH_EXTRA_ARGS[@]}" --host "${SERVER_HOST:-localhost}" --port "${SERVER_PORT:-25565}"
  exit $?
fi
