#!/bin/bash

# custom AppRun for Sunshine AppImage

# path of the extracted AppRun
HERE="$(dirname "$(readlink -f "${0}")")"
SUNSHINE_PATH=/usr/bin/sunshine
SUNSHINE_BIN_HERE=$HERE/usr/bin/sunshine
SUNSHINE_SHARE_HERE=$HERE/usr/share/sunshine

# Set APPDIR when running directly from the AppDir:
if [ -z "$APPDIR" ]; then
    ARGV0="AppRun"
fi

cd "$HERE" || exit 1

function help() {
echo "
 ------------------------------
   Sunshine AppImage package.
 ------------------------------

 sunshine.AppImage options
 ------------------------

 Usage:  $ARGV0  --help, -h
 ------            # This message

         $ARGV0  --install, -i
                   # Install input rules sunshine.service files. Restart required.

         $ARGV0  --remove, -r
                   # Remove input rules sunshine.service files.

         $ARGV0  --appimage-help
                   # Show available AppImage options

 sunshine options
 ----------------
"
# print sunshine binary help, replacing the sunshine command in usage statement
"$SUNSHINE_BIN_HERE" --help | sed -e "s#$SUNSHINE_BIN_HERE#$ARGV0#g"
}

function install() {
  # user input rules
  # shellcheck disable=SC2002
  cat "$SUNSHINE_SHARE_HERE/udev/rules.d/60-sunshine.rules" | sudo tee /etc/udev/rules.d/60-sunshine.rules
  cat "$SUNSHINE_SHARE_HERE/modules-load.d/60-sunshine.conf" | sudo tee /etc/modules-load.d/60-sunshine.conf
  sudo modprobe uhid
  sudo udevadm control --reload-rules
  sudo udevadm trigger --property-match=DEVNAME=/dev/uinput
  sudo udevadm trigger --property-match=DEVNAME=/dev/uhid

  # sunshine service
  mkdir -p ~/.config/systemd/user
  cp -r "$SUNSHINE_SHARE_HERE/systemd/user/" ~/.config/systemd/
  # patch service executable path
  sed -i -e "s#$SUNSHINE_PATH#$(readlink -f $ARGV0)#g" ~/.config/systemd/user/app-dev.lizardbyte.app.Sunshine.service

  # setcap
  sudo setcap cap_sys_admin,cap_sys_nice+p "$(readlink -f "$SUNSHINE_BIN_HERE")"
}

function remove() {
  # remove input rules
  sudo rm -f /etc/udev/rules.d/60-sunshine.rules

  # remove uhid module loading config
  sudo rm -f /etc/modules-load.d/60-sunshine.conf

  # remove service
  sudo rm -f ~/.config/systemd/user/app-dev.lizardbyte.app.Sunshine.service
}

# process arguments
if [ "x$1" == "xhelp" ] || [ "x$1" == "x--help" ] || [ "x$1" == "x-h" ] ; then
    help
    exit $?
fi

if [ "x$1" == "xinstall" ] || [ "x$1" == "x--install" ] || [ "x$1" == "x-i" ] ; then
    install
    exit $?
fi

if [ "x$1" == "xremove" ] || [ "x$1" == "x--remove" ] || [ "x$1" == "x-r" ] ; then
    remove
    exit $?
fi

# create config directory if it doesn't exist
# https://github.com/LizardByte/Sunshine/issues/324
mkdir -p ~/.config/sunshine

# run sunshine
"$SUNSHINE_BIN_HERE" $@
