#!/usr/bin/env bash
set -Eeuo pipefail

if [[ ${EUID} -ne 0 ]]; then
  echo "The power helper must run as root." >&2
  exit 1
fi

case "${1:-}" in
  reboot|poweroff)
    action="$1"
    ;;
  *)
    echo "Usage: pi-car-companion-power reboot|poweroff" >&2
    exit 2
    ;;
esac

/usr/bin/systemd-run \
  --quiet \
  --collect \
  --on-active=3s \
  --unit="pi-car-companion-${action}" \
  /usr/bin/systemctl "${action}"
