/*
 * AutoFAX has no information for you as the sysop to edit
 *
 * Go change the FAX-Times file and leave this one alone...
 *
 *
 * This is the program that converts the FAX TEXT into a FAX 
 * then tries to send it out.
 */
 
options results

/* Check to be sure GPFax is even running.... 
 */
if show(prorts,REXX_GPFAX) ~= 1 then exit

Version='FAX-IT v2.5   -*-   AutoFAX v2.5   -*-   By: Glenn J. Schworak'

Address cnetRexx0
GetUser 1307954 ; UserDir = Result
Doors   = 'Doors:' ; GPF = 'Doors:Fax-IT/'
LogFAX = 'Logs:FAX.log'
FaxIT 		= Doors'Fax-IT/'
Times 		= FaxIT'FAX-Times'
Messages  = FaxIT'Text/'
Numbers   = FaxIT'Numbers/'


/* As in the DataCall.gpf file, we set a file in the T: directory to keep
 * any other FAX-IT programs from trying to access GPFax while we are
 * working
 */
Shell Command 'C:Echo >t:FAXnode ""'




/*
 * Edit the file "FAX-Times" to configure the correct times for your
 * FAX modem to attempt to dial. These times will not stop a user from
 * typing a FAX message, just keep the system from calling out right away.
 */

stat=Exists(Times)
if stat~=1 then do
	address command 'Echo >>'LogFAX' "b1c1ERROR: c6The c3FAX-Times c6file is missing from the FAX-IT door area...w2q1"'
	exit
end
Open(tempfile,Times,'read') 
	hours=readln(tempfile)
	days=readln(tempfile)	
Close(tempfile)


Now = '-'Time('Hours')'-'
Day = SubStr(Date('W'),1,3)


Force="*"
stat=Exists("T:FAX-NOW") 
if stat=1 then do
	Open(tempfile,"T:FAX-NOW",'read') 
		Force=readln(tempfile)
	Close(tempfile)
	Shell Command "C:Delete T:FAX-NOW"
end


Open(tempfile,'T:TryFAX','write') ; Close(tempfile)

Do While Exists('T:TryFAX')='1'
	Address Command 'c:Delete t:TryFAX'
	if Force="*" then do
		If Index(Hours,Now)=0 | Index(Days,Day)=0 Then Call Quit
		Address Command 'c:List >T:FAX-List 'Messages'GJSFAX#?.#? Lformat "%S"'
		end
	else do
		Address Command 'C:Echo >T:FAX-List 'Force
	end
	Address rexx_gpfax

	File = Open(ListFile,'T:FAX-List','READ')
	If File~=1 Then Call Quit

	Do Until EOF(ListFile)
		TextFile = ReadLN(ListFile)
		convert Messages||TextFile
	  	File = Open(Text,Messages||TextFile,'READ')
	  	if File=1 Then do
	     FAXNUM = ReadLN(Text)
	     txt=''
		  FAXed='FAXed Out: 'Time('Civil')'  'Date('Normal')
		  do while Index(txt,"TO:")<1      
	        txt = ReadLN(Text)
	     end
		  TO='       'Strip(txt)
       do while Index(txt,"FAX:")<1     
          txt = ReadLN(Text) 
       end
		  FAX='      'Strip(txt)
       do while Index(txt,"SUBJECT:")<1 
          txt = ReadLN(Text) 
       end
		  Subject='  'Strip(txt)
       do while Index(txt,"FROM:")<1    
          txt = ReadLN(Text) 
       end
		FROM2=txt 
       UserNum=Strip(SubStr(txt'          ',Index(txt,#)+1,10))
       do while Index(txt,"WRITTEN:")<1 
          txt = ReadLN(Text) 
       end
		  Written='  'Strip(txt)
       Close(Text)
		  SendF = 'GPFax:FAX_OUT/'TextFile'_TFAX'
       SendFAX SendF' to 'Compress(FAXNUM)
		  Stat=''
		  ReportLog T 3
		  Stat=result
		  File2 = open(StatFile,UserDir'FaxStatus','Append')
		  if File2 = 0 then File2 = open(StatFile,UserDir'FaxStatus','Write')
       if SubStr(Stat,1,4)='OK.S' then do
         if File2 = 1 then call FAXGOOD
	     end 
       else do
		    if File2 = 1 then Call FAXBAD
         address command 'c:copy 'Messages||TextFile' to 'UserDir
       end
       if File2 = 1 then do
		    WriteLN(StatFile,'02}n1k4-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-q1')
	     end
	     if File2 = 1 then Close(StatFile)
	  end 
      address command 'c:delete >NIL: 'Messages||TextFile
	end 
  Close(ListFile)

  address command 'c:delete GPFax:FAX_OUT/GJSFAX#?.p#?'
  address command 'c:delete T:FAXnode'
end
call Quit


Quit:
  address command 'c:delete t:#?FAX#?'
exit


FAXGOOD:
  If ~Exists(Numbers||FAXNUM) then do
      open(NumFile,Numbers||FAXNUM,'Write')
	  WriteLN(NumFile,'k1---k0c1 NUMBER IS UNKNOWN TO THIS SYSTEMk1 ---k0q1')
      close(NumFile)
  end
  WriteLN(StatFile,'n3c3This FAX was successfully sent....   'FAXed)
  WriteLN(StatFile,'n1c2'TO'n1'FAX'n1'Subject'n1'Written)
  call StartFile
  open(LogFile,LogFAX,'Append')
  WriteLN(LogFile,'n2c2---SUCCESSFUL---n1q1  'From2'n1'TO'n1'FAX'n1'Subject'n1'Written)
  numf=open(NumFile,Numbers||FAXNUM,'Read')
  if numf=1 then do
    Pre='     NOTE:c6 '
    do while ~eof(numfile)
		txt=ReadLN(numfile)
       WriteLN(StatFile,Pre||txt)
       WriteLN(LogFile,Pre||txt)
		Pre='           '
    end
    close(numfile)
  end
  close(LogFile)
  Return


FAXBAD:
  WriteLN(StatFile,'n3c3This FAX failed to send....  'time('civil'))
  WriteLN(StatFile,'n1c1'TO'n1'FAX'n1'Subject'n1'Written'n2')
  WriteLN(StatFile,'--FAILED--')
  WriteLN(StatFile,TextFile)
  open(LogFile,LogFAX,'Append')
  WriteLN(LogFile,'n2c1---FAILED---n1q1  'From2'n1'TO'n1'FAX'n1'Subject'n1'Written)
  close(LogFile)
  Return


StartFile:
	if ~exists(LogFAX) then do
		open(LogFile,LogFAX,'Write')
		WriteLN(LogFile,'f1q1'Version'n2Started: 'Date(n)'  -*-  'Time(c)'n2')
		close(LogFile)
	end
	return
	

Quit:
	exit
