function path_sw, module, count, nopath=nopath, nopro=nopro, swdirs=swdirs, $
				 multi=multi, exact=exact, readfile=readfile
;
;+
;   Name: path_sw
;
;   Purpose: return the path to on-line software using mapfile contents
;
;   Input Parmeters:
;      module 	- software module name or substring (.pro file, script, etc)
;
;   Optional Keyword Parameters:
;      nopath - if set, only return the module names (no path info)
;      nopro  - if set, all file types in path matching module are returned
;      multi  - if set, all occurences are returned (default is first)
;               (this should be enhanced to use !path to determine order)
;      readfile - force reading of file and regeneration of common block
;      swdirs - if set, return $ys directories (ignore module, if present)
;
;   Output Parameters: function returns full path specification 
;      count - number of matches found
;	
;   Calling Sequence: 
;	fullpath=path_sw(module [,count , /nopath , multi, /nopro])
;
;   Category:
;      system, swmaint, util, gen
;
;   Method - on first call, reads generic file $DIR_GEN_SETUP/data/swmap.genx
;	     into a common block - once common is established, finding module
;	     is via. table lookup
;
;   History: slf, 8-Aug-92
;	     slf, 7-Jan-93	use DIR_GEN_SETUPD (=DIR_GEN_SETUP/data)
;            			call wc_where to find pattern match
;	     slf, 29-jan-93	use rd_genx instead of restgen (faster)
;  
;-
common	path_sw_blk, psw_paths, psw_modules, psw_map
common  path_sw_blk2, file_create
;
if n_elements(file_create) eq 0 then file_create=''
rd_genx,concat_dir('$DIR_SITE_SETUPD','swmap.genx'), /nodata, header=header
reread=file_create ne header.creation
readfile=keyword_set(readfile) or reread
; set up common on first call or on request
if readfile then begin	
   messpre=['','re-']
   whichpre=messpre(file_create ne '')
   message,/info,whichpre + 'reading software map file...
;  slf, 29-jan-1993 - use rd_genx, slightly faster then restgen
   rd_genx,concat_dir('$DIR_SITE_SETUPD','swmap.genx'), mapstruct   
   psw_paths=mapstruct.paths			; assign common
   psw_modules=mapstruct.files
   psw_map=mapstruct.map
   file_create=header.creation
endif

;
if keyword_set(swdirs) then return, psw_paths

outarr=''					; initialize to null
inmod=module	

if  not keyword_set(nopro) then inmod= $	; default is idl source code
      str_replace(inmod,'.pro','') + '.pro'

success=wc_where(psw_modules,inmod,count,/case_ignore)

; strip out duplicates unless specifically requested
if not keyword_set(multi) and count gt 0 then begin 
   outarr=psw_modules(success)				;subarray to uniqify
   success=success(uniq(outarr,sort(outarr),/first))	;update subscripts
   count=n_elements(success)				;update output count
endif
      
; success vector is fully established, now define the output format
if count gt 0 then begin				; I am asking this way
;					        	; way  too frequently
   outarr=psw_modules(success)				; module names
   if not keyword_set(nopath) then outarr= $		; default adds path
	concat_dir(psw_paths(psw_map(success)),outarr )   
endif
if n_elements(outarr) eq 1 then outarr=outarr(0)	; force scaler
return,outarr
end
