#!/bin/false	# This is a set of ksh functions for use inside utdtsession
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# @(#)xdisplayutil.sh	1.22 04/01/14
#
# Utilities used to manipulate the Xservers and Xconfig files
#

# Reset list of locks currently in use.
function startlocks {
	_LOCKS=""
}

# Keep track of locks currently in use. Makes it easier to
# clear them on exit
function addlock {
	if [ -z "$_LOCKS" ]
	then
		_LOCKS="$1"
	else
		_LOCKS="$1 $_LOCKS"
	fi
}

# Used to clear locks on exit
function endlocks {
	if [ -n "$_LOCKS" ]
	then
		/bin/rm -f $_LOCKS
	fi
}

# Conditionally enter a lock
function lock {
	set -u
	_lockFileName=$1
	_lockProg=${2:-$SUNWUTLIBDIR/lockprog}
	if $_lockProg -r 2500 -n 0.2$RANDOM -l 36000 -c "$$@$(uname -n)" \
		$_lockFileName
	then
		addlock $_lockFileName
	else
		print -u2 "Lock file <$_lockFileName> exists"
		return 1
	fi
	# XXX cannot put (trap "rm -f lockfilename" 0) here because trap 0
	# XXX would execute when this functions returned
	return 0
}

# Used to clear locks and exit
function Exit {
	endlocks
	exit $1
}
