#!/bin/sh -f
#
# start up WinCenter, and wait until its ready to accept rsh commands
# 
# usage: wncwait NThost <wincenter arguments>
#
# returns 0 once its finally up, otherwise non-zero
#

username=$USER
wnccmd="wincenter -display $DISPLAY -depth 4 -geometry =1024x768+20+20"

# how many attempts to be made
maxtries=10

# duration of sleep between checks
delay=3

case $# in
0)
    echo Usage: `expr //$0 : '.*/\(.*\)'` 'NT-hostname [wincenter arguments]'
    exit 1
    ;;
1)
    hostname=$1
    ;;
*)
    hostname=$1
    wnccmd=
    shift
    ;;
esac

# first see if its even necessary
case `wnccheck $hostname` in
yes)
    exit 0
    ;;
*)
    ;;
esac

# start up WinCenter

if [ "$wnccmd" ] ; then
    response=`rsh $hostname $wnccmd`
else
    response=`rsh $hostname wincenter $*`
fi

case $response in
*started*)
    ;;
*)
    echo "Failed to start up WinCenter: $response"
    exit 1
    ;;
esac

# now wait till rsh can work
#

tries=0
while true
do
    case `wnccheck $hostname` in
    yes)
	exit 0
	;;
    no)
	tries=`expr $tries + 1`
	sleep $delay
	;;
    esac

    if [ $tries -gt $maxtries ] ; then
	echo "Timed out waiting for user to log in"
	exit 1
    fi
done

