#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(which sxmo_common.sh)"

pkill -9 lisgd

if [ -z "$LISGD_THRESHOLD" ]; then
	LISGD_THRESHOLD=125
fi
if [ -z "$LISGD_THRESHOLD_PRESSED" ]; then
	LISGD_THRESHOLD_PRESSED=60
fi

case "$(sxmo_wm.sh)" in
	sway)
		output="$(
			swaymsg -t get_outputs \
				| jq -r '.[] | select(.name == "DSI-1") | {transform: .transform, width: .current_mode.width, height: .current_mode.height}' \
				| sed "s|normal|0|"
		)"
		transform="$(printf %s "$output" | jq -r .transform)"
		width="$(printf %s "$output" | jq -r .width)"
		height="$(printf %s "$output" | jq -r .height)"
		orientation="$((transform / 90))"
		;;
	dwm)
		output="$(xrandr | grep DSI-1 | cut -d' ' -f4)"
		height="$(printf %s "$output" | cut -d'x' -f2 | cut -d'+' -f1)"
		width="$(printf %s "$output" | cut -d'x' -f1)"
		case "$(xrandr | grep DSI-1 | cut -d' ' -f 5)" in
			right) orientation=1;;
			left) orientation=3;;
			inverted) orientation=2;;
			*) orientation=0;;
		esac
		if [ "$((orientation%2))" -ne 0 ]; then
			tmp="$height"
			height="$width"
			width="$tmp"
		fi
esac

#-g format:
#   fingers,swipe,edge,distance,command
#order matters, only the first match gets executed
lisgd "$@" -o "$orientation" -h "$height" -w "$width" \
	-t "$LISGD_THRESHOLD" -T "$LISGD_THRESHOLD_PRESSED" \
	-g '1,DRUL,BR,*,sxmo_inputhandler.sh bottomrightcorner' \
	-g '1,DLUR,BL,*,sxmo_inputhandler.sh bottomleftcorner' \
	-g '1,ULDR,TL,*,sxmo_inputhandler.sh topleftcorner' \
	-g '1,URDL,TR,*,sxmo_inputhandler.sh toprightcorner' \
	-g '1,LR,B,L,sxmo_inputhandler.sh rightbottomcorner' \
	-g '1,RL,B,L,sxmo_inputhandler.sh leftbottomcorner' \
	-g '1,LR,L,*,sxmo_inputhandler.sh rightleftcorner' \
	-g '1,RL,R,*,sxmo_inputhandler.sh leftrightcorner' \
	-g '1,DU,L,*,P,sxmo_inputhandler.sh upleftcorner' \
	-g '1,UD,L,*,P,sxmo_inputhandler.sh downleftcorner' \
	-g '1,LR,T,*,P,sxmo_inputhandler.sh righttopcorner' \
	-g '1,RL,T,*,P,sxmo_inputhandler.sh lefttopcorner' \
	-g "1,DU,B,*,sxmo_inputhandler.sh upbottomcorner" \
	-g "1,UD,B,*,sxmo_inputhandler.sh downbottomcorner" \
	-g "1,UD,T,*,sxmo_inputhandler.sh downtopcorner" \
	-g "1,DU,T,*,sxmo_inputhandler.sh uptopcorner" \
	-g "2,UD,T,*,sxmo_inputhandler.sh twodowntopcorner" \
	-g "2,UD,B,*,sxmo_inputhandler.sh twodownbottomcorner" \
	-g '1,DU,R,*,P,sxmo_inputhandler.sh uprightcorner' \
	-g '1,UD,R,*,P,sxmo_inputhandler.sh downrightcorner' \
	-g '1,LR,R,S,sxmo_inputhandler.sh rightrightcorner_short' \
	-g '1,RL,L,S,sxmo_inputhandler.sh leftrightcorner_short' \
	-g '1,RL,*,L,sxmo_inputhandler.sh longoneleft' \
	-g '1,LR,*,L,sxmo_inputhandler.sh longoneright' \
	-g '1,DU,*,L,sxmo_inputhandler.sh longoneup' \
	-g '1,UD,*,L,sxmo_inputhandler.sh longonedown' \
	-g '1,RL,*,M,sxmo_inputhandler.sh mediumoneleft' \
	-g '1,LR,*,M,sxmo_inputhandler.sh mediumoneright' \
	-g '1,DU,*,M,sxmo_inputhandler.sh mediumoneup' \
	-g '1,UD,*,M,sxmo_inputhandler.sh mediumonedown' \
	-g '1,RL,*,*,sxmo_inputhandler.sh oneleft' \
	-g '1,LR,*,*,sxmo_inputhandler.sh oneright' \
	-g '1,DU,*,*,sxmo_inputhandler.sh oneup' \
	-g '1,UD,*,*,sxmo_inputhandler.sh onedown' \
	-g '1,DRUL,*,*,sxmo_inputhandler.sh upleft' \
	-g '1,URDL,*,*,sxmo_inputhandler.sh downleft' \
	-g '1,DLUR,*,*,sxmo_inputhandler.sh upright' \
	-g '1,ULDR,*,*,sxmo_inputhandler.sh downright' \
	-g '2,RL,*,*,sxmo_inputhandler.sh twoleft' \
	-g '2,LR,*,*,sxmo_inputhandler.sh tworight' \
	-g '2,DU,*,*,sxmo_inputhandler.sh twoup' \
	-g '2,UD,*,*,sxmo_inputhandler.sh twodown' \
	>"$CACHEDIR/lisgd.log" 2>&1