#!/bin/bash

background_stuff() {
	if [[ "$1" -eq 1 ]]; then sleep 1; else sleep 0.5; fi
	export DISPLAY=
	export DP_SETTINGS=

	while [ "$DISPLAY" = "" ]
	do
		cd /tmp/.X11-unix && for x in X*; do
		if [ ! -e "$x" ]; then continue; fi
		export DISPLAY=":${x#X}"

		USER_NAME=$(who | awk -v vt="$DISPLAY" '$0 ~ vt {print $1}')
		USER_ID=$(id -u "$USER_NAME")
		PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"

		# from https://wiki.archlinux.org/index.php/Acpid#Laptop_Monitor_Power_Off
		export XAUTHORITY=$(ps -C Xorg -f --no-header | sed -n 's/.*-auth //; s/ -[^ ].*//; p')

		if [[ "$1" -eq 1 ]]
		then
			# Read custom WIDTHxHEIGHT from dock-hotplug.conf
			export DP_SETTINGS=$(cat /etc/dock-hotplug.conf)
			if [[ -n $DP_SETTINGS ]]; then
				export DP_SETTINGS="--mode "$DP_SETTINGS" --panning "$DP_SETTINGS"+0+0 --pos 0x0"
			else
				export DP_SETTINGS="--auto"
			fi
			xrandr --output DP-0 $DP_SETTINGS --output DSI-0 --off
			xinput disable 6 #disable touch screen
			sudo -u "$USER_NAME" xrandr --output DP-0 $DP_SETTINGS --output DSI-0 --off
			sudo -u "$USER_NAME" xinput disable 6 #disable touch screen
			sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 1 off
			sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 output:hdmi-stereo
		else
			xrandr --output DSI-0 --mode 720x1280 --rotate left --panning 1280x720+0+0 --pos 0x0 --dpi 237
			xinput enable 6 #enable touch screen
			sudo -u "$USER_NAME" xrandr --output DSI-0 --mode 720x1280 --rotate left --panning 1280x720+0+0 --pos 0x0 --dpi 237
			sudo -u "$USER_NAME" xinput enable 6 #enable touch screen
			sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 1 HiFi
			sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile 0 off
			gsettings set com.ubuntu.user-interface scale-factor "{'': 8, 'DSI-0': 10, 'DP-0': 8}"
		fi
		done
		sleep 1
	done
}

if grep -q 1 "/sys/class/switch/dp/state"; then
		background_stuff 1 & disown
else
		background_stuff 0 & disown
fi

