#!xchat
# @(#) Login.PortSelUnix V1.0 Tue Sep  1 14:57:05 1992 (Bob Denny)
#
# NOTE: Untested with xchat V1.1. Taken from DECUS UUCP.
#
# From: "Kent C. Brodie" <moocow!brodie@CSD4.MILW.WISC.EDU>
#	uucp:	{uunet!marque,csd4.milw.wisc.edu}!moocow!brodie  
# special script for "uwmcsd4", have to go through a port selector (and then
# log in via standard Unix procedures).  
#
# Also included is the ability to wait in the port selector queue.
# Be forwarned that the debug log can get pretty big depending on 
# how many times you "wait" in the queue.
# (C) 1989 Kent C. Brodie - Medical College of Wisconsin

# P0 is systemname , P1 is username, P2 is password. 

	zero

# send a CR to get the selector's attention.  Sleep a little bit
# due to large login text of selector.   It sends "Which System?"
# when it's ready. 

getprtslct:
	count
	ifgtr	noprtslct	6
	break
	send	\r
	sleep	2000
	flush
	expect	prtslctok	ystem?
	timeout	getprtslct	15000

noprtslct:
	logerr	Sent cr, no "Which System?" from port selector
	failed

# Send the system name.  We either get "OK" (connected), or we
# get "No ports available, would you like to wait?" (wait in queue)

prtslctok:
	zero
	sendstr	0
	send	\r
	expect	connected	OK
	expect	prtslctwait	wait?
	timeout	noconnect	10000

# Usually we get "nn Your place in queue" messages.  JUST in case we
# get a free port right away, check for 'Are you ready?' as well.

prtslctwait:
	zero
	send	Y\r
	expect	prtslctque	queue
	expect	prtslctrdy	ready?
	timeout	prtwaitbad	70000

prtwaitbad:
	logerr	Sent "Y" to wait in queue, did not get valid response.
	failed

# Here's where we wait in the queue.  The port selector sends us a status
# message about once a minute.  We either get "nn Your place in queue"
# or we get "System Available.  Are you Ready?". 
# If something goes wrong, we time out waiting for either response.
# The reason we don't sleep for 40-50 seconds is because as SOON as the
# port is ready, it informs us.  If we wait too long, it drops us.
# This setup is laid out for a maximum of 20 "tries" which is ABOUT
# 20 minutes.   Note:  This constant retrying can make log files
# kind of big....

prtslctque:
	count
	ifgtr	prtslcttry	20
	expect	prtslctque	queue
	expect	prtslctrdy	ready?
	timeout	noportwait	70000

prtslcttry:
	logerr	Too many (20) wait/retries -- queue too busy.
	failed

prtslctrdy:
	send	Y\r
	expect	connected	OK
	timeout	noconnect	20000


noportwait:
	logerr  Timed out awaiting place in port queue
	failed

noconnect:
	logerr	Sent system name, no "OK" from selector
	failed

# standard Unix login stuff.  Send cr, expect "ogin:", if no, send a break 
# (which tells Unix to try the next bit rate) and try again.  

connected:
	send	\r
	zero
	goto	waitlogin

sendbreak:
	count
	ifgtr	nolgi		6
	flush
	break

waitlogin:
	expect	gotlogin	ogin:
	timeout	sendbreak	5000

nolgi:
	logerr	No login: prompt
	failed

gotlogin:
	sendstr	1
	send	\r
	expect	gotword		word:
	timeout	nopwd		10000

nopwd:
	logerr	No password: prompt
	failed

gotword:
	sendstr	2
	send	\r
	success
