#!/usr/bin/env bash
# Stands in for the release venv's python in certrun.bats, as the core:
#   python -I -B -m certrun JOB       does what APP/core-mode says: "exit N", "sleep N" (TERM
#                                     ends the sleep) or "kill" (dies of SIGKILL at once)
app=$(cd -- "$(dirname -- "$0")/../../.." && pwd -P)
echo "$$" >"$app/core.pid"
read -r action arg <"$app/core-mode"
case $action in
  exit) exit "$arg" ;;
  kill) kill -KILL "$$" ;;
  sleep)
    sleep "$arg" &
    trap 'kill "$!"; echo stopped >"$app/core.stopped"; exit 143' TERM
    wait "$!"
    ;;
esac
