! 20 May 89
! Name:		autopoll.cmd
! Purpose:	Perform unattended reliable file transfers
! Usage:	"term autopoll infile outfile"
!		use "term -q autopoll infile outfile" from UNIX cron
!		infile contains each system name to poll, one per line,
!		matching the corresponding .con file in the poll directory.
!		autopoll writes outfile with the system names that failed.

! the following line specifies the maximum number of systems that can be polled
setvar maxnodes 25			! change to new maximum if needed

!
! Polling directories: (change if desired, must change autop2.cmd also)
!
switch
case opsys()=0		! VMS
	setvar qdir	"SYS\$SYSROOT:[TERM.POLL]"
case opsys()=1		! MSDOS
	setvar qdir	"/usr/term/poll/"
case opsys()=8		! BTOS
	setvar qdir	"[Sys]<Term>poll>"
case opsys()=9		! Mac
	setvar qdir	""
default			! UNIX-like systems
	setvar qdir	"/usr/term/poll/"	! queue dir
endswitch

setvar qlog	"term.log"		! logfile name in qdir
!
! the following constants are best not modified
dimstr nodes maxnodes			! allocate poll names array
setvar nodeext	".con"			! MUST be lowercase for compares
setvar qext	".*"
setvar wcard	"*"
setvar nodespec	"*"+nodeext
setvar reqspec	qdir+qext

!
! determine if we're being run correctly
if '$1'='' or '$2'=''
	? "Usage: autopoll infile outfile"
	quit
endif
setvar infile "$1"
setvar outfile "$2"

!
! specify our logfile name, turn on logging
logfile $(qdir)$(qlog)
set log on			! auto logging
log "Autopoll Startup"

iferror fopen 1,infile
	? "Can't open input file: $(infile)"
	log "Can't open input file: $(infile)"
	quit
endif
iferror fcreate 2,outfile
	? "Can't create output file: $(outfile)"
	log "Can't create output file: $(outfile)"
	quit
endif

setvar cnt 0
repeat
	freadln 1,filename
	if !feof(1)
		if cnt>=maxnodes
			? "Too many nodes, system $(filename) skipped"
			log "Too many nodes, system $(filename) skipped"
		else
			setvar nodes[cnt] filename
			setvar cnt cnt+1
		endif
	endif
until feof(1)
fclose 1

! cnt now contains the actual number of nodes we might have to call
if cnt=0
	? "Autopoll Finish: No systems defined for automatic polling process"
	quit
endif

on abort
	log "\^C operator abort"
	? "^C operator abort"
	logfile close
	return
endon

setvar num 0
setvar numpolled 0
setvar anycalled FALSE
repeat
	setvar yesreq FALSE
	! check for any files named <sysname>.nnn
	for i in ($(qdir+nodes[num]+qext)) do
		if tolower(right(i,len(nodeext)))!=nodeext
			setvar yesreq TRUE
		endif
	endfor
	if yesreq
		setvar anycalled TRUE
		! autop2.cmd writes the sysname to file 2 if unsuccessful
		do autop2 $(nodes[num])
		setvar numpolled numpolled+1
	endif
	setvar num num+1
until num=cnt
iferror fclose 2
	? "Error closing output file: $(outfile)"
	log "Error closing output file: $(outfile)"
endif

if not anycalled
	? "Autopoll Finish: $(numcount) systems polled"
	log "Autopoll Finish: $(numcount) systems polled"
endif
iferror logfile close
	? "Logfile close error"
endif

quit
