; Send an alpha page via BT gateway
;
; This is a KERMIT SCRIPT not a SHELL SCRIPT!
;
; these settings and macros from ckpage.ksc script shipped with ckermit 6

define \%A 90345581354		; number of the call centre
set modem type usr		; I have a USR modem
set line /dev/modem		; symbolic link to COM1
set speed 2400			; Uses 2400 bps
set flow xon/xoff		; and Xon/Xoff flow control
set modem flow none		; end-to-end, not local

;------------------ Should be ok below here ---------------------------------
; define the pager number and message
define \%B \Fsubstr(\&@[2], 2, (\Flength(\&@[2])-1))
define \%C \Fsubstr(\&@[3], 2, (\Flength(\&@[3])-1))

define APAGE {
    set dial retries 10        ; Allow 10 redials
    dial \%1                   ; Call the pager service
    if success -               ; If the call is answered
      tapmsg \%2 {\%3}         ; Send the page
    else end 1 Page failed.    ; otherwise fail
}

;
;   apage message_centre phone_number { this is a message }
;

COMMENT - TAPMSG - Send a one-line alpha page using TAP
;   \%1 = Pager ID (that's %1 to the function, not the script!)
;   \%2 = Message (ditto %2)
;
def TAPMSG {
  loc \%i \%m \%s blk        ; Local variables
  asg \%m \2\%1\13\%2\13\3   ; <STX>ID<CR>msg<CR><ETX>
  asg \%s \fchecks(\%m)      ; Get checksum and make block
  asg blk \%m\fchar(\fmod(\%s/256,16)+48)-
\fchar(\fmod(\%s/16,16)+48)-
\fchar(\fmod(\%s,16)+48)\13  ; Checksummed TAP block

  for \%i 1 6 1 {            ; Try six times to get prompt
    out \13                  ; Send <CR>
    in 3 ID=                 ; Wait for "ID="
    if succ break
  }
  if > \%i 6 end 1 No prompt
  for \%i 1 8 1 {            ; Send <ESC>PG1, get <ACK>
    msl 500
    out \{27}PG1\13
    min 3 {\6\13} {\21\13} {ID=} {\27\4\13}
    switch \v(minput) {
      :0, continue                ; Timeout
      :1, break                   ; <ACK>
      :2, continue                ; <NAK>
      :3, out \{27}PG1\13, continue
      :4, stop 1 Forced disconnect ; Fatal error
    }
    break
  }
  if > \%i 8 end 1 Timeout or bad response
  in 5 \27[p\13                     ; Wait for go-ahead
  if fail end 1 No go-ahead         ; Didn't get it
  for \%i 1 8 1 {                   ; Try eight times
    msl 500
    out \m(blk)                     ; Send block
    min 8 {\6\13} {\21\13} {\13\27\4\13} {\30\13}
    switch \v(minput) {             ; Get response
      :0, continue                  ; Timeout
      :1, break                     ; <ACK> - success
      :2, continue                  ; <NAK>
      :3, stop 1 Forced disconnect
      :4, echo ERROR - RETRYING, continue
    }
    out \4\13                       ; Sign off with <EOT>
    in 5 \27\4\13                   ; Get <ESC><EOT> back
    break                           ; But ignore timeout
  }
  if > \%i 8 end 1 Timeout or bad response
}

; start of main routine

xif not equal \v(args) 4 -
{echo Usage: kermit \v(cmdfile) =mobile_number ="Message Text", quit} -
else {echo Correct number of arguments...}

; Trim the message to 80 characters, if required
if > \Flength(\%C) 80 def \%D \Fsubstr(\%C,1, 78)+>
else def \%D \%C

#set debug on
#log debug
#log session

; when I say hangup, hangup dammit!
set exit warning off

; make the call
apage \%A \%B { \%D }
if failure goto failed
if success goto success
:failed
hangup
echo { Unable to page \%B - Aborting.. }
exit 1
:success
hangup
echo { Paged \%B }
exit 0
