/*
 *	$Id: Dial.ssrx,v 4.4 1996/09/26 23:16:38 jraja Exp $
 *
 *	AmiTCP/IP Dial Script Subroutine to Dial phone numbers.
 *      Modem must be in command state before this.
 *
 *	Copyright © 1996 AmiTCP/IP Group,
 *	                 Network Solutions Development Inc.
 *	                 All rights reserved.
 * 
 *	Echancements provided by:
 *		Ariel Magnum
 *		Thomas Wilhelmi
 */

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

Get ModemDialPrefix /* dial prefix for the modem */
Get ModemDialAttempts /* defaults to 10 */
Get ModemDialDelay /* defaults to 5 secs */
Get PhoneNumber /* space delimited phone numbers to try */

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

/*
 * Parse the phone number, which can contain multiple numbers separated by 
 * spaces.
 */
PhoneNumber = strip(PhoneNumber)	/* remove leading and trailing spaces */
numbers = words(PhoneNumber)		/* count the # of phonenumbers */
number = 1				/* start from the first */

/*
 * Set Inter-character delay to 20 ms.
 * Some modems need this.
 */
Set InterCharDelay 20
OldDelay = result /* save the old value */

/*
 * Get the current value of WaitForTimeout
 */
Set WaitForTimeout 2
OldTimeOut = result  /* save the old value */

/* enable error signal */
signal on error

/* Initialize variables */
onOnlineState = 0  /* assume not in online state */
retries = 1
calls = 0

do until (onOnlineState)
  calls = calls + 1

  Status "Dialing number "||word(PhoneNumber,number)||" ("||number||"/"||numbers||"), Attempt # "||calls||"/"||(numbers*ModemDialAttempts)||"."
 
  /*
   * Send the dial command
   */
  SendLn ModemDialPrefix||word(PhoneNumber,number)

  /*
   * Wait for our command to be echoed back (within 2 seconds)
   */
  Set WaitForTimeout 2
  WaitFor word(PhoneNumber,number)||"\r"

  /*
   * Wait for dial result. CONNECT is the succesfull case.
   * Wait for the result code maximum of 65 seconds
   */
  Set WaitForTimeout 65
  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 (result = 0) then do
      /*
       * Modem did not respond in time, cancel dialing
       */
      Send "\r"
      Set WaitForTimeout 2
      WaitFor! "OK\r\n"
    end
    if (retries >= ModemDialAttempts) then do
      if (numbers > 1) then
        Status "Could not dial to any of the numbers" PhoneNumber
      else
        Status "Could not dial to the number" word(PhoneNumber,number)
      exit 10
    end
    /*
     * Switch to the next number, wrap to the first after the last.
     * Also, for each try, try all the numbers present.
     */
    Pause ModemDialDelay
    number = number + 1
    if (number > numbers) then do
      number = 1
      retries = retries + 1
    end 
  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
