#!/bin/bash
set -u

# Pin PATH rather than rely on sudoers secure_path being set.
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH

usage() {
  echo "usage: unifi-protect-uled-ctrl blink 500 0xffffff 0x000000|fw idle" >&2
  exit 1
}

# The two shapes the app sends are fixed, so match the whole argument vector. The arity check is
# what keeps "$*" honest: without it one space-containing argument joins to the same string.
case "$*" in
  'blink 500 0xffffff 0x000000') [ $# -eq 4 ] || usage ;;
  'fw idle') [ $# -eq 2 ] || usage ;;
  *) usage ;;
esac

# Bound the call here: the unprivileged caller cannot signal this process, and a wedged uled-ctrl
# strands the LED in whatever state the last call set.
exec /usr/bin/timeout -k 3 5 /usr/bin/uled-ctrl "$@"
