pro mk_mapfile, sw=sw, data=data, outfile=outfile
;
;+
;   Name: mk_mapfile
;
;   Purpose: create/update generic file which maps files into pathnames
;
;   Input Parameters:
;
;   Optional Keyword Paramters:
;	sw	- if set, the current $ys... idl routines are mapped (default)
;	data	- if set, the current online data is mapped
;
;   Category:
;       system, swmaint, unix_only
;
;   History - slf,  8-Aug-1992
;	      slf, 19-nov-1992	; some upgrades for data path function
;	      slf,  7-jan-1993  ; use DIR_GEN_SETUPD (DIR_GEN_SETUP/data)
;	      slf, 13-Jan-1993  ; use DIR_SITE_SETUPD if exists
;	      slf, 13-may-1993  ; exclude /ys/atest_NNNNN directories
;
;   Motivation - location of reformatted data sets and software modules are
;	not so dynamic to justify repeated (and slow) execution of findfile.
;       Instead, the file-pathname relation ship can generally be mapped 
;	once and remain valid for the session.   Associated routines would 
;	read these files on the first access into a common area.  Subsequent 
;	calls are via a table lookup for high efficiency.
;		 
;   Calling Sequence:
;     	mk_mapfile [, /sw, /data]
;
;
; *** this routine should be run as a cron job (via idl_batch) a 
;     few times a day
;
;  Restrictions: unix only.  Assumes links are used for software (/ys) and
;	data directories (/ydnn).  Should be upgraded to allow environmentals
;       and VMS logicals for these
;-
;
;
; default files are assumed to be on $DIR_GEN_SETUPD (=DIR_GEN_SETUP/data)
deffile=concat_dir('$DIR_SITE_SETUPD', ['swmap.genx','datamap.genx'])


sw=keyword_set(sw) or ( 1-keyword_set(sw) and 1-keyword_set(data) )
;
filemap=[0]				; initialize map array

; define specfic ls command(s) in case
case 1 of
;
   keyword_set(sw): begin			; software paths
      filetype=0
      delims=[':',',']     
      paths=str2arr(!path,delims(strlowcase(!version.os) eq 'vms'))
      out=''
      for i=0, n_elements(paths)-1 do begin
         filesi=findfile(paths(i),count=fcount)
         if fcount gt 0 then begin
            out=[out,filesi]
            filemap=[filemap, intarr(fcount)+i]
         endif
      endfor
      out=out(1:*)            
      mapfiles=indgen(n_elements(out))
   endcase
;
;  for data file mapping
   keyword_set(data):begin			   ; reformatted data files
      filespecs=['???9?????.????','%%%9%%%%%.%%%%']
      wspec=filespecs(strlowcase(!version.os) eq 'vms')
      out=file_list(data_paths(), wspec) 
      break_file,out,log,paths,files,versions
      files=files+versions
      paths=log + paths
      upaths=uniq(paths)      
;     make map 
      for i=0,n_elements(upaths)-1 do begin
         whichp=where( strpos(out,paths(upaths(i))) ne -1, count)
         filemap=[filemap,intarr(count)+i]
      endfor
      out=files
      paths=paths(upaths)			   ; only keep unique paths
      mapfiles=indgen(n_elements(files))		   ; 
      filetype = 1
   endcase
;
   else: begin					; misc cases added later?
      on_error,2
      message,'No file type supplied, returning...
   endcase
endcase

filemap=filemap(1:*)				; eliminate initial value


; Now write the map file using 'generic' format
if not keyword_set(outfile) then outfile = deffile(filetype)
;
; check protections?
spawn,'rm -f outfile'
message,/info,'Writing generic file: ' + outfile
; now write the generic file
savegen, paths, out(mapfiles), filemap, file=outfile,  $
	 text='fullfile(n)=p1(p3(n))+p2(n)', $
	 names=['paths','files','map']   	
chmodcmd='chmod 666 ' + outfile
spawn,chmodcmd

return
end
