vga() {
    xrandr | grep -q "^VGA1 connected"
}
a=2
while true
do
 b=$(vga)
 if [ "$a" != "$b" ]; then
  echo "something changed"
  if vga; then
   echo "docked"
   xrandr --output "DVI1" --auto
   xrandr --output "VGA1" --auto
   xrandr --output "LVDS1" --off
   xrandr --output "LVDS1" --pos 0x821 
  else
   echo "undocked"
   xrandr --output "DVI1" --off
   xrandr --output "LVDS1" --auto
  fi
 fi
 a=$b
 sleep 5
done
