pro mk_hst_from_tstol, date, out, out0
;
;
;
tarr = anytim2ex(date)
date2doy, strmid( ex2fid( tarr ), 0, 6), doy, yr
indir = '/td10/summary/tstol'
infilnam = 'EVENT_' + strtrim(yr,2) + '-' + string(doy,format='(i3.3)') + '.DAT'
print, 'Looking for ', infilnam, '    in directory: ', indir
ff = file_list(indir, infilnam + '*')
infil = ff(0)
if (infil eq '') then goto, skip
;
tmp_fil = '/tmp/mk_hst_summary.tmp'
file_delete, tmp_fil
if (not file_exist(infil)) then goto, skip
spawn, "grep 'STOL_MSG: Opened' " + infil + '  > ' + tmp_fil
mat = rd_tfile(tmp_fil, nocomment='#')
file_delete, tmp_fil
if (mat(0) eq '') then goto, skip
;
timezone = 5*60.*60.	;EST
if ( int2secarr(date, '6-Apr-97') gt 0) then timezone = 4*60.*60.	;EDT
;
n = n_elements(mat)
out0 = strarr(n)
for i=0,n-1 do begin
    ;17 97-086-18:45:53   STOL_MSG: Opened ion in /home/trace/procs/inst/ion.proc
    ;17 97-065-12:18:35   STOL_MSG: Opened th_heaters_off in /home/trace/procs/inst/th_heaters_off.proc
    arr = str2arr( strcompress(strtrim(mat(i),2)), delim=' ')
    ;
    time = strmid(arr(1), 7, 8)
    daytim = date + ' ' + time
    daytim = fmt_tim( anytim2ints(daytim, off=timezone))		;convert EST to GMT
    ;;if (fix(strmid(arr(1), 3, 3)) ne doy) then stop, 'DOY not match input date'
    ;
    cfl = arr(4)
    fid0 = str_replace(ex2fid( anytim2ex(daytim), /sec), '.', '_')
    fid0 = fid0 + '#' 
    out0(i) = fid0 + '       ' + cfl
end

skip:
outfil = '/td10/summary/tstol/cmd_hist_from_tstol'
if (keyword_set(out0)) then file_append, outfil, out0, /uniq
;
fid = ex2fid( anytim2ex(date) )
spawn, 'grep ' + strmid(fid, 0, 6) + ' ' + outfil, out0
if (out0(0) eq '') then return
;
if (out(0) eq '') then begin
    out = out0
    return
end
;
;if (out0(0) ne '') then out = [out, out0]
n1 = n_elements(out)
n2 = n_elements(out0)
out2d = strarr(2,n1)
out02d = strarr(2,n2)
for i=0,n1-1 do out2d(*,i) = str2arr( strcompress(out(i)), delim=' ')
for i=0,n2-1 do out02d(*,i) = str2arr( strcompress(out0(i)), delim=' ')
tim  = anytim2ints(fid2ex( reform(out2d(0,*)) ))
tim0 = anytim2ints(fid2ex( reform(out02d(0,*)) ))
qok = bytarr(n1) + 1
for i=0,n1-1 do begin
    ss = where(out02d(1,*) eq out2d(1,i), nss)
    ;;print, i, nss, '  ', out2d(1,i)
    if (nss ne 0) then begin
	;;junk = tim2dset(tim0(ss), tim(i), delta=delta)
	delta = abs( int2secarr(tim0(ss), tim(i)) )
	qok(i) = 1 - ( min(delta, imin) lt 60 )		;within 60 seconds means duplicated
	if (not qok(i)) then out0(ss(imin)) = str_replace(out0(ss(imin)), '# ', '#*')
    end
end
;
ss = where(qok, nss)
if (nss eq 0) then out = out0 else out = [out(ss), out0]
;
end
;--------------------------------------------------------
pro mk_hst_summary, date_in, out, outfil=outfil, outdir=outdir, usr_outdir=usr_outdir
;+
;NAME:
;	mk_hst_summary
;PURPOSE:
;	Make the history summary file for
;SAMPLE CALLING SEQUENCE:
;	mk_hst_summary, '15-Jan-97'
;	
;	for i=gt_day('1-oct-96'),gt_day(!stime) do mk_hst_summary, fmt_tim(i)
;	for i=gt_day('1-Mar-97'),gt_day(!stime) do mk_hst_summary, fmt_tim(i)
;HISTORY:
;	Written 20-Jan-97 by M.Morrison
;	 1-Apr-97 (MDM) - Added checking for TSTOL logged run time
;	 1-Apr-97 (MDM) - Added searching through EVENT files too
;	22-Apr-97 (MDM) - Modified alphabetical sort command
;-
;
;;if (n_elements(outdir) eq 0) then outdir = '/tsw/dbase/cal/summary/perf_hist/'
;
date_in = gt_day(date_in, /str)
;
fid = strmid(ex2fid(anytim2ex(date_in)), 0, 6)
infil = '$GSE_HISTORY_DIR/' + fid + '.hst'
if (n_elements(outdir) eq 0) then outdir = '/td10/summary/perf_hist/' + strmid(fid,0,4)
if (not file_exist(outdir)) then spawn, 'mkdir ' + outdir
outfil = concat_dir(outdir, fid + '.cfl_sum_cron')
;
print, 'Now processing:' + infil
;
break_file, infil, dsk_log, dir, filnam
;
tmp_fil = '/tmp/mk_hst_summary.tmp'
file_delete, tmp_fil
if (not file_exist(infil)) then goto, skip
spawn, 'grep perf ' + infil + ' | grep CMD_ECHO_TYPE > ' + tmp_fil
mat = rd_tfile(tmp_fil, nocomment='#')
if (mat(0) eq '') then n2 = 0 else n2 = n_elements(mat)		;where mat2 starts
spawn, 'grep SORT_CLI ' + infil + ' | grep "Executing proc" > ' + tmp_fil
mat2 = rd_tfile(tmp_fil, nocomment='#')
if (mat(0) eq '') then mat = mat2 else mat = [mat, mat2]
;
;  grep 'STOL_MSG: Opened' EVENT_97-086.DAT
;
file_delete, tmp_fil
if (mat(0) eq '') then goto, skip
;
date = gt_day( fid2ex(filnam), /str)
;
n = n_elements(mat)
out = strarr(n)
for i=0,n-1 do begin
    arr = str2arr( strcompress(strtrim(mat(i),2)), delim=' ')
    time = arr(2)
    ;
    cfl = arr(n_elements(arr)-1)
    cfl = str_replace(cfl, '"', '')
    ;
    ;;out(i) = date + ' ' + time + '     ' + cfl
    fid0 = str_replace(ex2fid( anytim2ex(date + ' ' + time), /sec), '.', '_')
    if (i ge n2) then fid0 = fid0 + '*' else fid0 = fid0 + ' '
    out(i) = fid0 + '       ' + cfl
    ;970103_182533       mk_ff_patch_v13.src
end

skip:

mk_hst_from_tstol, date_in, out

if (keyword_set(out)) then out = out(sort(out))
;
if (keyword_set(outfil) and keyword_set(out)) then prstr, out, file=outfil
;
;----------------------------------------------------
;
; alphabetical summary
;
outfil = concat_dir(outdir, strmid(fid, 0, 4) + '.cfl_sum_alph')
;cmd = 'cd ' + outdir + ' ; cat ' + strmid(fid, 0, 4) + '*.cfl_sum_cron  | sort +2 -3 +1 -2'
cmd = 'cd ' + outdir + ' ; cat ' + strmid(fid, 0, 4) + '*.cfl_sum_cron  | sort +0.15'
spawn, cmd, r
if (r(0) ne '') then prstr, r, file=outfil
;
;----------------------------------------------------
;
; cfl usr portion
;
indir = '/tsw/cfl/cdh_test/usr'
if (n_elements(usr_outdir) eq 0) then usr_outdir = '/td10/summary/usr_hist/' + strmid(fid,0,4)
if (not file_exist(usr_outdir)) then spawn, 'mkdir ' + usr_outdir
outfil = concat_dir(usr_outdir, fid+'.usr')
cmd = 'cd '+ indir + ' ; cat ' + fid + '*'
print, cmd
spawn, cmd, r
file_delete, outfil
if (r(0) ne '') then prstr, r, file=outfil


;
;----------------------------------------------------
; TSTOL "usr" log files
;
 indir = '/td10/summary/tstol'
outdir = '/td10/summary/usr_hist'
;
ff = file_list(indir, 'inst_test_log.*')
n = n_elements(ff)
break_file, ff, dsk_log, dir, filnam, ext
outfilnam = strmid(ext, 5, 2) + strmid(ext, 1, 2) + strmid(ext, 3, 2)
outdir2 = concat_dir(outdir, strmid(outfilnam, 0, 4))
outfil = concat_dir(outdir2, outfilnam + '.inst_test_log')
if (ext(n-1) eq '.txt') then outfil = outfil(0:n-2)	;drop inst_test_log.txt
;
n = n_elements(outfil)
for i=0,n-2 do begin
    cmd = ['cp', '-p', ff(i), outfil(i)]
    print, cmd
    spawn, cmd, /noshell
end
;
end