#!/bin/bash

# WindowMaker-Lock

# (c) 2002 Alexey Voinov <voins@voins.program.ru>
# Licence: GPL

# Launch the default X locker. Now searches for xlock and for xscreensaver
# xscreensaver locker is preferred.
# if there's no xscreensaver daemon running then one will be started
# If no xscreensaver found xlock is used.

WMLOCKCONFIG=/etc/X11/wmlocker
WMLOCKLOCAL=~/.wmlocker
XLOCKNAME=""
XSSNAME=""

progname=xscreensaver
for pathtoprog in `echo $PATH | tr ':' ' '`; do
	if test -x "$pathtoprog/$progname"; then
		XSSNAME="$pathtoprog/$progname"
		break
	fi
done

progname=xlock
for pathtoprog in `echo $PATH | tr ':' ' '`; do
	if test -x "$pathtoprog/$progname"; then
		XLOCKNAME="$pathtoprog/$progname"
		break
	fi
done

[ -r $WMLOCKCONFIG ] && source $WMLOCKCONFIG
[ -r $WMLOCKLOCAL ] && source $WMLOCKLOCAL

if test -z "$WMLOCKNAME"; then
	if test -n "$XSSNAME"; then
		if ! ps auxwww | grep [x]screensaver > /dev/null 2>&1; then
			$XSSNAME -no-splash > $HOME/.xscreensaver.log 2>&1 &
		fi
		WMLOCKNAME="$XSSNAME-command -lock"
	else if test -n "$XLOCKNAME"; then
			WMLOCKNAME="$XLOCKNAME"
			[ -z "$WMLOCKOPT" ] && WMLOCKOPT="-allowroot -usefirst"
		else
			WMLOCKNAME="xmessage \"no locker found\""
			WMLOCKOPT=""
		fi
	fi
fi

$WMLOCKNAME $WMLOCKOPT "$@"

