#!/bin/bash
#
# Script used by the pcoip_agent application to lock the screen when a PCoIP
# session is disconnected.
#
# Release 4.0.6 and later invoke this script when tying to lock the screen,
# which is done when a session is disconnected and the host SW is configured
# to lock the screen when a PCoIP session becomes disconnected. This script
# is stored in the directory the host SW executable is stored in (/usr/bin).
# The host SW calls this script to provide some flexibility to the end-user.
# If the screen saver invoked by this script is not installed on the end-users
# Linux distribution this file can be easily modified to call a program that
# locks the screen. This avoids having to recompile the host SW executable.
#
# To use this file do the following:
#
# 1) If necessary, edit the statements below to signal a screensaver installed 
#    on the workstation running the pcoip_agent executable
#
# The lockscreen process is done by signaling to the active screensaver
# using DBus. This script is called by the Host Software, which is called
# by a the pcoip daemon, so it doesn't inherit the same environment that
# a shell started by the desktop manager does.
#
# To lock the screen, we need to acquire the correct DBUS_SESSION_BUS_ADDRESS
# We'll do this by finding the desktop manager process, parsing the
# environment it is running in, and extracting the variables we need.
#
# There are processes other than the desktop manager which could provide
# what we need, but this also tells us something about which screensaver to use.

export DM_NAME="Unknown"
# Try the following list of desktop manager process names
for DM in kded gnome-session 
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
        export DM_NAME=$DM
        # extract its environment variables into our environment
        export $(</proc/$DM_PID/environ tr \\0 \\n | grep -E '^DBUS_SESSION_BUS_ADDRESS=') >/dev/null
        export $(</proc/$DM_PID/environ tr \\0 \\n | grep -E '^XAUTHORITY=') >/dev/null
    fi
done

if [ "$DM_NAME" == "gnome-session" ] ; then
    export GNOME_VERSION=`gnome-session --version | awk '{print $2}' | awk -F"." '{print $1}'`
    if [ "x$GNOME_VERSION" == "x3" ] ; then
        export DM_NAME="gnome-session-3"
    fi
fi

# kdesktop_lock is present in CentOS 5.x and works in KDE and Gnome.
# If present, use it.
if [ -e /usr/bin/kdesktop_lock ] ; then
    export CMD="exec /usr/bin/kdesktop_lock --forcelock"

# KDE
# When KDE is running, signaling org.freedesktop.Screensaver should work.
elif [ "$DM_NAME" == "kded" ] ; then
    export CMD="exec dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Lock"

# Gnome 3
# This will signal gnome-screensaver on systems running gnome version 3
elif [ "$DM_NAME" == "gnome-session-3" ] ; then
    export CMD="dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock"

# SLED 11 SP3, Centos 6.x - non KDE
# If qdbus is present, it provides an easy way to list available DBus services.
# Parse this list for a screensaver, and signal "lock" to it.
# This will generally require that gnome-screensaver is running (it will
# register with DBus).
elif [ -e /usr/bin/qdbus ] ; then
    export CMD="exec qdbus `qdbus | grep -m 1 -i screensaver` /ScreenSaver Lock"

# Gnome
# This will signal gnome-screensaver on systems without qdbus.
elif [ "$DM_NAME" == "gnome-session" ] ; then
    export CMD="dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock"

else
# The default - If you're reaching this, you probably need to determine or install a
# lockscreen program and modify this file.
# xlock should be installed on most systems (comes with X11R6)    
    export CMD="exec xlock -mode blank"
fi

# Run it.
$CMD &
