pro diskclean, path, byday=byday, byfid=byfid, noconfirm=noconfirm, $
	nodelete=nodelete, filter=filter, move=move
;
;   Purpose: menu yohkoh reformatted disk file deletion utility
;
;   Input Parameters:
;      path - pathname
;
;   Optional Keyword Parameters:
;      byday -  if set, removes full days with each command
;      byweek - if set, removes full weeks with each command
;      byfid -  if set, removes by fileid (default)
;      noconfirm - if set, a last chance abort is bypassed
;      nodelete - if set, delete commands are not spawned (test mode)
;      filter - string with file filter (not neccessarily Yohkoh data!)
;      move   - directory name to move files too (if switch, will prompt)
;
;   Calling Sequence: 
;	diskclean [,path ,/byday , /byfid, /byweek] for Yohkoh Reformatted Data
;	diskclean [,path ,/byweek, /move]           move (not delete)
;	diskclean [,path, filter=filefilter]	    for files of any type
;
;   Category:
;      system, disk, gen 
;
;   History: slf, 7-Aug-92 (motivation - clean up /yd3/reformat)
;
;   WARNING: THIS ROUTINE CAN DESTROY DATA
;
;   Method: use existing routines
;
;-
; default is temporary reformat
if n_elements(path) eq 0 then path='/yd3/reformat'
;
message,/info,'Searching files on files on: ' + path
;
; do keyword setup
;
delok = keyword_set(noconfirm)		; ok to delete without a question
;
dodelete = 1 - keyword_set(nodelete)	; cleaner logic

; set up file filters
; if the user passed a file filter use that instead of Yohkoh data specific
if keyword_set(filter) then $
   spawn,'ls -ac1 ' + concat_dir(path,filter),filelist $
else begin
   case 1 of 
      keyword_set(byday):filelist='*' + fidrange(path,/day) + '*'
      else: filelist='*' + fidrange(path)
   endcase
   filelist=concat_dir(path,filelist)		; add path back on 
endelse
;
;
on_error,2
if filelist(0) eq '' then $
   message,'No matching files found on: ' + path + ' 
on_error,0

; instructions (brief)
print,'Select Files For Deletion'
if not keyword_set(noconfirm) then $
   print,'You may abort after selection if you wish'
;
; offer menu for selection
whichf=wmenu_sel(filelist)

if not keyword_set(move) then move=''

smove=size(move)
if smove(1) ne 7 then begin
   move=''
   read,'Enter destination directory for move: ',move
endif
 
while keyword_set(move) and 1-file_exist(move,/dir) do begin
   print,'Output directory: ' + move + ' not found'
   yesnox,'Do you want to create it? ',ans, 'Y'
   if not ans then message,'aborting....'
   spawn,'mkdir -p ' + move,out
   if out ne '' then message,'Error creating directory, aborting...'
endwhile
   
cmdtype= ['Delete','Move']
spawnpre=['rm -f ', 'mv -f ']

if whichf(0) eq -1 then begin
   message,/info,'No files selected, Exiting'
endif else begin
   spawncmds=spawnpre(keyword_set(move)) + filelist(whichf) + $
	' ' + move
;  one last chance for the meek
   if not delok then begin
      print,'The following ' + cmdtype(keyword_set(move)) + $
         ' commands are contemplated:'      
      prstr,spawncmds
      yesnox,'Continue with ' + cmdtype(keyword_set(move)) ,delok
   endif
   if delok then begin
;  we are committed now (unless /nodel set).... theres alaways a <CTRL>-C
      for i=0,n_elements(spawncmds)-1 do begin
         message,/info,spawncmds(i)
         if dodelete then spawn,spawncmds(i),out
       endfor
   endif else message,/info,'Aborting delete on user request'
endelse
return
end
            
