;HISTORY:
;	Written 8-Aug-96 by M.Morrison
;	16-Aug-96 (MDM) - Modified to let "check_dumps" figure out which datasets are dumps
;			- Added sending mail if there are failure messages
;	2-Dec-97 (CED)  - Mail gets sent to mailing lists instead of
;			  to individual users.
;	1-Oct-99 (RIB)  - All mail is sent to mdiops@mdisas.nascom.nasa.gov
;                         Cleaned up messages
;     6-Jan-2000 (RIB)  - Changed infil to not point at diapason
;     7-Jun-2000 (RIB)  - Modified initial loop to not die on empty file
;

userid = 'mdiops@mdisas.nascom.nasa.gov'
outfil = '$MDI_CAL_INFO/check_dumps.log'

for ifil=4,0,-1 do begin
    infil = get_last_tfr(ifil)
    print, 'Now processing file: ' + infil
    delvarx, dsets
    check_dumps, infil, dsets, out, qplot=qplot, qstop=qstop
    if (n_elements(out) gt 1) then begin
      print,out
      file_append, outfil, out(1:*), /uniq
    end
end

mat = rd_tfile(outfil,1,2)
fid = strmid(mat, 0, 11)
tim = anytim2ints(fid2ex(fid))
ss = sel_timrange(tim, anytim2ints(ut_time(), off=-2*86400), ut_time(), /bet)

if (ss(0) eq -1) then begin
    subject = 'MDI Memory check missing'
    msgmiss = ['CHECK_DUMPS  Ver 1.0    Program Run: ' + !stime, ' ',$
               '------ No memory dumps in last 48 hours ------']
    print, subject
    print, msgmiss
    mail, msgmiss, user=userid, subj=subject
    return
end

mat2 = mat(ss)
ppp = strpos( mat2, 'FAIL')

if (max(ppp) ne -1) then begin
    subject = 'MDI Memory check failure'
    msgfail = ['CHECK_DUMPS  Ver 1.0    Program Run: ' + !stime, ' ',$
               '------ Failure detected in last 48 hours ------', mat2]
    print, subject
    print, msgfail
    mail, msgfail, user=userid, subj=subject
end else begin
    subject = 'MDI Memory check OK'
    msgok = ['CHECK_DUMPS  Ver 1.0    Program Run: ' + !stime, ' ',$
             '------ No failure detected in last 48 hours ------', mat2]
    print, subject
    print, msgok
    mail, msgok, user=userid, subj=subject
end

end
