function sw_head,  path, purpose=purpose, print = print
;
; 	NAME: SW_HEAD
;
;	PURPOSE: extract documentation headers from idl procedures
;
;	OPTIONAL INPUT:
;		 path = directory for search 
;		 purpose=purpose - only name and purpose
;
;	Method: quick and dirty
; 
;	Restrictions: unix only x 
;
;	Modification History: SLF, July 30, 1991
;-
if n_elements(path) eq 0 then $ 
	path='/0p/sxt/soft/rel'			;slf 10/31/91
files=findfile(path + '/*')
files=files(where(files ne ''))			; get rid of nulls
purpose_limit=4					; n lines limit
purpose_found=0					; 
lines=0

line = ''					
term_pattern=[';-','INPUT:']
terminate=term_pattern(keyword_set(purpose))		;when to stop
purpose=keyword_set(purpose)
;
keywords=['CALL', 'INPU','OUTP','CATE']			;1st guess
logging = keyword_set(print) 
if logging then openw,unit0,/get_lun,'sxt_sw.doc' else $
   unit0 = -1 						; terminal out
;
for i=0, n_elements(files)-1 do begin
   if strpos(files(i),'.pro') lt 0 then begin	; probably subdirectory
         colon=strpos(files(i),':')
	 if colon ge 0 then begin
	    path = strmid(files(i),0,colon) ; extract pathname
	    printf,unit0,format='(//,2a)','Current Path is ',path
	 endif
      endif else begin      
         openr,unit,/get_lun, path + '/' + files(i)
         ; printf,unit0,'File: ',path,+'/'+ files(i)
         readf,unit,line
;
; skip uninteresting header lines
	 skip=1
	 while not eof(unit) and skip do begin 
           skip = strpos(line,';+') lt 0
           skip=skip and strpos(strupcase(line),'NAME:') lt 0
	   readf,unit,line
	 endwhile
;
	 if not eof(unit) then printf,unit0,format='(//)' 
;
 	 purpose_found=0
	 purpose_count=0
	 key_pos=-1
	 keytest=-1
	 next_key=0
;
; a bunch of kludges to determine when to terminate header output
         while (not eof(unit)) and (strpos(strupcase(line),terminate) lt 0) $
	    and (purpose_count lt purpose_limit)  $
	     and (keytest(0) lt 0) do begin
;
;
	    ptest=strpos(strupcase(line),'PURPOSE') 
;
	    if ptest ge 0 then begin 
 	       purpose_found=1
	       key_pos=ptest
	    endif
;
	    if purpose and purpose_found then $
		purpose_count=purpose_count + 1		    ; enough 
;
            if strpos(line,';') eq 0 then begin $
		strput,line,' ',0
		printf,unit0,line
	    endif			                    ; print
;
            readf,unit,line				    ; get next
;
; try to determine if a keyword is present
	    if (key_pos ge 0 ) and purpose then begin 
	       next_key=strupcase(strmid(line,key_pos,4) )
	       keytest=where(next_key eq keywords)
	    endif	
         endwhile
         free_lun,unit
      endelse
endfor
if logging then free_lun, unit0
return,files
end
