#!/bin/sh

# Load uhid (DS5 emulation)
echo "Loading uhid kernel module for DS5 emulation."
modprobe uhid

# Ensure Sunshine can grab images from KMS
path_to_setcap=$(which setcap)
path_to_sunshine=$(readlink -f "$(which sunshine)")
if [ -x "$path_to_setcap" ] ; then
  echo "Setting CAP_SYS_ADMIN, CAP_SYS_NICE capabilities on Sunshine binary."
  echo "$path_to_setcap cap_sys_admin,cap_sys_nice+p $path_to_sunshine"
  $path_to_setcap cap_sys_admin,cap_sys_nice+p $path_to_sunshine
  echo "CAP_SYS_ADMIN, CAP_SYS_NICE capabilities set on Sunshine binary."
else
  echo "error: setcap not found or not executable."
fi

# Trigger udev rule reload for /dev/uinput and /dev/uhid
path_to_udevadm=$(which udevadm)
if [ -x "$path_to_udevadm" ] ; then
  echo "Reloading udev rules."
  $path_to_udevadm control --reload-rules
  $path_to_udevadm trigger --property-match=DEVNAME=/dev/uinput
  $path_to_udevadm trigger --property-match=DEVNAME=/dev/uhid
  echo "Udev rules reloaded successfully."
else
  echo "error: udevadm not found or not executable."
fi
