# connect (Based upon ppp-ex.scr by Daniel Chouinard.)
#
# Modified by John E. Gotts <jgotts@engin.umich.edu>.

let $d="/dev/ttyS1"
# let $n="8241130"
# let $n="7959100"
let $n="92137970"
# let $n="92133720"
let $l="/tmp/ppp.log"

set senddelay 0.10

print "login: "
input $u
print "Password:"
set echo off
input $p
set echo on
print "\n"
open com $d
set com 38400n81
let n=0
:reset
inc n
if n>3 print "Failed to reset modem.\n" goto error
print "\nResetting modem...\n"
waitquiet 5 0.5
if % = -1 print "Couldn't get silence.\n" goto error
send "ATZ0^m"
waitfor 5 "ok"
if % != 0 print "Failed to receive acknowledgment.\n" goto reset
let n=0
:dial
waitquiet 2 0.5
if % = -1 print "Couldn't get silence.\n" goto error
inc n
let $a=$mid($time(),11,8)
print "\r",$a," Dial attempt #",n
send "ATDT "+$n+"^m"
waitfor 80 "connect","busy","no dial tone","no carrier"
if % = 1 goto dial
if % = 2 print "\n\nNo dial tone.\n" goto error
if % = -1 send " " sleep 2 goto dial
if % != 0 print "\n\nNo carrier or other error.\n" goto error
get 5 " ^m" $c
let n=0
print "\nConnected: ",$c,"\n"
sleep 1
print "Waiting for host:...\n"
waitfor 5 "ost:"
if % != 0 print "Could not get host: prompt.\n" goto error
print "Sending ppp...\n"
send "ppp^m"
print "Waiting for login:...\n"
waitfor 5 "ogin:"
if % != 0 print "Could not get login: prompt.\n" goto error
print "Logging in...\n"
send $u+"^m"
print "Waiting for Password:\n"
waitfor 10 "word:"
if % != 0 print "Could not get password prompt.\n" goto error
print "Sending password...\n"
send $p+"^m"
print "Starting ppp...\n"
send "ppp^m"
fork
let i=%
if i != 0 goto nonforked
exec "/usr/sbin/pppd -detach "+$dev()
:nonforked
let $b=$time()
let d=time()
print "Type ENTER to close connection.\n"
print "Connected since ",$b,".\n"
print "Connect ",$c," as ",$u,".\n"
:tloop
let $t=$hms(time()-d)
print "\r",$mid($time(),11,8)," ->  ",$t," "
sleep 1
testkey
if % = 0 goto tloop
open file $l
fprint $b," - ",$time()," -> ",$t,"\n"
close file
kill 15 i
sleep 1
exit 0
:error
print "\nGot error #",%,"\n"
sleep 2
exit 1
