#!/bin/bash

getXuser() {
       user=`finger| grep -m1 ":$displaynum " | awk '{print $1}'`
       if [ x"$user" = x"" ]; then
               user=`finger| grep -m1 ":$displaynum" | awk '{print $1}'`
       fi
       if [ x"$user" != x"" ]; then
               userhome=`getent passwd $user | cut -d: -f6`
               export XAUTHORITY=$userhome/.Xauthority
       else
               export XAUTHORITY=""
       fi
}
# end of getXuser from /usr/share/acpi-support/power-funcs
#

checkVGAStatus()
{
    status=`xrandr -q`

    if [ $(echo $status | grep -q "VGA disconnected" ; echo $?) -eq 0 ]
    then
        return 0
    else
        if [ $(echo $status | grep -q "VGA connected (" ; echo $?) -eq 0 ]
        then
            return 1
        else
            if [ $(echo $status | grep -q "VGA connected" ; echo $?) -eq 0 ]
            then
                return 2
            fi
        fi
    fi
}

for x in /tmp/.X11-unix/*; do
   displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
   getXuser;
   if [ x"$XAUTHORITY" != x"" ]; then
       export DISPLAY=":$displaynum"
       checkVGAStatus;

       case $? in
           2 ) xrandr --output VGA --off;;     # if VGA is connected&active, disable it
           * ) xrandr --auto;;	                # otherwise, let xrandr figure it out
       esac

   fi
done
