;   $VER: ipd.Login 1.4 (16.1.96)
;
;   Define and set up the device to use
;
    DEVICE ${Surfer/SerialDevice} UNIT ${Surfer/SerialUnit}
    set BAUD=${Surfer/SerialBaud} DATABITS=8 PARITY=NONE PROTOCOL=NONE STOPBITS=1 BUFSIZE=16384

;   Enable the DSR signal to get RTS/CTS 7 wire hardware handshaking
;   working (&S0). Just to be sure, we also enable command results (Q0)
;   and verbose results (V1)

    ECHO "${MSG_INITIALIZING_MODEM}\n"

    SEND "AT&S0Q0V1\r"

;   Wait for the modem ok message; break if timeout

    WAIT TIMEOUT=10 "OK"
    ON STATUS GOTO TIMEOUT

;   Turn on hardware handshaking
    set PROTOCOL=7WIRE

;   Send initialization command

    SEND "${Surfer/ModemInit}\r"

;   Wait for the modem ok message; break if timeout

    WAIT TIMEOUT=10 "OK"
    ON STATUS GOTO TIMEOUT

;   Initialize the modem not to hang up when IPDial terminates (&D0).

    SEND "AT&D0\r"

;   Wait for the modem ok message; break if timeout

    WAIT TIMEOUT=10 "OK"
    ON STATUS GOTO TIMEOUT

;   Dialing ...

Dial:
    ECHO "${MSG_DIALING} ${Surfer/DialPrefix}${Surfer/DialNumber}...\n"

    SEND "${Surfer/DialCommand}${Surfer/DialPrefix}${Surfer/DialNumber}\r"

    WAIT TIMEOUT=60 "CONNECT" "NO CARRIER" "BUSY"
    ON STATUS GOTO TIMEOUT Login

;   The usual case: Our Remote is busy.
;   We do a delay of 30 seconds and dial again.

    ECHO "${MSG_BUSY}\n"
    DELAY 30
    ECHO "${MSG_REDIALING}\n\n"
    GOTO Dial

;   Okay, we are connected. Login into the IBM Information Services
;   host. This is accomplished by sending an ampersand to wake up
;   the host.

Login:
    ECHO "${MSG_MODEM_CONNECTION1}\n${MSG_MODEM_CONNECTION2}\n"

    DELAY 2
    SEND "&"

;   Wait for the host to prompt for entering the script version.
;   This defines the protocol of questions and answers to follow
;   before getting the SLIP connection up and running

    WAIT TIMEOUT=60 "NO CARRIER" "script version"
    ON STATUS GOTO TIMEOUT NoCarrier
    SEND "1.0\r"

;   Wait for the service prompt; we will always answer "INTERNET".

    WAIT TIMEOUT=15 "NO CARRIER" "Enter Service"
    ON STATUS GOTO TIMEOUT NoCarrier
    SEND "INTERNET\r"

;   Now wait for the login prompt; we will answer with the account
;   name, our user ID and the password.

    WAIT TIMEOUT=15 "NO CARRIER" "UserId Password"
    ON STATUS GOTO TIMEOUT NoCarrier
    SEND "${Surfer/Account} ${Surfer/UserID} ${Surfer/Password}\r"

;   Now the host will tell us our IP address in the following
;   format:
;
;       1.2.3.4 is your IP Address
;
;   Scan this and store the value into the environment variable
;   IPADDRESS so that they can be used later.

;   You need to "WAIT" until the message from the remote is complete
    WAIT TIMEOUT=30 "your IP Address"
    ON STATUS GOTO TIMEOUT

;   You must scan for an EXACT match
    SCAN "%[IPADDRESS%] %{is your IP Address%}" GLOBAL

;   Hit return for the next line
    SEND "\r"

;   This follows the destination address in the following format:
;
;       1.2.3.4 is your Destination Address

    WAIT TIMEOUT=30 "your Destination Address"
    ON STATUS GOTO TIMEOUT

    SCAN "%[DESTADDRESS%] %{is your Destination Address%}" GLOBAL

;   That's all, folks.

    ECHO "\n\n${MSG_SLIP_CONNECTION}\n"
    EXIT 0

;   Error message: Timeout

TIMEOUT:
    ECHO "\n${MSG_TIMEOUT}"
    DELAY 2
    SEND "+++"
    WAIT TIMEOUT=2 "OK"
    SEND "AT&D2H\r"
    WAIT TIMEOUT=5 "OK"
    ON STATUS GOTO NoHangUp
    ECHO "${MSG_DONE}\n"
    EXIT 10

NoHangUp:
    ECHO "${MSG_HANGUP_ERROR}\n"
    EXIT 10

;   Error message: NO CARRIER

NoCarrier:
    ECHO "\n${MSG_CONNECTION_LOST}\n"
    EXIT 10
