#!/usr/bin/env bash
# ft-cutouts: the hand cutouts without pinches and grips. Your hands show through Frametop's
# screens, but ft-hands runs with --no-gestures, so nothing clicks or drags. ft-handsctl on is
# the full version; each stops the other. Like it, this doesn't start with SteamVR, and it
# stops when SteamVR does. On the Frame.
#
#   ft-cutouts on | off | status
#
# Runs this checkout's hands/build as transient user units, so it doesn't need hands/run.sh
# install: only a build (hands/build.sh) and ft-camd's capabilities (hands/run.sh caps).
set -euo pipefail
here=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)
camd=frametop-cutouts-camd.service hands=frametop-cutouts-hands.service

die() { echo "$*" >&2; exit 1; }

run_unit() {  # unit, the ft-handsctl unit it replaces, description, then the command
  local unit=$1 conflicts=$2 what=$3
  shift 3
  systemctl --user -q is-active "$unit" && return
  systemd-run --user --quiet --collect --unit="$unit" --description="Frametop hand cutouts: $what" \
    -p PartOf=steamvr.service -p After=steamvr.service -p Conflicts="$conflicts" \
    -p Restart=always -p RestartSec=5 -p TimeoutStopSec=5 "$@"
}

case ${1:-status} in
  on)
    systemctl --user -q is-active steamvr.service || die "SteamVR isn't running"
    [ -x "$here/build/ft-camd" ] && [ -x "$here/build/ft-hands" ] || die "not built: hands/build.sh"
    grep -qa -- --no-gestures "$here/build/ft-hands" || die "ft-hands predates --no-gestures: hands/build.sh"
    getcap "$here/build/ft-camd" | grep -q cap_sys_ptrace ||
      die "ft-camd needs its capabilities: hands/run.sh caps (asks for sudo)"
    if systemctl --user -q is-active frametop-camd.service frametop-hands.service; then
      echo "stopping ft-handsctl's hand tracking (it has pinches and grips)"
    fi
    run_unit $camd frametop-camd.service "the camera broker" "$here/build/ft-camd" --status 60
    if ! systemctl --user -q is-active $hands; then
      "$here/../scripts/container-up.sh"  # so stopping the unit can't take the container down
      pkill -x ft-hands || true
    fi
    run_unit $hands frametop-hands.service "hand tracking, no gestures" "$HOME/.local/bin/distrobox" enter dev -- \
      "$here/build/ft-hands" --status 60 --no-gestures
    "$here/ft-handsctl" cutouts on >/dev/null || true
    sleep 4
    "$0" status ;;
  off)
    systemctl --user stop $hands $camd 2>/dev/null || true
    systemctl --user -q is-active frametop-hands.service || pkill -x ft-hands || true
    echo "hand cutouts off" ;;
  status)
    for u in $camd $hands; do echo "$u: $(systemctl --user is-active $u || true)"; done
    echo "ft-screens cutouts: $("$here/ft-handsctl" cutouts state 2>&1)"
    inv=$(systemctl --user show -p InvocationID --value $hands)  # this run's lines only
    [ -n "$inv" ] && journalctl --user _SYSTEMD_INVOCATION_ID="$inv" --no-pager -o cat |
      grep -E '^ *[0-9.]+s |sets with a hand|cameras:' | tail -2 | cut -c1-160 || true ;;
  *) sed -n '2,10p' "$0" | sed 's/^# \{0,1\}//'; exit 2 ;;
esac
