#!/bin/bash
#
# Script used by the pcoiphostswd to acquire an XAUTHORITY cookie.

# This script is run by the pcoiphostswd as root.
# It is stored in the directory the host SW executable is stored in (/usr/bin).
# The host SW daemon calls this script to provide some flexibility to the end-user.
# If the window manager looked for by this script is not active on the end-user's
# Linux distribution this file can be modified to acquire the XAUTHORITY value
# in other ways. This avoids having to recompile the host SW daemon.
#
# To use this file do the following:
#
# 1) If necessary, edit the statements below to acquire an XAUTHORITY value 
#    on the workstation running the pcoip_agent executable. 
#    The first place to start would be to modify the list of desktop manager 
#    process names to match your environment.
#
# 2) If this is not possible, the command 'xhost +LOCAL:' must be run from an
#    environment which does have X server authority to allow all local connections 
#    to the X server.
#

# Note that there are processes other than the desktop manager which could provide
# what we need.
#
# Try the following list of desktop manager process names
#
for DM in kded kdm_greet gnome-session gdmgreeter icewm 
do
    # find the desktop manager's process ID if possible
    export DM_PID=`ps -efw | grep $DM | grep -m 1 -v grep | awk '{print $2}'`
    if [ "x$DM_PID" != "x" ] ; then
        echo "Found $DM"
        # extract its environment variable
        export $(</proc/$DM_PID/environ tr \\0 \\n | grep -E '^XAUTHORITY=') 2>/dev/null
        echo "XAUTHORITY=$XAUTHORITY"
    fi
done
