#!/bin/bash -eu

echo ".: BEGIN $0 rev ${BITBUCKET_COMMIT:-undefined} :."

[[ ! -z ${AUTH0_CLIENT_ID:+true} ]] || {
  echo "ERROR: got empty or unset trying to set AUTH0_CLIENT_ID"
  exit 3
}

[[ ! -z ${AUTH0_CLIENT_SECRET:+true} ]] || {
  echo "ERROR: got empty or unset trying to set AUTH0_CLIENT_SECRET"
  exit 4
}

# exec, so the JVM replaces this shell and becomes PID 1.
#
# Without it bash stays PID 1 and java is its child. bash does not forward signals to a child it is waiting on, so ECS's
# SIGTERM reaches the shell and never the JVM: `server.shutdown: graceful` in application.yml is never entered, in-flight
# requests are cut, and the task is SIGKILLed when the stop timeout expires. Nothing logs a shutdown, which is what makes
# it easy to miss.
exec java ${JAVA_OPTS:-} -jar frontdoor-${VERSION}-SNAPSHOT.jar 2>&1
