#!/bin/sh
# $Revision: 1.21 $

msgfile=uxsgidesktop
errmsg=`gettxt ${msgfile}:445 "Usage: %s [-f] [-display <DISPLAYNAME>]"`
XCONFIRM=/usr/bin/X11/xconfirm
HOST=`hostname`

FORCEIT=0
while [ "$1" != "" ]
do
        case $1 in
         -display)    DISPLAY=$2; break ;;
	 -f) FORCEIT=1;;
	*) printf "${errmsg}\n" "$0"; exit;;
        esac
        shift
done


if [ -z "$DISPLAY" ]; then
    printf "%s\n" "`gettxt ${msgfile}:446 'Terribly sorry, but non-graphics control is not available'`" 1>&2
    exit 1
fi
if [ "$REMOTEHOST" -a "$REMOTEHOST" != "$HOST" ]; then
    str=`gettxt ${msgfile}:447 "-t 'Do you want to log out from '%s' now?'"`
    TEXT=`printf "${str}" "${DISPLAY}"`
else
    TEXT=`gettxt ${msgfile}:448 "-t 'Do you want to log out now?'"`
fi

CANCEL_LABEL=`gettxt ${msgfile}:438 "No"`
DOIT_LABEL=`gettxt ${msgfile}:437 "Yes"`
CANCEL="-B '$CANCEL_LABEL'"
DOIT="-b '$DOIT_LABEL'"

if [ $FORCEIT = 0 ] ; then
#   if the user wants the option of confirming
    CMD="$XCONFIRM \
    -display $DISPLAY -header Confirm -icon question $CANCEL $DOIT $TEXT"
    
#   Get the user's command
    REPLY="`eval $CMD`"
    case "$REPLY" in
        $DOIT_LABEL)   ;;
        $CANCEL_LABEL)  exit 1;;
        *)              printf "`gettxt ${msgfile}:449 'Unknown reply %s'`\n" "$REPLY"
			exit 99;;
        esac
fi 

# if 4Dwm is running, tell 4Dwm that the session is ending
if /usr/bin/X11/tellwm -t end_session >/dev/null 2>&1
then
     /usr/bin/X11/tellwm end_session >/dev/null 2>&1
#    in case 4Dwm was killed (tellwm thinks 4Dwm runs) remove _SGI_TELL_WM
#    so that at least the next attempt to log out won't fail
     /usr/bin/X11/xprop -display $DISPLAY -root -remove _SGI_TELL_WM
else
/usr/bin/X11/xprop -display $DISPLAY -root -remove _SGI_SESSION_PROPERTY
fi
