/*
 *	$Id$
 *
 *	AmiTCP/IP Dial Script Subroutine to Dial a number.
 *      Modem must be in command state before this.
 *
 *	Copyright © 1995 AmiTCP/IP Group,
 *	                 Network Solutions Development Inc.
 *	                 All rights reserved.
 * 
 *      
 */

/*
 * Get the phone number and retry count
 */
OPTIONS results
ARG maxRetries

Get PhoneNumber
 
/* 3 retries by default */
if (maxRetries = "") then maxRetries = 3

Get ModemDialPrefix

if ( PhoneNumber = "" | ModemDialPrefix = "" ) then do 
  Status "No phone number or dial prefix, cannot dial!"
  pause 5
  exit 10
end

Status "Dialing number" PhoneNumber||"..."
 
/*
 * Set Inter-character delay to 100 ms.
 * Some modems need this.
 */
Set InterCharDelay 100
OldDelay = result /* save the old value */

/*
 * Set the WaitFor timeout to 60 seconds.
 * Modem needs to respond in this time.
 */
Set WaitForTimeout 60
OldTimeOut = result  /* save the old value */

/* enable error signal */
signal on error

/* Initialize variables */
dialPrefix = ModemDialPrefix
onOnlineState = 0  /* assume not in online state */
retries = 0

do until (onOnlineState)
  /*
   * Send the dial command
   */
  SendLn dialPrefix||phonenumber

  /*
   * Wait for our command to be echoed back
   */
  WaitFor phonenumber||"\r"

  /*
   * Wait for dial result. CONNECT is the succesfull case.
   */
  WaitFor "CONNECT|NO CARRIER|BUSY|ERROR|NO DIALTONE|NO ANSWER|OK"

  if (result = 1) then do
    Status "Dialing Succeeded."
    onOnlineState = 1
    ModemOnline YES  /* now use Carrier Detect if configured */
  end
  else do
    if (retries > maxRetries) then do
      Status "Could not dial to the number" phonenumber
      exit 10
    end
    Status "Redialing number" phonenumber||"..."
    retries = retries + 1
  end
end

/*
 * return old values
 */
Set WaitForTimeout OldTimeOut
Set InterCharDelay OldDelay
exit 0

error:
Say "Dial.ssrx: Command on line" SIGL "returned" RC ":" SerScript.LASTERROR
Exit 10
