#!/usr/local/bin/kermit +
#
# d i a l o u t  --  Menu-driven dialing out with C-Kermit.
#
# Forces user to fill in the necessary blanks before it will dial.
#
# Hint: Set up and run the "getline" script first and all the blanks 
# will be filled in except the phone number.  For example, make a new
# script that contains something like:
#
#   getline /dev/cua,usr,57600:/dev/cub,hayes,2400:/dev/ttyS0,unknown,38400
#   dialout {\%1}
#
# The phone number can be supplied as a command-line argument (in quotes
# if it contains spaces or parens, etc, because of the shell).
#
# Ideas for further development:
#  . Choices for local country and area code.
#  . Choices for dialing directory.
#  . Choices for redial limit and interval.
#  . Choices for terminal type and many other settings.
#
# Requires C-Kermit 7.0 Beta.07 or later.
#
# Author: F. da Cruz, Columbia University, May 1999.

local modem device speed phone watch      ; Local variables
local \%p \%m \%c \%x lastmsg

set eval new                              ; Use new form of EVALUATE command.
set case off                              ; Case doesn't matter in commands
set quiet on                              ; Suppress most error messages
set take error off                        ; Ditto
set macro error off                       ; Ditto
set exit warning off                      ; Ditto

define MSG {                              ; Print message at given location.
    if > \v(argc) 2 screen move \%2 \%3   ; Move to given row/col (if any)
    screen cleol                          ; Clear to end of line
    if def \%1 xecho {\fcontents(\%1)}    ; Print message (if any)
}

define ERRMSG {                           ; Print error message on message line
    asg lastmsg {  \fcontents(\%1)}       ; Save the message for repainting
    msg {\m(lastmsg)} \%m
}

define PAINT {                            ; Paint the menu
    screen clear                          ;      Row:
    echo                                         ;  1
    echo {  \v(xprogram) Dialout Setup}          ;  2
    echo                                         ;  3
    echo {   1. Modem type         [\m(modem)]}  ;  4
    echo {   2. Device             [\m(device)]} ;  5
    echo {   3. Speed              [\m(speed)]}  ;  6
    echo {   4. Phone number       [\m(phone)]}  ;  7
    echo {   5. Watch modem dialog [\m(watch)]}  ;  8
    echo                                         ;  9
    echo {   6. Go}                              ; 10
    echo {   7. Restart/Clear}                   ; 11
    echo {   8. Help}                            ; 12
    echo {   9. Quit/Exit}                       ; 13
    echo                                         ; 14
    def \%p 15                                   ; Prompt line
    def \%m 16                                   ; Message line
    msg {\m(lastmsg)} \%m
}

:BEGIN
if ( not local && equal "\v(modem)" "none" ) { ; If remote or no modem type...
    set modem type unknown                     ; Default modem type is UNKNOWN
}
assign modem \v(modem)                    ; Set local modem variable too.
define watch No                           ; Default watch modem dialog is No.
undef device                              ; Initialize device to none.
undef speed                               ; Ditto for speed.
undef phone                               ; And phone number.
if ( local ) {                            ; But if we have a device assigned
    assign device \v(line)                ; make it the default.
    assign speed \v(speed)                ; And pick up its speed.
}
if def \%1 asg phone \%1                  ; Get phone number from command line.

:REFRESH                                  ; Back here to refresh screen.
paint                                     ; Display the menu.
set flag off                              ; FLAG ON means to exit the loop.

while ( not flag ) {                      ; Loop to read and execute choices.
    msg {} \%p                            ; Move to prompt row and clear it.
    getc \%c {  Choice: }                 ; Prompt and get one character.
    eval \%x \fcode(\%c)&127              ; Get its 7-bit code.
    if ( < \%x 32 || = \%x 127 ) {        ; Handle control characters.
        xecho ^\fchar(\%x#64)             ; Echo as ^X.
	switch \%x {                      ; What is it really?
	  :12, goto refresh               ; ^L = Refresh screen.
	  :3,:4                           ; ^C and ^D...
            msg {} \%m, exit 0 {  Bye}    ; are the same as Quit.
	  :default, continue              ; Ignore all others.
        }
    }
    xecho \%c                             ; Not a control character - echo it.
    switch \%c {                          ; Execute the choice.
      :m, :1                              ; MODEM...
          msg {} \%m                      ; Move to message line.
          ask modem {  Modem: }           ; Prompt for modem type.
          if def \m(modem) {              ; If they gave one...
               set modem type \m(modem)   ; Try to set it.
               if fail { 
                   errmsg {Invalid modem type: "\m(modem)"}
               } else {
                   undef lastmsg
               }
               asg modem \v(modem)
          }
          paint                           ; Repaint to show new modem type
          break
      :d, :2                              ; DEVICE...
          msg {} \%m                      ; Move to message line
          ask device {  Device: }         ; Prompt for device.
          if def \m(device) {             ; Etc, as above.
               set line \m(device)
               if fail {
                   errmsg {\m(device): \v(setlinemsg)}
               } else {
               	   if not def speed asg speed \v(speed)
                   undef lastmsg
               }
               asg device \v(line)
          }
          paint
          break
      :s, :3                              ; SPEED...
          if not local {                  ; No SET SPEED before SET LINE.
              errmsg {You must choose a device before setting the speed}
              continue
          }
          msg {} \%m                      ; Move to message line
          ask speed {  Speed: }           ; Prompt for speed
          if def \m(speed) {
              set speed \m(speed)
              if fail {
                  errmsg {Can't set speed: "\m(speed)"}
                  undef speed
              } else {
                  asg speed \v(speed)
                  undef lastmsg
              }
          }
          paint
          break
      :n, :p, :4                          ; PHONE NUMBER...
          msg {} \%m
          ask phone {  Number: }
          paint
          break
      :w, :5                              ; WATCH...
          msg {} \%m
          getok {  Watch? },
          if success def watch Yes
          else define watch No
          undef lastmsg
          paint
          break
      :g, :6                              ; GO...
          if not def modem  { errmsg {Modem not selected},     continue }
          if not def device { errmsg {Device not selected},    continue }
          if not def phone  { errmsg {Phone number not given}, continue }
          set flag on, break
      :c, :r, :7                          ; CLEAR or RESET...
          close
          set modem type unknown
          undef phone
          undef lastmsg
          undef \%1
          goto begin
      :h, :8                              ; HELP...
          msg {Press the digit or the first letter of the menu choice.} \%m
          break          
      :q, :e, :9                          ; QUIT...
          msg {} \%m, exit 0 {  Bye}
      :default
          errmsg {Not a valid choice: "\%c"}
    }
}
msg {} \%m

echo
set dial display off                      ; Watch modem display...
if eq "\m(watch)" "yes" set dial displ on ; Translate yes/no to on/off.

set quiet off                             ; Allow Kermit messages again.
set dial connect on                       ; CONNECT automatically.
set exit on-disconnect on                 ; Exit automatically on hangup.
dial \m(phone)                            ; Dial the number.
if fail exit 1 Call failed.               ; Give up if no answer.

; Here we exit automatically if the connection drops.  If the user
; escapes back, she gets the prompt so files can be transferred, etc.,
; and has the opportunity to re-CONNECT.
;
; End.
