pro rd_index, lun_filnam, ibyt0, index
;
;
;HISTORY:
;	Written Fall '91 by M.Morrison
;	19-Mar-92 (MDM) - Changed to only read the roadmap if
;			  the "ibyt0" is not defined.  Before, it would
;			  always read the roadmap if the "lun_filnam" was
;			  the filename.
;	19-Mar-92 (MDM) - Modified to maintain a common block of the structures
;			  already defined and only call "make_str" if it is a
;			  new structure.
;	 4-May-92 (MDM) - Changed to trap when entry_type was zero only
;			  when it didn't find any good version numbers
;			  (fixed case of SXT GND structure with ver=0)
;	 3-Feb-94 (MDM) - Added call to bsc_old_struct and bsc_struct
;
common rd_index_blk, make_str_arr, str_name_arr
;
hxt_struct
sxt_struct
sxt_old_struct
gen_struct
gen_old_struct
bcs_struct
bcs_old_struct
wbs_struct
att_struct
cba_struct
bsc_struct
bsc_old_struct
;
qdebug = 0
;
siz = size(lun_filnam)
typ = siz(siz(0)+1)
if (typ eq 7) then begin               ;passed file name
    get_lun, lun
    openr, lun, lun_filnam, /block
end else begin
    lun = lun_filnam
end
if (n_elements(ibyt0) eq 0) then begin		;19-Mar-92
    rd_roadmap, lun, roadmap
    ibyt0 = roadmap(0).byteskip		;take first dataset by default
end
;
ibyt = ibyt0
;
buff = intarr(8)	;16 bytes
qfirst = 1
qdone = 0
nbyte_out = 0
while (not qdone) do begin
    rdwrt, 'R', lun, ibyt, 0, buff
    entry_type = buff(0)

    if (entry_type ne 0) then begin	
	if (qdebug) then print, ibyt, string(entry_type,'(2x,z8.8)')

	get_index, entry_type, str_name, n_bytes, tag_name

	if (qdebug) then print, ibyt, entry_type, str_name, n_bytes

	if (qfirst) then begin
	    str_list = str_name
	    tag_list = tag_name
	    qfirst = 0
	    ;
	    exe_stat = execute( 'gen_index = ' + str_name )
	    rdwrt, 'R', lun, ibyt, 0, gen_index
	    nindexbyte = gen_index.nindexbyte			;find out how many bytes are supposed to be read
	end else begin
	    str_list = [str_list, str_name]
	    tag_list = [tag_list, tag_name]
	end
	ibyt = ibyt + n_bytes
	nbyte_out = nbyte_out + n_bytes
    end else begin
	qdone = 1
	if (nbyte_out eq 0) then message, 'Unrecognized index structure type. Must be corrupted file. Stopping'
    end
    if (nbyte_out ge nindexbyte) then qdone = 1
    ;TODO - check # of index structures in fheader to check if read them all
end
;
n = n_elements(str_list)
str = '{dummy'
nout = 1
for i=0,n-1 do begin
    qwrite = 0
    if (i eq n-1) then qwrite =1  else if (str_list(i+1) ne str_list(i)) then qwrite = 1 ;write out structure if at end of list
											;or next struct is not the same as current
    if (qwrite) then begin
	;what to do about WBS where the tag name will repeat
	ss = -1
	if (i ne 0) then ss = where(tag_list(0:i-1) eq tag_list(i))
	if (ss(0) ne -1) then extra = strtrim(n_elements(ss)+1,2) else extra = ''
	;For WBS case where there are two sets of GEN and WBS records, but not in a row
	;TODO - If there is an array of one type of structure, and then a different one, and then a repeat of the prior one,
	;TODO - we only want the tag to be "2" - need to call "unique" to see next SERIES of repeats
	str = str + ', ' + tag_list(i) + extra + ':replicate(' + str_list(i) + ',' + strtrim(nout,2) + ')'
	nout = 1
    end else begin
	nout = nout + 1
    end
end
str = str + '}'
;
qnew = 1
qfirst = (n_elements(make_str_arr) eq 0)
if (not qfirst) then begin
    ss = where(make_str_arr eq str, count)
    if (count ne 0) then begin
	cmd = 'index = {' + str_name_arr(ss(0)) + '}'
	stat = execute(cmd)
	qnew = 0
    end
end
if (qnew) then begin
    index = make_str(str, str_name=str_name)
    if (qfirst) then begin
	make_str_arr = str
	str_name_arr = str_name
    end else begin
	make_str_arr = [make_str_arr, str]
	str_name_arr = [str_name_arr, str_name]
    end
end
;
rdwrt, 'R', lun, ibyt0, 0, index 
;
if (typ eq 7) then free_lun, lun
end
