function adsdirs, weekid, doq=doq, nodel=nodel, nweeks=nweeks , $
	 actual=actual, week_str=week_str, all=all
;
;+
;   Name: adsdirs
; 
;   Purpose: return current ads directory queue pathnames
;	     optionally make new and delete old if week rollover
;
;   Input Parameters:
;      weekid  - (optional) week directory to create
;  
;   Output:
;      function returns ads queue path , string array of n elements where
;		 element(0) - oldest and element(n-1) is newest
;
;   Optional Keyword Parameters:
;      doq    -  if set, make directories which dont exist and delete old
;      nodel  -  if set, dont delete oldest event if mknew is set
;      nweeks - number of weeks in queue (default is most recent 4 weeks)
;      all    -  If set, then return the "old" week directories too
;
;   Calling Sequence:
;      adsq=adsdirs()			; current ads queue pathname array
;      adsq=adsdirs(/doq)		; make new queue entry if required
;      adsq=adsdirs(/doq,/nodel)  	; same but dont delete oldest
;      adsq=adsdirs(t0,nweeks=1,/doq)   ; make a non-standard dir (ex catchup)
;      adsq=adsdirs(/actual)		; read all ads queue (new + catchup)
;
;   Categories:
;      pointing, site , system, swmaint
;
;   History:
;       slf - 21-oct-92
;	mdm - 29-Mar-93 - Added /ALL keyword option
;			- Also added $DIR_SITE_ADSFTP2
;       slf -  1-apr-93 - replace dir search logic with call to dir_list.pro
;-
; setup keywords...
if n_elements(nweeks) eq 0 then nweeks=4 	;queue default length = 4 weeks
;
; dont delete if time is supplied (assume catchup or old data)
delete=(1 - keyword_set(nodel)) and (keyword_set(doq) and $
	(n_elements(weekid) eq 0))

if n_elements(weekid) eq 0 then $		; default new week is now
   weekid=syst2ex()

; use environmental adsftp for ada directory queue
toplev=getenv('DIR_SITE_ADSFTP')
dummy = getenv('DIR_SITE_ADSFTP2')			;MDM added 29-Mar-93
if (dummy(0) ne '') then toplev = [toplev, dummy]	;MDM added 29-Mar-93

ads_cur=''
; form directory ids for ads queue pathnames (one per week)
if nweeks ne 0 and n_params() eq 0 then begin
   reftime=anytim2ints(weekid)		; t0
   reftimearr=replicate(reftime,nweeks)	; number paths = queue length
   offsets=-(lindgen(nweeks)*7.*24.*60.*60.)	; consecutive weeks
   weektimes=anytim2ints(reftimearr, off=offsets)  ; get 
   tim2orbit, weektimes, wid=wid			; wid is in form yy_ww
   ;ads_cur=concat_dir(toplev,('ads' + wid))
   ads_cur=concat_dir(toplev(0),('ads' + wid))		;MDM added 29-Mar-93
endif 


; find existing queue pathnames 
filprefix = 'ads9*'						;MDM added 29-Mar-93
if (keyword_set(all)) then filprefix = [filprefix, 'oldads9*']	;MDM added 29-Mar-93

; slf, 1-apr-93 fix case where ads top level only contains 1 direcory?
ads_exist=dir_list(toplev, filprefix, count=ncount)

if n_params() eq 1 then begin		; after thought
   doq=1
   delete=0
   wid=weekid
   ads_exist=''
   ;;ads_cur=concat_dir(toplev,('ads' + wid))
   ads_cur=concat_dir(toplev(0),('ads' + wid))		;MDM added 29-Mar-93
endif

; make current ads queue paths which dont already exist...
if keyword_set(doq) then begin
   newest=n_elements(wid)-1			; index of most recent
   for i=0,newest do begin
      curchk=where(ads_cur(i)  eq ads_exist, curcount)
      if curcount eq 0 then begin
         mkdircmd='mkdir -p ' + ads_cur(i)
         message,/info,'Creating new ads directory via: ' + mkdircmd
         spawn,mkdircmd
	 spawn,'chmod 777 ' + ads_cur(i)
      endif
   endfor
endif

; delete ads queue entries (paths and ads files) if so directed
if delete then begin
   for i=0, n_elements(ads_exist)-1 do begin
      existchk=where(ads_exist(i) eq ads_cur,existcount)
      if existcount eq 0 and ads_exist(i) ne '' then begin
         rmdircmd='rm -fr "' + ads_exist(i) + '"'
         message,/info,'Removing old ads directory via: ' + rmdircmd
         spawn,rmdircmd
      endif
   endfor
endif

retval=ads_cur				; default return is nominal queue

; if all ads directories requested, then repeat search 
; (retval includes all catchup/old and current queue paths)
if keyword_set(actual) then begin	; may differ from nominal (old data)
   retval=dir_list(toplev, filprefix, count=ncount) ; slf 1-apr-93
   notnull=where(retval ne '',ncount)
   if ncount gt 0 then retval=retval(notnull)
endif

; now time order (oldest in element(0))
case 1 of
   n_elements(retval) gt 1: begin
      retval=retval(uniq(retval,sort(retval)))
      retval=retval(sort(retval))
   endcase
   nweeks eq 0: retval=''
   else:
endcase

; if week_str is past, look for matching directory and return that one
if keyword_set(week_str) then begin
   weekdir=where(strpos(retval,week_str) ne -1,count)
   if count eq 0 then retval='' else retval=retval(weekdir(0))
endif

return, retval
end
