#!/bin/sh
#
# @(#)$Id: xdladm.sh,v 1.3.2.1.8.4.2.1 1996/11/13 14:51:31 cvsusr Exp $
#
# Copyright 1993 Dynamic Software AB
# This is unpublished proprietary source code.
#
# Description: administration script for X-locking
#
# Usage:
#	xdladm -h HOST ...	- work with HOST, rather than localhost
#	xdladm -u/-r display 	- unlock or reset a locked display
#	xdladm -s/-S		- show some/all parameters
#	xdladm -X on|off	- turn daemon (autolock) on/off
#	xdladm -z SEC		- sleeptime for daemon
#	xdladm [-H]		- list locked displays, users etc [+header]
#	xdladm -d		- as above, but just the names
#	xdladm -v ...		- be verbose
#	xdladm -m PARAMS	- set parameters, on the form "Param:Value"
#				  e.g. Timeout:20 sets timeout limit to 20

DASHLINE="------------------------------------------------------------"
progname=xdladm

# Try to run some operations in parallell, at most this any processes
# at at time.
#
Concur=10

# Get BoKS locations
for d in DIR etc sbin lib  var tmp; do
    eval BOKS_$d="`boksdir $d`"
    export BOKS_$d
done
test -z "$BOKS_lib" && { echo "Must be run in a BoKS environment" >&2; exit 1; }
PATH="$BOKS_lib:$BOKS_sbin:$BOKS_bin:$PATH"; export PATH

TMPF=$BOKS_tmp/x$$x
XLOCKS=$BOKS_var/XLOCKS

trap "{ rm -f ${TMPF}* ; exit 1; }" 1 2 3 15

#
# Get MLANG and friends
#
if [ -z "$MLANG" ]; then
	eval "`grep MLANG $BOKS_etc/ENV`"
fi
if [ -z "$MENUASCII" ]; then
	eval "`grep MENUASCII $BOKS_etc/ENV`"
fi

# Set default values
#
if [ -z "$MLANG" ]; then
	MLANG=eng
	export MLANG
fi
if [ -z "$MENUASCII" ]; then
	MENUASCII=7BIT
	export MENUASCII
fi

# Read messages
eval "`msglist $BOKS_lib/mess ${progname}.sh 2>/dev/null`"

# Read messages
eval "`msglist $BOKS_lib/mess share.sh 2>/dev/null`"


# Function to show a parameter from a file
# $1 = parameter  $2 = description  $3 (optional) filename
#
do_show() {
	ovr=$MESS_ovr
	test -z "$1" && return
	file=${3:-${TMPF}}
	line=`grep "^Boks.${1}:" $file 2>/dev/null`
	value=`expr "$line" : "[^:]*:\(.*\)" 2>/dev/null`
	test "$value" = "yes" && value=${MESS_yes:-yes}
	test "$value" = "no"  && value=${MESS_no:-no}
	grep "^Boks.${1}Override:" $file >/dev/null 2>&1 && ovr=
	test -n "$verbose" && printf "%-50s %-5s" "${2:-$1}" "$ovr"
	echo "$value"
}


# Read options
#
Host=
USAGE="$progname [-h host] [-r|-u display] [-sSHvd] \
[-X ${MESS_on:-on}|${MESS_off:-off} ] \
[-z seconds] [-m parameter:value ... ]"
if set -- `getopt "h:Hxr:u:msSvX:z:d" $*` ; then
	while [ -n "$1" ]; do
		case $1 in
		-H)	Header=yes; shift;;
		-h)	Host="$2"; shift 2;; 
		-x)	dontpropagate=yes; shift;;
		-r|-u)	UnlockMode="$1"; Display="$2"; shift 2;;
		-m)	Modify=yes; shift;
			test "--" = "$1" && shift; # Added by getopt
			break;;
		-s)	Show=yes; shift;;
		-S)	ShowAll=yes; shift;;
		-v)	verbose=yes; shift;;
		-X)	Daemon="$2"; shift 2;;
		-z)	Sleep="$2"; shift 2;;
		-d)	NamesOnly=yes; shift;;
		--)	shift;;
		*)	echo "$USAGE"; exit 1;;
		esac
	done
	ModifyArgs="$*" # List of parameter:value pairs. No whitespace!
else
	echo $USAGE; exit 1
fi

# Handle missing number for display name
#
if expr "$Display" : '.*:.*' > /dev/null 2>&1 ; then :
else
	Display="${Display}:0"
fi

# Fiddle with the daemon? (on/off & sleep time)
#
if [ -n "$Sleep$Daemon" ]; then
	test -z "$Host" && Host=`/usr/bin/uname -n`
	if [ "$Daemon" = "$MESS_on" -o "$Daemon" = on ]; then
		Daemon=on
	elif [ "$Daemon" = "$MESS_off" -o "$Daemon" = off ]; then
		Daemon=off
	elif [ -n "$Daemon" ]; then
		echo "$USAGE"; exit 1
	fi
	if [ -n "$Sleep" ]; then
		test "$Sleep" -lt 15 && Sleep=15
		args="-EXD_SLEEP=$Sleep"
	fi
	test -n "$Daemon" && args="$args -EXD=$Daemon"
	i=0
	for host in `lh -uh $Host 2>/dev/null`; do
		cadm -h $host $args 2>/dev/null &
		i=`expr $i + 1`
		test $i -ge $Concur && { i=0; wait; }
	done
	wait
	exit 0
fi


# Unlock or Reset ?
#
if [ -n "$UnlockMode" ]; then
	test -z "$Host" && Host=ALL
	remote=
	if [ -z "$Display" ]; then
		echo "${MESS_nodisplay:-No display specified}"
		exit 1
	fi
	line="`grep $Display $XLOCKS 2>/dev/null | line`"
	if [ -z "$line" ]; then
		test -n "$dontpropagate" && exit 1
		i=0;j=0
		for host in `lh -uh $Host 2>/dev/null`; do
			cadm -N -h $host -l -f XLOCKS 2>/dev/null \
				> ${TMPF}$j &
			j=`expr $j + 1`
			i=`expr $i + 1`
			test $i -ge $Concur && { i=0; wait; }
		done
		wait	
		if [ $j -gt 0 ]; then
			cat ${TMPF}?* >> $TMPF
			rm -f ${TMPF}?*
		fi
		line="`grep $Display ${TMPF} 2>/dev/null | line`"
		rm -f ${TMPF}
		remote=true
	fi
	if [ -n "$line" ]; then
		echo "$line" | while read dpy usr pid host; do
			if [ -n "$remote" ]; then
				cadm -N -h $host -s "$progname -x \
					$UnlockMode $Display" 2>/dev/null
			else
				if [ "$UnlockMode" = "-r" ]; then
					sig=-USR1
				else
					sig=
					# This is the local machine
					# Remove the lock-line
					grep -v "^$dpy " $XLOCKS \
						2>/dev/null > ${TMPF}
					cp ${TMPF} $XLOCKS
				fi
				kill $sig $pid >/dev/null 2>&1
			fi
		done
		exit 0
	fi
	[ -n "$verbose" ] && printf "${MESS_notlocked:-Display %s is not locked}\n" "$Display"
	exit 1
fi

test -z "$Host" && Host=`/usr/bin/uname -n`

# Show anything?
#
if [ -n "${Show}$ShowAll"  ]; then
	host=`lh -uh $Host | line`
	cadm -N -h "$host" -l -f Xdefaults >> ${TMPF} 2>/dev/null

	do_show "Timeout"      "${MESS_Timeout:-Timeout}"
	do_show "Warntime"     "${MESS_Warntime:-Warntime}"
	do_show "BeepInterval" "${MESS_BeepInterval:-Beep Interval}"
	do_show "FastBeep"     "${MESS_FastBeep:-Fast Beep}"
	do_show "Volume"       "${MESS_Volume:-Volume}"
	do_show "Transparent"  "${MESS_Transparent:-Transparent}"
	do_show "Log"          "${MESS_Log:-Log}"
	do_show "Retries"      "${MESS_Retries:-Retries}"
	do_show "Wait"         "${MESS_Wait:-Wait}"
		
	if [ -z "$ShowAll" ]; then
		rm -f ${TMPF}
		exit 0
	fi

	do_show "Font"         "${MESS_Font:-Font}"
	do_show "Ascii"        "${MESS_Ascii:-Ascii}"
	do_show "Sensitive"    "${MESS_Sensitive:-Sensitive}"
	do_show "Access"       "${MESS_Access:-Access}"
	do_show "IgnoreMotion" "${MESS_IgnoreMotion:-IgnoreMotion}"
	do_show "PromptLinger" "${MESS_PromptLinger:-Prompt Linger}"

	rm -f ${TMPF}
	exit 0
fi

# Modify parameters.
#
if [ -n "$Modify" ]; then
	for host in `lh -uh $Host`; do
		cadm -N -l -f Xdefaults -h $host > ${TMPF} 2>/dev/null
		args=
		for i in $ModifyArgs; do
			test "--" = "$i" && continue # Leftovers from getopt
			param=`expr "$i" : "\([^:]*\).*" 2>/dev/null`
			value=`expr "$i" : "[^:]*:\(.*\)" 2>/dev/null`
			line=`grep "^Boks.${param}:" ${TMPF} 2>/dev/null`
			test -n "$line" && args="$args -d $line"
			test "$value" = "$MESS_yes" && value=yes
			test "$value" = "$MESS_no"  && value=no
			# Will this asterisk cause problems?
			args="$args -a Boks*$param:$value"
		done
		test -n "$args" && cadm -N -fXdefaults -h$host $args 2>/dev/null
	done
	rm -f ${TMPF}
	exit 0
fi

# Else show locked displays
#
i=0;j=0
for host in `lh -uh $Host`; do
	cadm -N -h $host -l -f XLOCKS > ${TMPF}$j 2>/dev/null &
	i=`expr $i + 1`
	j=`expr $j + 1`
	test $i -ge $Concur && { i=0; wait; }
done
wait
if [ $j -gt 0 ]; then
	cat ${TMPF}?* >> ${TMPF}
	rm -f ${TMPF}?*
fi
if [ -n "$Header" ]; then
	echo "${MESS_listhdr:-Display      Name         Pid    Host}"
	echo "$DASHLINE"
fi
if [ -n "$verbose" -a ! -s ${TMPF} ]; then
	echo "(${MESS_nonlocked:-no displays locked})"
elif [ -n "$NamesOnly" ]; then
	awk '{print $1;}' ${TMPF}
else
	awk '{printf "%-12s %-12s %-6s %-12s\n", $1, $2, $3, $4; }' ${TMPF}
fi
rm -f ${TMPF}

