#!/usr/bin/env bash

DEVICE=/dev/input/by-path/pci-0000:00:15.0-platform-i2c_designware.0-event
ORIENTATION=${ORIENTATION:-normal}


echo "Starting lisgd with orientation: $ORIENTATION" >> /tmp/lisgd.log


if [[ $ORIENTATION == "normal" ]]; then
  niri msg output eDP-1 transform normal
elif [[ $ORIENTATION == "270" ]]; then
  niri msg output eDP-1 transform 270
elif [[ $ORIENTATION == "inverted" ]]; then
  niri msg output eDP-1 transform inverted
elif [[ $ORIENTATION == "90" ]]; then
  niri msg output eDP-1 transform 90
else
  echo "Unknown orientation: $ORIENTATION" >> /tmp/lisgd.log
fi

stop_lisgd() {
  pkill -f "lisgd -d $DEVICE" 2>/dev/null
}

start_lisgd() {
  echo "Starting lisgd for device: $DEVICE" >> /tmp/lisgd.log
  stop_lisgd
  sleep 0.1
  lisgd -d "$DEVICE" -t 10 -T 5 \
    -g "4,DU,*,*,niri msg action open-overview" \
    -g "1,DU,B,*,niri msg action open-overview" \
    -g "3,LR,*,*,niri msg action focus-column-left" \
    -g "3,RL,*,*,niri msg action focus-column-right" \
    -g "3,DU,*,*,niri msg action focus-workspace-up" \
    -g "3,UD,*,*,niri msg action focus-workspace-down" \
     &

  LISGD_PID=$!

  echo "Started lisgd with PID: $LISGD_PID" >> /tmp/lisgd.log
}

cleanup() {
  stop_lisgd
  exit 0
}

trap cleanup SIGINT SIGTERM


start_lisgd