; pcplus Aspect Script file for transferring data to AccuWeather

;#define test 1 ; this definition puts script in test mode, must be commented out before compiling release code!!

#ifndef test
	#define redials 2 ; # of times to retry of can't connect with AccuWeather
	#define waittime 20; # seconds to wait for response from other end
	#define connectpromptwaittime 5 ; time to wait after connection for system prompt
	#define dialtime  60; # seconds to wait for dialing to succeed
	#define logretries 10 ; # times to send <CR> waiting for login message
#else
	#define redials 1 ; # of times to retry of can't connect with AccuWeather
	#define waittime 3; # seconds to wait for response from other end
	#define connectpromptwaittime 2 ; time to wait after connection for system prompt
	#define dialtime  3; # seconds to wait for dialing to succeed
	#define logretries 2 ; # times to send <CR> waiting for login message
#endif
	
integer xmitOK
#define myID  s4
#define username s5
#define mypassword s6
#define DataDirectory s7

#define DataManager 1 ; the index # identifying dial dir entry for data manager # (to call and xmit data to)
#include "Call.was"

string prompt = "?"
integer  scriptstatus =0

; proc to send data file
proc SendData
strparm filist ; this is the list of filenames which is to be transferred

string finame
string junk
string f
integer filenum
integer nullflag
integer tries

breakpoint
if AccuCall() ; 	call AccuWeather
	scriptstatus=-2 ; set return status as faillure, as default
	statmsg "call connected and carrier detected"
	;	login to VAX
	if login() 
		identify(myID)
		for tries=1 upto 5
			statmsg "Waiting for prompt"
			waitfor prompt, waittime
			if success
				exitfor
			else
				transmit "^M^J"
				statmsg "Trying to get prompt"
			endif
		endfor
		; transfer up to 25 files at one shot
		breakpoint
		for filenum = 0 upto 24
			statmsg "finding file name ..."
			strextract finame filist ",",filenum	
			strreplace finame " " "" ; remove all spaces from file name
			statmsg "transferring file : %s" finame
			nullstr finame, nullFlag
			breakpoint "nullflag: %s" NullFlag
			if nullFlag != 0
				statmsg "no more files, exitting ..."
				breakpoint "null file name, exitting loop"
				exitfor
			else
				assign f, DataDirectory
				strcat f, "\"
				strcat f, finame
				if findfirst f   ; only attempt transfer if file exists
					breakpoint "calling xmitfile : %s", f
					if xmitfile (f) == 1
						breakpoint "deleting %s" finame
						statmsg "deleting %s" finame
						delfile f; DELETE TRANSMITTED FILE
						xmitOK=1 ; flag that file xmitted OK
						assign s0,"OK"
						transmit "^M^J"
						waitfor prompt, waittime
						scriptstatus=1 ; set return status as OK
					else
						breakpoint "xmitfile failed"
						assign s0, "XFER FAILED"
						statmsg "xmitfile failed" 
					endif
				else
					statmsg "file does not exist %s" f
					assign s0,"file does not exist: "
					strcat s0, f
				endif
			endif
		endfor
		transmit "^M^J"
		waitfor prompt, waittime
		transmit "bye^M^J"
		breakpoint
	else
		statmsg "login failed"
		assign s0, "LOGIN FAILED"
		breakpoint
	; error login failed
	endif
else
	;
	statmsg "AccuCall failed"
	scriptstatus=-1
	; s0 return error message was already set by AccuCall()
endif
hangup
while $carrier
	comgetc i3, 20
	if i3 == 13 ; exit loop if CR received
		exitwhile
	endif
endwhile
endproc

func AccuCall:integer
	return CallAndConnect(DataManager)
endfunc

func Identify:integer
strparm ID
	integer r=0
	#ifndef test
	statmsg "waiting for ID prompt"
	waitfor "ID:" 40
	STATMSG "Got ID prompt"
	r=1
	transmit ID
	transmit "^M^J"
	#else
		r=1 ; force function to return 'ok'
	#endif
	return r	
endfunc

func Login : integer
	integer r=0
	string ss
	integer lcount=0
	#ifndef test
	transmit "^M^J" ; poll VAX server
	LOGLOOP1:
	waitfor "-", connectpromptwaittime 
	if Success
		waitquiet 2, waittime
		ss= "logging on : "
		strcat ss, username
		statmsg ss
		transmit username
		transmit "^M^J"
		call xwait
		if WaitQuiet 2,waittime
			statmsg "password prompt received"
			statmsg "sending password"
			set aspect display off
			transmit mypassword
			transmit "^M^J"
			r=1
			call xwait
			set aspect display on
		else
			statmsg "no password prompt"
		endif
	else
		statmsg "no prompts yet, sending <CR>"
		transmit "^M^J"
		lcount=lcount+1
		if lcount < logretries
			goto logloop1
		endif
	endif
	#else
	r=1
	#endif
	return r
endfunc

func xmitfile:integer
	strparm datafile
	integer r = 0
	integer counts=0
	breakpoint "xmitfile : %s", datafile
	#ifdef test
	r=1
	#else
     	 getpromptloop:
		transmit "^M^J"
		waitfor prompt, waittime
		if not Success
			counts=counts+1
			if counts > 5 
				goto getpromptloop
			endif
		endif
	counts=0
	xmodloop: 
	  transmit "XMODREC^M^J"
	   statmsg "waiting for xmodem start message, pass #%d", counts
	   if waitquiet 5,WaitTime	; wait for xmodem start prompt
	   	sendfile xmodem datafile
	   else
		counts=counts+1
		if counts < 5
			goto xmodloop
		endif
	   endif
	   scriptstatus=$FILEXFER	; get current transfer status
	   while scriptstatus==1	; loop while transfer is in progess
	      scriptstatus=$FILEXFER	; get current transfer status
	   endwhile	
	   if scriptstatus==2	; success
	      statmsg "Transfer was successful!!"
		r=1
	   elseif scriptstatus==3	; failure
	      statmsg "Transfer aborted!!"
		r=0
	   endif
	#endif
	return r
endfunc

proc xwait
	statmsg "waiting for xmit buffer to empty"
	while $txcount > 0
	endwhile
	statmsg ""
endproc


proc main
	string filist
	i0=0
	assign filist, s0
	s0="TRANSMITTING"
	set txpace 50
	set relaxed on
	set connection statmsg on
	set alarmtime 0
	set aspect display on
	SendData(filist)
	statmsg "status = %d", scriptstatus
	i0=scriptstatus
	waitfor "junkstring", 5 ; wait for 5 seconds
endproc

