#!/bin/bash

ACTION=$1

# execute action
if [ "$ACTION" != "" ]; then
  if [ "$ACTION" = "rotate" ]; then
    xrandr -o inverted
    touch /tmp/screen_rotated.txt
    rm /tmp/screen_normal.txt 2> /dev/null
  elif [ "$ACTION" = "normal" ]; then
    xrandr -o normal
    touch /tmp/screen_normal.txt
    rm /tmp/screen_rotated.txt 2> /dev/null
  fi

  exit
fi

if [ -e "/tmp/screen_normal.txt" ]; then
  xrandr -o inverted
  touch /tmp/screen_rotated.txt
  rm /tmp/screen_normal.txt 2> /dev/null
elif [ -e "/tmp/screen_rotated.txt" ]; then
  xrandr -o normal
  touch /tmp/screen_normal.txt
  rm /tmp/screen_rotated.txt 2> /dev/null
else
  xrandr -o inverted
  touch /tmp/screen_rotated.txt
fi

