#!/usr/bin/with-contenv bashio
# ==============================================================================
# Home Assistant Add-on: Grocy API
#
# This script is executed when the Grocy API Server terminates
# ==============================================================================

# Get the exit code of the application
EXIT_CODE=$1
SIGNAL_NUM=$2
CORE_DUMPED=$3

# Log the reason for termination with appropriate severity level
if (( SIGNAL_NUM > 0 )); then
  if (( CORE_DUMPED > 0 )); then
    bashio::log.critical "Application crashed with signal ${SIGNAL_NUM} and produced a core dump."
  else
    bashio::log.warning "Application terminated with signal ${SIGNAL_NUM}."
  fi
else
  if (( EXIT_CODE == 0 )); then
    bashio::log.info "Application stopped gracefully with exit code ${EXIT_CODE}."
  else
    bashio::log.error "Application exited with non-zero exit code ${EXIT_CODE}."
  fi
fi

# Perform any necessary cleanup here
bashio::log.info "Cleaning up resources..."

# Graceful shutdown: attempt to stop HTTP server if running
if [ -n "${HTTP_SERVER_PORT}" ]; then
  bashio::log.info "Attempting graceful shutdown of HTTP server on port ${HTTP_SERVER_PORT}..."
  # Optionally, send SIGTERM to any process listening on the port (if needed)
  # fuser -k ${HTTP_SERVER_PORT}/tcp || true
fi

# Return the original exit code
exit ${EXIT_CODE}
