#!/bin/ksh -p
#
# ident "@(#)utxlock.ksh	1.17 08/08/25 SMI"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# This command is intended to be run by utaction on a disconnect
# event.  It will issue an appropriate screen lock for the current
# windowing environment.
#
# Environments supported (and their default lock programs) are:
# CDE/"dtaction LockDisplay"
# Gnome/"xscreensaver-command -lock"
# other/xlock
#
# If an environment variable SUN_SUNRAY_UTXLOCK_PREF is set,
# if its value is "none" or empty/NULL, no utaction will not be
#       launched to invoked this program at all
# if its value is some other string, that string will be executed on
#       a disconnect instead of the default programs listed above, in
#       order to execute a user's preferred screen lock
#
# The safe/correct place to set this environment variable is in
# $HOME/.dtprofile
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/X11R6/bin:/usr/openwin/bin

# GNOME is installed at a different location on SLES10.
if [ -d /opt/gnome/bin ]; then
    PATH=$PATH:/opt/gnome/bin
fi
#
# Solaris 10 Trusted Extension guard
#
ORIGIN=`/usr/bin/dirname $0`
UTIL_LIB=${ORIGIN:-/opt/SUNWut/lib}/../lib/support_lib/util_lib
. $UTIL_LIB
FailExecInLocalZoneOnTx 
#
# Solaris 10 Trusted Extension guard 
#

function notfndhndlr {
    print -u2 "${CMD} not found!"
    logger "$0($(whoami)): ${CMD} not found!"
}

# Return success iff xscreensaver is running (i.e. there is a window
# for application class XScreenSaver)
function is_xscreensaver_running {
    X_WININFO=xwininfo
    [[ -n "$XSCREENLOCKCMD" ]] &&
    [[ -x "$XSCREENLOCKCMD" ]] &&
    $X_WININFO -root -children | grep XScreenSaver >/dev/null 2>&1
}

function lock_xscreensaver {
    ${XSCREENLOCKCMD} -lock > /dev/null 2>&1
}

# Return success iff gnome-screensaver is running
# (i.e. gnome-screensaver-command --query succeeds)
function is_gnome_screensaver_running {
    [[ -n "$GNOMESCREENLOCKCMD" ]] &&
    [[ -x "$GNOMESCREENLOCKCMD" ]] &&
    "$GNOMESCREENLOCKCMD" --query >/dev/null 2>&1
}

function lock_gnome_screensaver {
    "$GNOMESCREENLOCKCMD" --lock
}

# Return success iff CDE is the desktop environment
function is_CDE {
    # if the property exists, this is CDE
    CDE_ATOM=_DT_SM_PREFERENCES
    [[ -n $CDELOCKCMD ]] &&
    [[ -x $CDELOCKCMD ]] &&
    /etc/opt/SUNWut/basedir/lib/utxprop -p ${CDE_ATOM} >/dev/null 2>/dev/null
}

function lock_CDE {
    ${CDELOCKCMD} LockDisplay
}

function is_xlock {
    [ -n ${XLOCKCMD} ] &&
    [ -x ${XLOCKCMD} ]
}

function lock_xlock {
    ${XLOCKCMD} -mode blank &
}

# defaults

if [ -n "${SUN_SUNRAY_UTXLOCK_PREF+foo}" ]
then
    if [ -z "${SUN_SUNRAY_UTXLOCK_PREF}" ]
    then
	exit 0
    else
	CMD="${SUN_SUNRAY_UTXLOCK_PREF}"
    fi
    trap notfndhndlr ERR
    set -e
    ${CMD}
else
    # No override - do the defaults
    # Either gnome-screensaver or xscreensaver is the typical
    # screenlock for Gnome.  Unfortunately different Linux distros
    # install them in different places.

    XSCREENLOCKCMD=`command -v xscreensaver-command`
    GNOMESCREENLOCKCMD=`command -v gnome-screensaver-command`
    XLOCKCMD=`command -v xlock`
    CDELOCKCMD=/usr/dt/bin/dtaction

    if is_xscreensaver_running; then
	lock_xscreensaver
    elif is_gnome_screensaver_running; then
	lock_gnome_screensaver
    elif is_CDE; then
	lock_CDE
    elif is_xlock; then
	# We dont know what it is, use generic X lock
	lock_xlock
    else
	# We didn't find xlock?!?
	COMPLAINT="no screenlock program available"
	print -u2 "$0: ${COMPLAINT}"
	logger "$0($(whoami)): ${COMPLAINT}"
	exit 2
    fi
fi
exit 0
