#!/bin/bash

changeMonitors="false";
dontChangeAudio="false";
noPresentationMode="false";

while [ -n "$1" ]; do
	[ "-c" = "$1" -o "--changeMonitors"  = "$1"    ] && changeMonitors="true";
	[ "-n" = "$1" -o "--dontChangeAudio" = "$1"    ] && dontChangeAudio="true";
	[ "-p" = "$1" -o "--noPresentationMode" = "$1" ] && noPresentationMode="true";
	shift;
done

Ports=$(xrandr | grep -P "DisplayPort-[0-9]+ connected" | sed -e "s/ .*$//");

myArr+=($Ports);

if [ -z "${myArr[1]}" ]; then
        echo "Keine zwei Monitore gefunden. Exit" 1>&2;
        exit 2
fi


if [ $changeMonitors = "true" ]; then
	dp1=${myArr[0]};
	dp2=${myArr[1]};
else
        dp1=${myArr[1]};
        dp2=${myArr[0]};
fi

/usr/bin/xrandr \
--output  $dp1  --primary --scale 1.0 --mode 1920x1080 --rate 60 --pos 0x0    --rotate normal \
--output  $dp2            --scale 1.0 --mode 1920x1080 --rate 60 --pos 1920x0 --rotate normal \
--output eDP --off

xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor$dp1/workspace0/last-image -s /home/fastix/Bilder/Wallpaper/papagei-gespiegelt.jpg
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor$dp2/workspace0/last-image -s /home/fastix/Bilder/Wallpaper/Papagei.jpg

if [ $dontChangeAudio = "false" ]; then
	hdmi_audio=$(wpctl status | grep 'Audio.*HDMI' | head -n1 | sed -E 's/^[^0-9]*([0-9]{1,3}).*$/\1/');

	if [ -n "$hdmi_audio" ]; then
		wpctl set-default $hdmi_audio
	else
        	echo "Kein HDMI-Output gefunden. Wird also nicht aktiviert." 1>&2;
	        exit 2
	fi

fi

if [ $noPresentationMode = "false" ]; then
	xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -s true
fi
