#!/bin/sh
###########################################################################
# Output DISPLAY and XAUTHORITY for the current user's X screen.
# A root user can then use those to access the user's display.
# Usage: export $(get-display)
#
# Copyright (C) 2012 Alkis Georgopoulos <alkisg@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
###########################################################################

exit_if_found() {
    # Check if DISPLAY/XAUTHORITY are valid.
    # XAUTHORITY may even be empty, in which case it must not be exported.
    test -n "$DISPLAY" || return
    export DISPLAY XAUTHORITY
    test -n "$XAUTHORITY" || unset XAUTHORITY
    if xwininfo -root -size >/dev/null 2>&1; then
        echo "DISPLAY=$DISPLAY"
        test -n "$XAUTHORITY" && echo "XAUTHORITY=$XAUTHORITY"
        exit 0
    else
        unset DISPLAY XAUTHORITY
    fi
}

find_xauthority() {
    # Plan A: try GNOME/GDM
    XAUTHORITY=$(ls /var/run/gdm/auth-for-$USER-*/database &> /dev/null)
    [ -s $XAUTHORITY ] && return

    # Plan B: try KDE
    UID="$(getent passwd "$USER" | cut -d: -f3)"
    XAUTHORITY=$(ls /tmp/kde-$USER/xauth-$UID-$(echo $DISPLAY | tr ':' '_') &> /dev/null)
    [ -s $XAUTHORITY ] && return

    # Plan C: try ~/.Xauthority
    HOME="$(getent passwd "$USER" | cut -d: -f6)"
    XAUTHORITY=$(ls $HOME/.Xauthority &> /dev/null)
    [ -s $XAUTHORITY ] && return

    #Plan D: give up!
    unset XAUTHORITY
}

# Plan A: check if the variables are already set
exit_if_found

# Plan B: check if we can get the info from some running process
for p in $(pgrep -x 'ldm|gdmgreeter|kdm_greet|lxdm-greeter-gt|dbus-daemon'); do
    # The first DISPLAY= is only there to prevent a possible syntax error
    export DISPLAY="$DISPLAY" $(tr '\0' '\n' 2>/dev/null < /proc/$p/environ |
        egrep '^DISPLAY=|^XAUTHORITY=')
    exit_if_found
done

# Plan C: try loginctl
if [ -x /usr/bin/loginctl ]; then
    for session in $(loginctl --no-legend | awk '{ print $1 }'); do
        while read line; do
            eval $line
            case "$line" in
                Name*)
                    USER=$Name
                    ;;
                Display*)
                    DISPLAY=$Display
                    if [ -n "$DISPLAY" ]; then
                        find_xauthority
                        exit_if_found
                    fi
                    ;;
            esac
        done << EOF
$(loginctl show-session $session)
EOF
    done
fi

# Plan D: try ck-list-sessions
if [ -x /usr/bin/ck-list-sessions ]; then
    while read var equals value; do
        case "$var" in
            Session*)
                unset USER DISPLAY XAUTHORITY
                ;;
            unix-user)
                value=${value%\'}
                USER=${value#\'}
                ;;
            x11-display)
                value=${value%\'}
                DISPLAY=${value#\'}
                if [ -n "$DISPLAY" ]; then
                    find_xauthority
                    exit_if_found
                fi
                ;;
        esac
    done <<EOF
$(ck-list-sessions)
EOF
fi

# Plan E: give up!
echo "Could not detect or access the active display" >&2
echo "DISPLAY=
XAUTHORITY="
exit 1
