pro srspas_chk, new_items=new_items, old_fil=old_fil, new_fil=new_fil, $
		log_fil=log_fil, copy2isass2=copy2isass2, nocopy=nocopy, copy_cmd=copy_cmd, qstop=qstop
;
;
;HISTORY:
;	Written Jan-93 by M.Morrison
;	26-Jan-93 (MDM) - Modified to recognize an empty SRSPAS file
;	24-May-93 (MDM) - Modified to run the program through flare1 instead
;			  of flare2
;	 7-Jun-93 (MDM) - Modified to only look at data that is within the past 
;			  7 days.  SRSPAS will return spurious data on occassion
;			  (it returned Oct-91 data in the temporary database on
;			  7-Jun-93)
;	10-Jun-93 (MDM) - Small patch related to 7-Jun patch
;	 7-Oct-93 (MDM) - Replaced /ys with $ys
;                       - Added a line to copy the SRSPAS.OLD file to isass2
;                         backup software tree
;	 8-Nov-93 (MDM) - Made 'srspas.new' copy to use "cp -f" instead of "cp"
;        9-Nov-93 (MDM) - Returned to make 'srspas.new' copy to use "cp" instead of "cp -f"
;                         because MIPS (flare8) machine does not recognize "-f"
;	 2-Dec-93 (MDM) - Added /NOCOPY command (to disable the copying of
;			  SRSPAS.NEW to SRSPAS.OLD and added COPY_CMD which
;			  is output and is the copy command string (which 
;			  will be executed in GO_TOBAN after reformatting).
;	 4-Jan-94 (MDM) - Patch for case where there is no overlap between
;			  SRSPAS.OLD and SRSPAS.NEW listings
;			- Added qstop option
;
;
copy_cmd = ''
new_items = 0
;
if (n_elements(old_fil) eq 0) then old_fil = getenv('ys') + '/site/logs/srspas.old'
if (n_elements(new_fil) eq 0) then new_fil = getenv('ys') + '/site/logs/srspas.new'
if (n_elements(log_fil) eq 0) then log_fil = getenv('ys') + '/site/logs/srspas.chk'
;
if (not file_exist(log_fil)) then begin
    openw, lun, log_fil, /get_lun
    printf, lun, 'Log File initialized : ', !stime
    printf, lun, ' '
    free_lun, lun
end
line = '                                                                                                                                    '
openr, lun, log_fil, /get_lun
while not eof(lun) do begin
    point_lun, -1*lun, p1		;position right before last line
    readf, lun, line
    point_lun, -1*lun, p2		;position right after last line
end
p = p2				;default it to append to the end
free_lun, lun
;
spawn, 'rm -f ' + new_fil
;
cmd = 'rsh flare1 -l sxt srspas_run ' + new_fil
spawn, cmd, result
rtime = !stime
;
ss = strpos(strupcase(result), 'ABORT')
if (max(ss) ge 0) then begin
    message = 'ERR: SRSPAS_RUN error --- ' + arr2str(result, delim=' // ')
end else begin
    old = rd_srspas(old_fil, /sort)
    new = rd_srspas(new_fil, /sort)
    if (get_nbytes(new) gt 10) then begin
	sss = sel_timrange(new, anytim2ints(!stime, off=-7*24*60*60.), anytim2ints(!stime, off=2*24*60.*60.), /bet)
	if (sss(0) eq -1) then new = 0b else new = new(sss)
    end
    ;
    if (get_nbytes(new) lt 10) then begin
	message = 'ERR: SRSPAS_RUN error --- Output file is empty'
    end else begin
	n = n_elements(new)
	old_line = strmid(old.line, 6, 38)		;lin 1
							;lin 2
							;lin 3

	new_line = strmid(new.line, 6, 38)		;lin 2
							;lin 3
							;lin 4
	ss = where_arr(new_line, old_line)
	qfound_new = bytarr(n) + 1
	if (ss(0) ne -1) then begin
	    qfound_new(ss) = 0
	    qfound_new(0:ss(0)) = 0		;old stuff that has dropped off queue, ignore
	end

	ss2 = uniq(new_line)
	qfound_new(ss2) = qfound_new(ss2) + 1	;want where it is new and uniq
        ;
	if (keyword_set(qstop)) then stop
	;
        ;--- Look for cases where the final SIRIUS database times are not exactly the same.  We have found that the
        ;    start times have always matched, so just check for matching start times between real time/pb temporary and final.
        ;
	ss = where((new.source eq 'SA220') and (qfound_new eq 2))		;real time
	if (ss(0) ne -1) then begin
	    for i=0,n_elements(ss)-1 do begin
		del = int2secarr(old, new(ss(i)))
		sss = where( (abs(del) lt 30) and (old.source eq 'SA228') )		;within 30 seconds of an old temporary entry
		if (sss(0) ne -1) then qfound_new(ss(i)) = -1
            end
	end

	ss = where((new.source eq 'SA221') and (qfound_new eq 2))		;play back
	if (ss(0) ne -1) then begin
	    for i=0,n_elements(ss)-1 do begin
		del = int2secarr(old, new(ss(i)))
		sss = where( (abs(del) lt 30) and (old.source eq 'SA229') )		;within 30 seconds of an old temporary entry
		if (sss(0) ne -1) then qfound_new(ss(i)) = -1
            end
	end

	if (keyword_set(qstop)) then stop

	ss = where(qfound_new eq 2)
	if (ss(0) ne -1) then begin
	    new_items = new(ss)
	    line_out = new(ss).line
	    message = 'NEW: ---- Total of' + string(n_elements(ss)) + ' new uniq files covering ' + $
			string(total(new(ss).nrec)) + ' records'
	    message = [message, 'NEW:      ' + line_out]
	end else begin
	    spawn, 'tail -1 ' + log_fil, r
	    tsince = rtime
	    ntry = 1
	    if (strpos(r(0), 'NNW') ne -1) then begin		;last entry was no new data also
		tsince = strmid(r(0), 50, 23)
		ntry = fix(strmid(r(0),75,4)) + 1
		p = p1		;write over the last line
	    end
	    message = 'NNW: No new data since ' + tsince + ' (' + string(ntry,format='(i4)') + ' checks)'
	end

	copy_cmd = 'cp -p ' + new_fil + ' ' + old_fil
	if (not keyword_set(nocopy)) then spawn, copy_cmd
	if (keyword_set(copy2isass2)) then spawn, 'cp -p ' + new_fil + ' /2p/yohkoh_sw/site/logs/srspas.old'
    end
end
;
openu, lun, log_fil, /get_lun
point_lun, lun, p
for i=0,n_elements(message)-1 do printf, lun, !stime + '    ' + message(i)
free_lun, lun
;
end
