pro trace_last_movie_queue, queue_days, topdir=topdir, testing=testing, $
			    force_index=force_index, prefix=prefix
;+
;   Name: trace_last_movie_queue
;
;   Purpose: maintain queue of TRACE NRT movie directories
;
;   History:
;      Circa June 8, 1998 - S.L.Freeland -
;      1-June-1999 - S.L.Freeland - allow mult dir/day , "offline"->"online"
;          move last_movies  -> last_movies_yyyymmdd_hhmm
;          move last_moviesx -> last_movies
;      8-June-1999 - S.L.Freeland - add call to trace_movie_qkl_queue
;-

testing=keyword_set(testing)
if not keyword_set(prefix) then prefix='last_movies'

if n_elements(queue_days) eq 0 then queue_days=5
if not data_chk(topdir,/string) then topdir=get_logenv('path_http')

tpat='????????_????'
movie_dirs=file_list(topdir,prefix+'_'+tpat,/cd)
nmd=n_elements(movie_dirs)

; delete contents and directory any older than queue_days
for i=0, nmd-queue_days-1 do begin 
   cmd='rm -rf '+ movie_dirs(i)
   box_message,['Removing: ' + movie_dirs(i),cmd]
   if not testing then spawn,cmd
endfor

tempdir=prefix+'_temp'
ondir=concat_dir(topdir,prefix)             ; "online" 
lmdir=concat_dir(topdir,tempdir)            ; "most recently generated"

if file_exist(lmdir) then begin
   chk=file_list(lmdir,'*',/cd)
   if n_elements(chk) gt 20 then begin               ; cutoff
      nowdir=ondir + '_' + time2file(reltime(/now))    ; new name
      mvcmd=['mv','-f',ondir,nowdir]
      if testing then box_message,mvcmd else spawn,mvcmd,/noshell
      mvcmd=['mv','-f',lmdir,ondir]
      if testing then box_message,mvcmd else spawn,mvcmd,/noshell
      if not testing then trace_movie_qkl_queue,/insert
   endif else box_message,'Too few files, Not moving ' + lmdir
endif else box_message,'Directory: ' + lmdir + ' not found, Not moving ' + lmdir
if testing then stop
return
end
