#!/bin/bash

name=
#nameWacom=

if [ $LANG = "ru_RU.UTF-8" ]; then
CAPTION="Вращение экрана"
MESSAGE="Не удалось выполнить вращение"
MESSAGE2="Не удалось определить текущую ориентацию экрана"
else
CAPTION="Screen rotation"
MESSAGE="Screen rotation failed"
MESSAGE2="Can't detect current screen orientation"
fi

connected=$(xrandr -q --verbose | grep " connected primary")
output=$(echo "$connected" | head -n 1 | sed -e "s/\([a-zA-Z0-9]\+\) connected.*/\1/")

if [ -z "$output" ] ; then
  connected=$(xrandr -q --verbose | grep " connected")
  output=$(echo "$connected" | head -n 1 | sed -e "s/\([a-zA-Z0-9]\+\) connected.*/\1/")
  if [ -z "$output" ] ; then
    echo "Не найден экран для поворота"
    exit 1
  fi
fi

#Try to guess about current orientation
#Find the line in "xrandr -q --verbose" output that contains current screen orientation and "strip" out current orientation. 
rotation="$(echo "$connected" | egrep -o  '\) (normal|left|inverted|right) \(' | egrep -o '(normal|left|inverted|right)')"
#cut the first connected
rotation="$(echo $rotation | cut -d' ' -f1)"


#Get input devices to rotate too
#\t is default delimiter, to set other use -d option, i.e. -d' ' for space
#name=`fly-wmfunc FLYWM_GET_INPUT_DEVICES   0x5 | grep -v Wacom | cut -f1  2> /dev/null | tr '\n' ';'`
#nameWacom=`fly-wmfunc FLYWM_GET_INPUT_DEVICES 0x5 | grep Wacom | cut -f1  2> /dev/null | tr '\n' ';'`
name=`fly-wmfunc FLYWM_GET_INPUT_DEVICES   0x5 | cut -f1  2> /dev/null | tr '\n' ';'`
IFS=';'

#stop compton to restart - no, restart in fly-wm only to be compton switched off during lock screen to avoid artefacts when rotated
#dont need to restart wi th compton 6.2
#if [ ${DESKTOP_SESSION} != "fly-mobile" ]; then
#  COMPTON_PID=`ps -C compton -o pid=`
#  [ ! -z $COMPTON_PID ] && kill $COMPTON_PID
#fi

if ! test -z "$1"; then

#obsolete workaround
#case "$1" in
#    left)
#    if [ "$rotation" = "right" ]; then
#      xrandr --output $output --rotate normal
#      if [ "$?" = 1 ]; then
#        fly-dialog --caption "$CAPTION" --msgbox "$MESSAGE"
#        exit 1
#      fi
#      sleep 3
#    fi
#    ;;
#    right)
#    if [ "$rotation" = "left" ]; then
#      xrandr --output $output --rotate normal
#      if [ "$?" = 1 ]; then
#        fly-dialog --caption "$CAPTION" --msgbox "$MESSAGE"
#        exit 1
#      fi
#      sleep 3
#    fi
#    ;;
#esac

#Do required rotation
xrandr --output $output --rotate "$1"
if [ "$?" = 1 ]; then
    fly-dialog --caption "$CAPTION" --msgbox "$MESSAGE"
    exit 1
fi

rotateInput=$1

else

rotateInput=$rotation

fi

# Using current screen orientation to rotate input tools.
case "$rotateInput" in
    left)
    for name_i in ${name}; do
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axes Swap" 1
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axis Inversion" 1 0
    xinput set-prop --type=int --format=8 "${name_i}" "Wacom Rotation" 2
    done
    ;;
    right)
    for name_i in ${name}; do
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axes Swap" 1
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axis Inversion" 0 1
    xinput set-prop --type=int --format=8 "${name_i}" "Wacom Rotation" 1
    done
    ;;
    normal)
    for name_i in ${name}; do
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axes Swap" 0
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axis Inversion" 0 0
    xinput set-prop --type=int --format=8 "${name_i}" "Wacom Rotation" 0
    done
    ;;
    inverted)
    for name_i in ${name}; do
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axes Swap" 0
    xinput set-prop --type=int --format=8 "${name_i}" "Evdev Axis Inversion" 1 1
    xinput set-prop --type=int --format=8 "${name_i}" "Wacom Rotation" 3
    done
    ;;
esac

#alex: need delay?
#sleep 1
#TODO: do recalculation of icon positions instead of lineup
#fly-wmfunc FLYWM_ORDER_ICON


#dont need to restart wi th compton 6.2
#if [ ${DESKTOP_SESSION} != "fly-mobile" ]; then
# [ ! -z $COMPTON_PID ] && compton &
#fi
