;
; MAILROOM automatic run script. - Ver. 1.0
;  copyright 1991 - James A. Littlefield 
;
; Script to automatically execute the contents of the batch file mailrm.bat
; at preset intervals.  The number of seconds between invokes is contained
; in the INTERVAL constant. This script will run mailrm every interval 
; seconds and will also run mailrm 1x after each remote login session.
; If INTERVAL is set to 0 then mailrm will not run periodically.
;
; After each remote user, pcmail is run to compress the procomm message data
; files and eliminate any files that may have been deleted.
;
; For proper operation the attached modem/computer must use CD, the procomm
; host options menu must have the connection type set for MODEM and the
; Goodbye Action option set for HANGUP.
;
; 12/04/91 - Send answer init string to modem on entry to the script
;            (Whatever is in the AutoAnswer setup option will be sent
;		   - display last user and connect time msg in dialog box
;	       - script will automatically set host mode for modem connection
;			 with hangup on logoff
; 12/05/91 - send modem init string on program entry
;          - remove script exit on abort of host mode
; 12/09/91 - try snatching baud rate after CD is detected
;		   - add code to detect and set modem baud rate messages
;		   - delay entry into host mode until CD is hight for 1 second
; 12/11/91 - break into subprograms to try to get around disk overlay
;		     problems.
; 12/13/91 - and setting restore around key processing
; 12/15/91 - trap all keys, allow only <esc> and <f2>
;			 <esc> = exit script
;            <f2>  = local login
;

integer INTERVAL=0 
string tvar
string msg1
string msg2
string msg300,msg1200,msg2400,msg4800,msg9600
string ans_on,ans_off
string batchname = "mailroom.bat"
integer next_time			; next time to run
integer con_tmr=0
long default_baud=1200


proc main					; script main procedure
integer index=0
integer mval=0

call init_proc				; set everything up
while (forever)				; just do host and run mailrm after each user
 call setup_tmr				; init scrn display and counter
 while (2>con_tmr)		    ; run mailrm every hour
  strcmp tvar $time1
  if not success
	con_tmr++
 	tvar=$time1
 	atsay 9 36 7 $time1
;
; Since the script turns of procomm key checking (for speed)
; capture and process all the keys that have arrived in the last
; second
;
	if (hitkey != 0) 
	  call process_keys
    endif

	if INTERVAL>0
 	 dec next_time 
 	 strfmt msg1 "Next Run in %6d sec." next_time
 	 if (next_time==0)
   		next_time=INTERVAL
; --------------------------- may cause a long wait before things start up
   		dos batchname		; run mailrm (put proper args in)
 	 endif
	else
	 msg1="Run After Next Remote User"
    endif
 	atsay 8 20 7 msg1
 endif

;
; Make sure CD is high for at least 1 second before going to host
;
    if (connected ==0)
	 con_tmr=0
	endif

;
; look for a connect type of message as defined in setup menus
;
while (comdata !=0)
  mval=0 
  while ((mval != 0x0d) && (mval != 0x0a) && (comdata !=0))
   comgetc mval				; read the keycode
   strpoke msg2 index mval
   index++
   if (index > 39) 
    index=0
   endif
  endwhile

if ((mval == 0x0d) || (mval == 0x0a))
  index--
  strpoke msg2 index 0		; tack a null on the end truncate last char

  switch msg2
   case msg300
    set baud 300
    endcase

   case msg1200
    set baud 1200
    endcase

   case msg2400
    set baud 2400
    endcase

   case msg4800
    set baud 4800
    endcase

   case msg9600
    set baud 9600
    endcase

  endswitch
  statrest
  strset msg2 32 80
  index=0
 endif
 endwhile
 endwhile

;
; If we got to here then CD must have been active for at least 1 sec.
; but apparently it is no gone so just hangup and resume waitloop.
;
 if (connected == 0)
  hangup
  loopwhile
 endif
 call do_host			; enter host mode
endwhile
endproc

;
; This procedure sets everyting up.
;
proc init_proc
 integer mval=0
 integer k=0
 integer index=0

 clear
 next_time=INTERVAL
 fetch modem cnct300 msg300
;
; remove the (sometimes) trailing ^M on the 300 baud connect string
;
 strlen msg300 index
 index--
 strpeek msg300 index mval
 index--
 strpeek msg300 index k
 if ((mval == 77) || (k == 94))
  strpoke msg300 index 0
 endif
 fetch modem cnct1200 msg1200
 fetch modem cnct2400 msg2400
 fetch modem cnct4800 msg4800
 fetch modem cnct9600 msg9600
 fetch modem autoansoff ans_off
 fetch modem autoanson ans_on
 fetch baud default_baud
 set host connection modem	; this is a modem connect so monitor CD
 set host goodbye hangup	; hangup line when user logs off
 set host autobaud on		; enable autobaud
 set modem autobaud on		; enable autobaud
 set display off				; inhibit echo to the screen
 set switchcase off			; case insensitive comparison for switch strings
 set keys on					; don't check for keys before each line
 set rxdata on				; script will handle i/o
 termkey 0x2400				; send modem init comnd - terminates ans. mode
endproc

;
; Since CD will be high on entry to the host mode the proper baud rate
; must be set prior to entry.  This function will enter host mode and
; when the remote user logs off, will execute the contents of the batch
; file whose name is contained in the string variable batchname
;
proc do_host
 clear
 set display on				; enable screen display
 host						; execute the host command 
 set display off			; inhibit screen display
 dos batchname				; put all the necessary command in here
 next_time=INTERVAL
 set baud default_baud		; restore default baud rate
 statrest					; redisplay status line
endproc

proc setup_tmr
 box 5 18 15 60 7
 atsay 6 20 7  "MAILRM Automatic Run Script"
 atsay 9 20 7  "Current time "
 atsay 11 20 7 "   Last User:"
 atsay 11 36 7 $H_NAME
 atsay 12 20 7 "Connected At:"
 atsay 12 36 7 $H_ONLINE
 atsay 13 20 7 "Session Time:" 
 atsay 13 36 7 $H_ELAPSED
 tvar=$time0
 transmit ans_on 			; send autoans enable to the modem 
 strset msg2 32 80
 con_tmr=0
endproc

;
; Process keys until none are left
;
proc process_keys
integer a
 while (hitkey !=0)
  keyget a
  switch a
  case 0x001b
    clear
	box 11 19 13 58 7
	atsay 12 20 5 "Terminating Automatic Execution Script"
	alarm 2
    transmit ans_off		
	clear
    exit					; terminate the script
  	endcase

  case 0x3c00				; establish local login session
	 vidsave 0
	 set keys off
	 call do_host			; start host mode session
	 set keys on
	 vidrest 0
	 transmit ans_on
  	endcase
  endswitch
 endwhile
 set baud default_baud
 statrest					; redisplay status line
endproc
