#!/usr/local/bin/kermit +
#
# UNIX: Change "kerbang" line above to path of C-Kermit program.
#
# n u m p a g e  --  Send a numeric page or dial a beeper
#
# Arguments:
# 1. Phone number
# 2. Optional message (omit for beepers)
#
# Redials automatically in case of busy signal (if modem supports "@").
# C-Kermit 7.0 or K95 1.1.19 required
#
# Notes:
# . Accurate reporting of success or failure and the automatic redialing
#   feature require a modem that supports the "Wait for Answer" dial
#   modifier "@".
# . Once the dial string is given to the modem, it is up to the modem to
#   do its job and report accurately whether it succeeded.  Experiments
#   with assorted modems give mixed results.  For example, "@" doesn't work
#   with some modems unless the phone rings at least 2 or 3 times before
#   picking up.
#
# Author: F. da Cruz, Columbia University, 16 April 1999.
# Revised Feb 2000 for transparent portability between K95 and C-Kermit.

define badversion {
    echo Sorry - C-Kermit 7.0 or K95 1.1.19 or later required.
    if eq "\%0" "\v(cmdfil)" exit 1
    stop 1
}
if not equal "\v(program)" "C-Kermit" badversion
if LLT \v(version) 700196 badversion

local kerbang result debug device modem poundsign speed ; Local variables

.kerbang = 0				; Assume no.
if eq "\%0" "\v(cmdfil)" .kerbang = 1	; This means we were.

define ERRQUIT {			; Macro to exit appropriately
    if def \%1 echo \%1			; with an error message.
    if \m(kerbang) exit 1		; If Kerbang script, exit all the way.
    stop 1				; Otherwise return to Kermit prompt.
}
if not def \%1 errquit {Usage: \%0: phone-number [ numeric-message ]}

; Get configuration parameters from environment variables. if any -- remove
; this section if you'd rather hard-wire the parameters into the script.

if not eq \v(system) WIN32 {		; (Windows uses TAPI)
    .modem := \$(MODEM)			; Modem type
    .device := \$(DEVICE)		; Dialout device / port
}
.speed := \$(SPEED)			; Port speed
.redials := \$(REDIAL_LIMIT)		; Number of times to redial if busy
.pause := \$(REDIAL_PAUSE)		; Pause between redials (sec)

; Local parameters...

.debug = 0				; (*) Change to 1 for debugging
.poundsign = 1				; (*) Change to 0 if "#" not required
if not eq \v(system) WIN32 {
  if not def modem .modem = usr		; (*) Change to match your modem
  if not def device .device = /dev/cua	; (*) Change to your dialout device
}
if not def speed .speed = 2400		; (*) Port speed - change if necessary
if not def redials .redials = 20	; (*) Change according to local laws!
if not def pause .pause = 1		; (*) Change according to local laws!

if \m(debug) {				; Echo parameters...
    echo { Number:      "\%1"}
    echo { Message:     "\%2"} 
    echo { Device:      "\m(device)"}
    echo { Speed:       "\m(speed)"}
    echo { Modem:       "\m(modem)"}
    echo { Redials:     "\m(redials)"}
    echo { Pause:       "\m(pause)"}
    echo { Poundsign:   "\m(poundsign)"}
}

if eq \v(system) WIN32 {		; Set up dialing device
    set tapi line			; Use TAPI in Windows
    if fail errquit {Error: Can't open TAPI device}
} else {
    set modem type \m(modem)		; Specify modem type
    if fail {				; Check for error
	errquit {Error: Modem type "\m(modem)" not known}
    }
    set line \m(device)			; Open the device
    if fail {				; Check for failure
	errquit {Error: \m(device) not available}
    }
}

def on_exit hangup			; In case of Ctrl-C
set exit warning off			; In case of misconfigured modem
set speed \m(speed)			; Set the desired speed

; Form the dial string...

if def \v(dm_wa) {			; Have wait-For-Answer dial modifier
    asg \%1 \%1\v(dm_wa)\%2		; Insert it between number and message
    set dial retries \m(redials)	; Adjust to local laws and regulations
    set dial interval \m(pause)		; Ditto (see comments above)
} else if def \%2 {			; No Wait-For-Answer but message given
    asg \%1 {\%1\frepeat(\v(dm_lp,5))\%2} ; Insert "long pause" before message
    set dial retries 0			; No automatic redialing
    if \m(debug) {
	echo { WARNING: This modem is not well-suited for paging.}
	echo { Busy signals are not detected and failure is not reported.}
	echo { Automatic redialing disabled.}
    }
}   
; Supply "#" at end of message if required and missing...

if ( def \%2 && \m(poundsign) && not eq "\fright(\%2,1)" "#" ) {
    asg \%1 \%1#			; if it was not there already
}
if \m(debug) {
    echo { Dial string: "\%1"}		; Show the final dial string
    set dial display on			; Watch computer/modem dialog
}
pdial \%1				; Dial number but don't expect carrier
.result := \v(status)			; Remember PDIAL result
if \m(debug) echo { Dial status: "\v(dialstatus)"}
pause 2					; Give modem time to catch up
hangup					; Hang up the phone
if \m(kerbang) exit \m(result)
end \m(result)				; Exit with code from PDIAL result
