# MacSlip script for SCS Cisco Terminal Server

# This script uses two parameters:
#    phone - the SCS dialin phone number
#    hostname - the name (or IP address) of this Mac

# We assume we're dialing in to TS2.

on abort goto abort
message "\bHyde Park MacSLIP Script starting..."
message "Phone: $phone. Host: $hostname". Password: $passwd

# The init string only hangs up the modem.
# Change it to suit your needs.
set initstring "H"

#	Preset counter 0 to 0. Send "AT" to wake up the modem
#	and let it autobaud our speed. If the modem does
#	not respond in 3 tries, abort the script. 
message "Looking for modem..."
setcount 0 0
label findmodem
flush
ifcountgt 0 3 goto cantfindmodem
send "ATZ\r"
{
	ifmatch "OK" break
	iftime 3 goto findmodem
}

#	Send the modem an initialization string.
message "Initializing modem..."
setcount 0 0
label initmodem
flush
ifcountgt 0 3 goto cantinitmodem
send "AT$initstring\r"
{
	ifmatch "OK" break
	iftime 3 goto initmodem
}

#	Dial the phone and wait for the terminal server
#	to send its login banner. Some areas support 
#	using *70 as a dialing prefix to suppress call-
#	waiting tones which will probably cause your modem
#	to lose the connection. Some terminal servers
#	may require you to autobaud before they will respond.
#	Allow enough time for the modem handshake to complete.

message "Dialing $phone..."
flush
on abort goto hangupabort  # We're about to go off hook, so use different abort handler.
send "ATDT$phone\r"
{
  ifmatch "CONNECT" break
	ifmatch "NO DIALTONE" goto nodialtone
	ifmatch "BUSY" goto phonebusy
	ifmatch "NO CARRIER" got nocarrier
	iftime 1:15 goto dialtimeout
}
#
# Now, send a return once every 2 seconds until we match the system 
# prompt. In this case we just look for the username prompt. 
# We give up and abort the script after 10 tries.
#
setcount 0 0
label auto
flush
ifcountgt 0 10 return 0
send "\r"
{
    ifmatch ">" break
    iftime 2 goto auto
}

#	Put the terminal line into SLIP mode.

message "Enabling SLIP mode on server..."
flush
send "terminal download\r"
send "slip $hostname\r" 
{
    ifmatch "Password:" break
    iftime 30 return 0
}
#
# the following ifmatch string should be "mode" for uncompressed,
# "system" for compressed.
#
flush
send "$passwd\r"
{
    ifmatch "system" break
    iftime 30 return 0
}

# wait for the rest of the line and make sure the internal buffer gets updated.
delay 1
setcount 0 0
{
     ifcountgt 0 1 break
}

message "Looking for IP address..."

# now parse the IP addresses that are in the receive buffer
ipfind IPADDRESS
# Use TS2 as our gateway
set IPGWADDRESS 128.2.254.129
set IPNETMASK 255.255.0.0
message "\bSLIP startup completed successfully ($IPADDRESS). "
delay 2
flush
# return success
return 1

#	Error handlers.

label abort
beep
message "\bCommand-. seen or Cancel button clicked."
goto aborted

label hangupabort
beep
message "\bCommand-. seen or Cancel button clicked."
goto hangup

label cantfindmodem
beep
message "\bThe modem didn't respond to attention"
message "\bbsignal.  Check modem and cabling."
goto aborted

label cantinitmodem
beep
message "\bThe modem didn't respond to initialization"
message "\bstring.  Check the modem."
goto aborted

label nodialtone
beep
message "\bThe modem could not detect a dialtone."
message "\bCheck the phone line."
goto aborted

label phonebusy
beep
message "\bThe number $phone is busy."
goto aborted

label nocarrier
beep
message "\bThe modem could not establish a connection."
message "\bMake sure there is a modem on the other end."
goto aborted

label dialtimeout
message "\bThe modem or SLIP server isn't responding."
message "\bTry dialing manually to make sure the modem"
message "\band SLIP server are OK."
goto aborted

label cantstartslip
beep
message "\bCan't put server into SLIP mode."
goto hangup

#	General back-end for bailing out.  Make sure we've hung up the phone.

label hangup
delay 1
send "+++"
delay 2
send "ATH\r"

label aborted
message "\bScript aborted!"
ask junk							# This activates the OK button.
flush
return 0

# Stuff to do when "Disconnect" button is pressed.

label disconnect
delay 1
send "+++"
delay 2
send "ATH\r"
message "Done."
flush
return 1
