;+
;NAME:
;     	RD_TAR
;PURPOSE:
;       Program extracts tar archived files.  First the specific tape is
;	chosen from a list of names in the tar_lists directory.
;		currently in: '/0p/chen/tar_lists' 
;	The user then choses the files to be extracted.
;CALLING SEQUENCE:
;	RD_TAR [[, DEV= 0 or 1][, /MK_LIST]]
;INPUTS: 
;	dev:  device number  0= master drive
;			     1= copy drive			        
;OUTPUTS:
;	tar files are extracted into current directory
;	If /mk_list is entered, then a tar list is created, which can be used
;	by rd_tar later.
;KEYWORDS:
;	/mk_list:  creates tar list 
;KNOWN BUGS:
;	program not compatible with all tape drives
;	(definately works on SXT0 while extracting byte swapped files.)
;PROGRAMS/FUNCTIONS CALLED:
;	REFORM1	:  just like normal REFORM(array) but doesn't bomb with 1x1 
;			array
;	WNMENU_SEL :  nice mouse interface for choosing files
;NOTES:
;		RD_TAR reads tar listings which are made from the UNIX 
;	'tar -t' command.  The file names generated must be rather 
;	specific, so please follow these directions carefully.
;   		The minimum requirement is that the file name must be 17
;	characters long with the '_set#' if the tar file is not the
;	first set on the tape.
;   		The standard file name is as follows:
;	    	     EX: SFD-91_36a-1.SXT0_set2   
;                         |     |   |   |    |
;     		     file id    |   |   |    tape file number
;	        	        |   |   tape origin
;		  first file date   version of tape 
;
;	If an error comes up saying requested menu is too large for display, 
;	run 'wmenu_sel_set'.  (see wmenu_sel_set's own documentation)
;HISTORY:
;	MGC, Mar, 1993.
;	MGC, Apr, 1993  Checks if size is given in listing.  Added 'NOTES:'
;		Added user specified path.
;	MGC, Apr, 1993  Adds tar list making feature.
;-                              
pro rd_tar, dev=dev, mk_list=mk_list
tar_lists_path= '/0p/chen/tar_lists/'
spawn, 'pwd', current_path
current_path= strtrim(current_path(0), 2)+'/'
print, '>>>>>>>>>>> rd_tar ver. 1.01, 8-April-93 <<<<<<<<<<<<<'
print, ''
;
;if (!version.os eq 'IRIX') then command = '(the SGI syntax)' $
;                else command = '(the DEC syntax)'
;
IF (keyword_set(dev) eq 0) then BEGIN
  dev = 0
  tapdev = def_tapd(dev)                        ;default to device 0
ENDIF ELSE tapdev = def_tapd(dev)
;
print, '   >>>>Using Tape Device: ', tapdev,' <<<<'
print, ''
;
;makes tar listing if necessary
if keyword_set(mk_list) then begin
	print, '   RD_TAR reads tar listings which are made from the UNIX '
	print, '"tar -t" command.  The file names generated must be rather '
	print, 'specific, so please follow these directions carefully.'
	print, '   The minimum requirement is that the file name must be 17'
	print, 'characters long with the "_set#" if the tar file is not the'
	print, 'first set on the tape."'
	print, '   The standard file name is as follows:'
	print, '     EX: SFD-91_36a-1.SXT0_set2   '
	print, '          |     |   |   |    |'
	print, '     file id    |   |   |    tape file number'
	print, '                |   |   tape origin'
	print,   'first file date   version of tape '
	print, ''
	print, '(M)inimum or (S)tandard?'
	m_or_s=''
	read, m_or_s
	if (m_or_s eq 'S') or (m_or_s eq 's') then begin
		repeat begin
		  print, 'Enter three character file id  (Ex: GBO, SFD, SFR)'
		  file_id=''
		  read, file_id 
		endrep until (strlen(file_id) eq 3)
		repeat begin
		  print, 'Enter six character first file date  (Ex: 91_30a, 93_03a)'
		  date=''
		  read, date
		endrep until (strlen(date) eq 6)
		repeat begin
		  print, 'Enter one character version of tape  (i.e. Is this the 1st '
		  print, '  time this tape has been made? 2nd time? 3rd time?)'
		  version=''
		  read, version
		endrep until (strlen(version) eq 1)
		file_name=file_id+'-'+date+'-'+version
	endif else $
	repeat begin
		print, 'Enter 17 character file name'
		file_name=''
		read, file_name
	endrep until (strlen(file_name) eq 17)
	print, 'Enter tar set/file number <default=1(first tar set on tape)>
	set=''
	read, set
	if (set eq '') then set='1'
	file_name= file_name+'_set'+set
	yesnox, '>>Ready to create '+file_name+' ?', answer, 'Yes'
	if answer then begin 
		print, '   Generating '+file_name
		spawn, 'tar -tvf '+tapdev+ ' > '+file_name
	endif 
	return
endif
;choosing the tar set and the files to extract
repeat begin
	print, '>>Please choose the tar set  (tape name)<<'
	print, ''
	print, 'Enter path name (no file names)'
	print, '		(default= ' + tar_lists_path + ')'
	print, '                (     0 = ' + current_path + ')'
	path=''			
	read, path
	case path of
		''  : path= tar_lists_path
		'0' : path= current_path
		else: path= path(0) 
	endcase
	last_char= strmid(path, strlen(path)-1, 1)
	last_char= last_char(0)
	if (last_char ne '/') then path= path+'/' 
	spawn, 'ls '+path, ls_list

	select= wmenu_sel(ls_list, /one)

	tape= ls_list(select)
	tape= tape(0)
	infil= path + tape
	if (path eq tar_lists_path) then begin
		if (strlen(tape) eq 17) then set= 1 $
			else set= fix(strmid(tape, 21,1)) 
		origin= strmid(tape, 13,4)
	endif else set = 1
        spawn, 'wc -l ' + infil, line_count
	spawn, 'wc -w ' + infil, word_count
	words_per_line= (word_count(0)*1)/(line_count(0)*1)
	openr, lun, /get_lun, infil

	junk= strarr(words_per_line, line_count(0))

	lin=' '
	for i=0, line_count(0)-1 do begin
		readf, lun, lin
	        junk(*,i)= str2arr(strcompress(lin), delim=' ')
	endfor
	close, lun

	file_numbers= wmenu_sel(junk(words_per_line - 1,*)) 
        answer=2
	if (file_numbers(0) eq -1) then begin
	  	print, 'You did not pick a file, '
	       	yesnox, 'Do you want to try a different set?', answer, 'Yes'
	endif 
	if (answer eq 0) then return
endrep until (answer eq 2)

;the only data needed from structure 'info' is the size and name
;the rest has yet to be implemented into the program
info= replicate({file_info, $
	protection: ' ', $
	numbers: ' ', $
	size: 999999, $
	date: ' ', $
	time: ' ', $
	name: ' '}, n_elements(file_numbers))
if (n_elements(file_numbers) eq 1) then file_numbers=file_numbers(0)
info.protection= reform1(junk(0, file_numbers))
info.numbers= reform1(junk(1, file_numbers))
if (origin ne 'SXT0') then begin
	info.size= -1
	info.date= reform1(junk(5, file_numbers)+' '+junk(2, file_numbers) $
		+' '+junk(3, file_numbers))
	info.time= reform1(junk(4, file_numbers))
	info.name= reform1(junk(6, file_numbers))
endif else begin
	info.size= reform1(junk(2, file_numbers))
	info.date= reform1(junk(6, file_numbers)+' '+junk(3, file_numbers) $
		+' '+junk(4, file_numbers))
	info.time= reform1(junk(5, file_numbers))
	info.name= reform1(junk(7, file_numbers))
endelse
total_size= total(info.size)/1000
if (total_size gt 0) then begin
	print, '******* File Sizes in Kbytes *******'
	print, 'File Name                    File Size   '
	print, '------------------------------------------'
	blank= '                   '
	for i=0, n_elements(info)-1 do $
      		print, info(i).name, blank, info(i).size/1000
	print, '------------------------------------------'
	print, ' Total	                	', total_size		
endif

print, 'Confirm that you have tar tape labelled '+ tape
yesnox, 'Continue?', answer, 'Yes'
if (answer eq 1) then begin
	if (set ne 1) then  begin
		print, 'mt -t ' + tapdev + ' fsf '+ strtrim((set - 1),1)
		spawn, 'mt -t ' + tapdev + ' fsf '+ strtrim((set - 1),1)
	endif
	names=' '
	for i=0, n_elements(info)-1 do names= names+info(i).name+' '
	print, 'tar -xvf '+ tapdev + names
	spawn, 'tar -xvf '+ tapdev + names
endif 
return
end
