#!/bin/bash

displaynum=`ls /tmp/.X11-unix/* | sed s#/tmp/.X11-unix/X##`
display=":$displaynum.0"
export DISPLAY=":$displaynum.0"
export XAUTHORITY=$(ps -C Xorg -f --no-header | sed -n 's/.*-auth //; s/ -[^ ].*//; p')

xrandr -q --verbose | grep connected | while read line
do
  connected=$(echo $line | grep " connected")
  output=$(echo "$connected" | head -n 1 | sed -e "s/\([a-zA-Z0-9]\+\) connected.*/\1/")
  if [ ! -z "$output" ] ; then
    xrandr --output $output --auto
  fi
done
