#!/bin/bash
set -o pipefail

export DISPLAY=:0

XRANDR="/usr/bin/xrandr"
AWK="/usr/bin/awk"
GREP="/usr/bin/grep"

PRIMARY_DISPLAY="eDP1"

function displayChange {
    STATUS=${2}
    DIS=${1}
    POS=${3}
    
    notify-send "Display $DIS $STATUS"
    echo "Display $DIS $STATUS" >> /home/paramah/.display.log

    if [ $STATUS = "connected" ]; then
        $XRANDR --output $DIS --auto
        $XRANDR --output $DIS $POS $PRIMARY_DISPLAY
    fi

    if [ $STATUS = "disconnected" ]; then
        $XRANDR --output $DIS --off
    fi

}

function myDisplays {
	for D in = $($XRANDR |$GREP -w "${1}" |$GREP -v $PRIMARY_DISPLAY |$AWK '{print $1}')
	do
		if [ $D = "DP2" ]; then
            displayChange "DP2" "${1}" "--left-of"
		fi

		if [ $D = "HDMI1" ]; then
            displayChange "HDMI1" "${1}" "--right-of"
		fi
	done
}

myDisplays "disconnected"
myDisplays "connected"



