pro trace_order_data, success_file, email=email, background=background, $
		      nomail=nomail, outfiles=outfiles
;+
;   Name: trace_success_file
;
;   Purpose: success file->data order FORM (fg) FORM order->data (background)  
;
;   Input Parameters:
;       success_file - ascii file list of successes from catalog search
;                      (from trace_idlcat.pro)
;   Side Effects:
;      if foreground, FORM echoed to user  (assumed at WWW browser)
;      if background, Data -> dropoff/pickup
;
;   History:
;       18-May-98  S.L.Freeland
;       19-May-98  S.L.Freeland - email , extend FORM 
;       20-May-98  S.L.Freeland - background task (order->pickup)
;       26-May-98  S.L.Freeland - add some additional tracking
;       27-May-98  S.L.Freeland - add /EXTEN to write_fits call
;       19-Feb-98  S.L.Freeland - email rationalize
;-  

box_message,success_file
; -------- use the FILE_DSET to link image/html documents ---------
file=(strsplit(success_file,'_',tail=imgss))(0)
break_file,success_file,ll,pp,ff,ee,vv
sfname=ff+ee                              

hname=success_file+'.html'
contents=rd_tfile(success_file)
query=where(strpos(contents,';') eq 0,qcnt)
if qcnt gt 0 then begin
   qcontents=str_replace(contents(query),';','')
   contents=strnocomment(contents,comment=';',/remove_nulls) 
endif

nimgs=n_elements(contents)             ; number of images
fdset=str2cols(contents)               ; files/dset 
hfiles=reform(fdset(0,*))              ; hourly files -> strarr
dsets=fix(reform(fdset(1,*)))                 ; dsets -> intarr
uhour=uniq(hfiles)                     ; uniq hourly (assume sorted)
nhour=n_elements(uhour)                ; number uniq hourly

background=keyword_set(background)     ; cron job sets this for serial process.

order_html=''
order_template=concat_dir(concat_dir('path_http','Data'),'order_data.template')

if file_exist(order_template) then begin
   order_html=rd_tfile(order_template)
endif  else begin
   box_message,'No html template: ' + order_template  
endelse

if data_chk(email,/scalar,/string) then $
    order_html=str_replace(order_html,'EMAIL',email(0)) else $
    email = 'freeland@sxt1.lmsal.com'                           ; problem??

stat_html=['Order includes a total of <br><select name="number_images">', $
              '<option selected><b><font color=red>' + strtrim(nimgs,2),'</font></b></select>', $
              ' images in <select name="number_hourly">', $
              '<option selected><b><font color=red>' + strtrim(nhour,2),'</font></b></select>', $
	      ' uniq hourly files']

succ_html=['<br><select name="success_file">', $
	   '<option selected>' + success_file,'</select>']

emailstr='email: '

if background then begin 
  ssemail=(where(strpos(strlowcase(qcontents),strtrim(emailstr,2)) ne -1 and $
		strpos(qcontents,'@') ne -1, emcnt))(0)
  if emcnt gt 0 then begin
     email=(strsplit(qcontents(ssemail),':',/tail))(0)
     box_message,'EMAIL: ' + email
  endif else begin 
      box_message,'Problem gettin email address'
      email='freeland@sxt1.lmsal.com'
  endelse      
  userid=strtrim((strsplit(email,'@',/head))(0),2)
  pickup_top=get_logenv('TRACE_DATA_PICKUP')
  if pickup_top eq '' then begin
    pickup_top=pp
    box_message,['Environmental $TRACE_DATA_PICKUP not defined...', $
		 'Using top level: ' +pickup_top]
  endif
  outdir=concat_dir(pickup_top,userid)
  spawn,['mkdir','-p',outdir],/noshell               ; make user subdirectory
  trace_data_request_mirror,userid                   ; make mirror job
  typestr='OUTPUT_TYPE:'
  sstype=(where(strpos(qcontents,typestr) ne -1, tcnt))(0)
  if tcnt gt 0 then flat=strpos(qcontents(sstype),'flat') ne -1
    
  outfiles=''
  for i=0,nhour-1 do begin                   ; loop by hourly file
    ss=where(hfiles eq hfiles(uhour(i)))
    infile=trace_file2path(hfiles(uhour(i)),online)
    if online(0) then begin     
       read_trace,infile,dsets(ss),index,data
       delvarx,outfile                         ; return auto-named filelist
       write_trace,index,data, outdir=outdir, flat=flat, outfile=outfile,/exten
       outfiles=[outfiles,outfile]
    endif else box_message,['File: ' + hfiles(uhour(i)) + ' is not online']
  endfor
  if n_elements(outfiles) gt 1 then outfiles=outfiles(1:*) ; return output list  
endif else begin 
  file_append,success_file,$
     [';;; trace_order_data: run at ' + systime(), ';;; ' + emailstr + email, $
      ';;; Author: S.L.Freeland, LMSAL <a href="mailto:freeland@sxt1.lmsal.com">freeland@sxt1.lmsal.com</a>',$
      ';;; Last Revision: 26-May-1998'     ]  ; append status
  html_doc,/header,hname
  file_append,hname,'<em>Generated by <b>trace_order_data.pro</b>' +  $
     ', Last Revision, S.L.Freeland, 26-May-1998</em>'
  file_append,hname,order_html  
  file_append,hname,stat_html
  file_append,hname,succ_html
  if qcnt gt 0 then file_append,hname,$
                strtab2html(transpose(['Catalog Query',qcontents]),/left,/row0)
  file_append,hname,'<h4>Total Number of Images: <b><em>'+strtrim(nimgs,2)+'</em></b></h4>'
  file_append,hname,['<pre>',contents,'</pre>']
  html_doc,/trailer,hname
  
endelse
  
return
end
