#!/bin/sh
#
# loginservers:
# report a list of currently available X11 login servers
# for a given user
#
# usage: $0 <account>
#
# $Id$
# $HeadURL$
#
# CHP, 2008-01-21
#

HASACCOUNTON_BIN=/etc/itwm/bin/hasaccounton

XDMPING_BIN=/opt/yass/lib/yass/xdmping
XDMPING_OPTIONS="-t 1 -r 1 -v"

SLEEP_TIME=2s

#
# no user-serviceable parts below!
#

ACCOUNT=$1

if [ -z "$1" ]; then
	echo "usage error: give account name as first argument!" 1>&2
	exit 1
fi

MYNAME=`basename $0`
DIR=`mktemp -t -d`

if [ $? -ne 0 -o ! -d "$DIR" ]; then
	echo "error: could not create temp dir. exiting." 1>&2
	exit 2
fi

#echo dir $DIR

# "xdmping" all hosts in background. write answer to temporary files.

for H in `$HASACCOUNTON_BIN $ACCOUNT`; do
	$XDMPING_BIN $XDMPING_OPTIONS $H 2> /dev/null | grep "^$H[:\.]" > $DIR/$H 2> /dev/null &
done
sleep $SLEEP_TIME

# report non-empty result files.
# these hosts are willing to handle X11 sessions!

for H in `ls $DIR/`; do
	if [ -s "$DIR/$H" ]; then
		echo $H
	fi
done

# remove temp dir

cd $DIR/ && rm *
rmdir $DIR/
