#!/bin/ksh
#
# ident "@(#)utxlock.ksh	1.8 02/12/05 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# 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
#

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

# defaults
GNOME_ROOT=/usr/openwin/bin
GNOME_LOCK=${GNOME_ROOT}/xscreensaver-command
GNOME_LOCKARGS="-lock"

CDE_LOCK=/usr/dt/bin/dtaction
CDE_LOCKARGS="LockDisplay"

OTHER_LOCK=/usr/openwin/bin/xlock
OTHER_LOCKARGS="-mode blank"

CDE_ATOM=_DT_SM_PREFERENCES

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}
    exit 0
else
    # No override - do the defaults

    # Is it Gnome?
    if [ -x ${GNOME_LOCK} ]
    then
	${GNOME_LOCK} ${GNOME_LOCKARGS} > /dev/null 2>&1
	# if lock front-end returns good status, this is Gnome - this case
	# combines the test and the lock
	if [ $? = 0 ]
	then
	    exit 0
	fi
    fi

    # Is it CDE?
    if /usr/openwin/bin/xlsatoms -name ${CDE_ATOM} 2>/dev/null | grep ${CDE_ATOM} >/dev/null
    then
	# if the property exists, this is CDE
        if [ -x ${CDE_LOCK} ]
	then
	    ${CDE_LOCK} ${CDE_LOCKARGS}
	    exit 0
	fi
    fi

    # We dont know what it is, use generic X lock
    if [ -x ${OTHER_LOCK} ]
    then
	${OTHER_LOCK} ${OTHER_LOCKARGS} &
	exit 0
    fi

    # We didn't find xlock?!?
    print -u2 "${OTHER_LOCK} not found!"
    logger "$0($(whoami)): ${OTHER_LOCK} not found!"
    exit 2
fi

exit 0
