#! /bin/sh
#
# switch_monitors.sh
# Copyright (C) 2017 Fabian Ising <fabian@murgi.de>
#
# Distributed under terms of the Apache v2.0 license.
#

# Get info on the monitors
HDMI_STATUS=$(</sys/class/drm/card0/card0-HDMI-A-1/status )  

HDMI_ENABLED=$(</sys/class/drm/card0/card0-HDMI-A-1/enabled)  

DP_STATUS=$(</sys/class/drm/card0/card0-DP-1/status )  
DP_ENABLED=$(</sys/class/drm/card0/card0-DP-1/enabled )  

# Check to see if our state log exists
if [ ! -f /tmp/monitor ]; then  
    touch /tmp/monitor
    STATE=5
else  
    STATE=$(</tmp/monitor)
fi

# The state log has the NEXT state to go to in it

# If monitors are disconnected, stay in state 1
if [ "disconnected" == "$HDMI_STATUS" -a "disconnected" == "$DP_STATUS" ]; then  
    STATE=5
fi
if [ $1 == "mirror" ]; then
    if [ $STATE == 4 ]; then
        /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Mirror Stuff from $STATE to 5"
        # eDP-1 is on, projectors are mirroring
        if [ "connected" == "$HDMI_STATUS" ]; then
            /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --auto --same-as eDP-1
            TYPE="HDMI"
        elif [ "connected" == "$DP_STATUS" ]; then
            /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --off --output DP-1 --auto --same-as eDP-1
            TYPE="DP-1"
        fi
        /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE mirroring"
        STATE=5
    else
        /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Mirror Stuff from $STATE to 4"
        # eDP-1 is on, projectors are extending
        if [ "connected" == "$HDMI_STATUS" ]; then
            /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --auto --right-of eDP-1
            TYPE="HDMI"
        elif [ "connected" == "$DP_STATUS" ]; then
            /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --off --output DP-1 --auto --left-of eDP-1
            TYPE="DP-1"
        fi
        /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE extending"
        STATE=4
    fi
else
TYPE="eDP-1"
case $STATE in  
    1)
    # eDP-1 is on, projectors not connected
    /usr/bin/xrandr --output eDP-1 --auto
    STATE=2
    /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE"
    ;;
    2)
    # eDP-1 is on, projectors are connected but inactive
    /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --off
    TYPE="eDP-1"
    STATE=3 
    /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE"
    ;;
    3)
    # eDP-1 is off, projectors are on
    if [ "connected" == "$HDMI_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --off --output HDMI-1 --auto
        TYPE="HDMI"
    elif [ "connected" == "$DP_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --off --output HDMI-1 --off --output DP-1 --auto
        TYPE="DP-1"
    fi
    /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE"
    STATE=4
    ;;
    4)
    # eDP-1 is on, projectors are mirroring
    if [ "connected" == "$HDMI_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --auto --same-as eDP-1
        TYPE="HDMI"
    elif [ "connected" == "$DP_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --off --output DP-1 --auto --same-as eDP-1
        TYPE="DP-1"
    fi
    /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE mirroring"
    STATE=5
    ;;
    5) 
    # eDP-1 is on, projectors are extending
    if [ "connected" == "$HDMI_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --auto --right-of eDP-1
        TYPE="HDMI"
    elif [ "connected" == "$DP_STATUS" ]; then
        /usr/bin/xrandr --output eDP-1 --auto --output HDMI-1 --off --output DP-1 --auto --left-of eDP-1
        TYPE="DP-1"
    fi
    /usr/bin/notify-send -t 5000 --urgency=low "Graphics Update" "Switched to $TYPE extending"
    STATE=2
    ;;
    *)
    # Unknown state, assume we're in 1
    STATE=1 
esac    
fi

echo $STATE > /tmp/monitor  
