#!/command/with-contenv bash
# shellcheck shell=bash
# Take down the S6 supervision tree when the service exits

set -o errexit -o nounset -o pipefail

declare exit_code_container
exit_code_container=$(cat /run/s6-linux-init-container-results/exitcode)
readonly exit_code_container
readonly exit_code_service="${1}"
readonly exit_code_signal="${2}"
readonly service="Frigate"

echo "Service ${service} exited with code ${exit_code_service} (by signal ${exit_code_signal})" >&2

if [[ "${exit_code_service}" -eq 256 ]]; then
  if [[ "${exit_code_container}" -eq 0 ]]; then
    echo $((128 + exit_code_signal)) > /run/s6-linux-init-container-results/exitcode
  fi
elif [[ "${exit_code_service}" -ne 0 ]]; then
  if [[ "${exit_code_container}" -eq 0 ]]; then
    echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode
  fi
else
  # Exit code 0 is expected when Frigate is restarted by the user. In this case,
  # we create a signal for the go2rtc finish script to tolerate the restart.
  touch /dev/shm/restarting-frigate
fi

exec /run/s6/basedir/bin/halt