	PRO RdTap2, dev, file, prefix = prefix, fileID = fileID, $
		week = week, dir = dir, range = range, software =software, $
		swpos = swpos, dpath = dpath, wpath = wpath, put_ydb=put_ydb, $
		script = script, alpha = alpha, edit = edit, noexec = noexec, $
		debug = debug, notape = notape, unload = unload
;	-------------------------------------------------------------
;+							21-Apr-95
;	NAME:
;		RdTap
;	PURPOSE:
;		Read ARCHIVE tape and write the requested files to
;		the default directory.
;	CALLING SEQUENCE:
;		rdtap, dev [,files, prefix=prefix,fileid=fileid,week=week,
;			dir=dir, range=range, software=software, 
;			swpos=swpos, dpath=dpath, wpath=wpath]
;	INPUT:
;		dev	device name of the tape unit.
;		file 	a list of requested file names.
;		prefix	a list of file prefixes.
;		fileID	a list of fileIDs or archive files.
;		week	list of weekly files to restore (eg. obs, pnt)
;		dir	flag present to create both TAPE directory files
;			on default dir or specified dir.  If specified
;			dir = log then xad=>$dir_gen_xad and $xbd=>dir_gen_xbd
;		range	flag present to create range of files 
;		software flag present to restore software tree
;		swpos	flag to position tape to software tar files & ret
;		dpath	dir specification for restored data files
;		wpath	dir specification for restored weekly files
;		put_ydb	if present will restore requested weekly files to
;			the appropriate $DIR_GEN_* directories.
;		script	generate a script file to do the tape extraction
;		alpha	generate a script file to do the tape extraction
;			with alpha specific commands.
;			(note this might be better to call it machine_type)
;		edit	switch to allow user to edit (vi) the script
;			before execution
;		noexec	switch to allow user to start the execution of the
;			script, otherwise the script is started automatically.
;		notape	force rdtap to read an online tape-dir file
;			instead reading it off tape.
;		unload	switch to request a tape unload command be placed
;			in the script.  Default is no-unload and rewind.
;
;
;	HISTORY:
;		written Nov-91, gal
;		update 27-Feb-92 (ver2.0) for new weekly files archive 
;			structure, creates both types of tape dir. files 
;			with /dir.
;		2-Mar-92 update to prefixck w/o obs log file.
;		6-Mar-92 whole-Lot-A-Changes
;		9-Mar-92, see update to rd_tapdir, and opens binary
;		tape-dir file w/block .
;		10-Mar-92, added swpos keyword, and device "dev" to 
;			sw_tape function calls.
;		3-Apr-92, added path options to restores
;		14-apr-92, updated online documentation
;		23-Apr-92, corrected check of requested file against
;			the tape contents.
;		27-Apr-92, minor bug fix to return when no files are
;			requested.
;		26-May-92, added mount switch to mt /open command for
;			vms machines
;		9-Feb-93, modified error reporting to be more specific
;			of the type of IO error encountered and added
;			a "err_recover.dat" file for automated recovery.
;		26-Apr-93, added put_ydb switch to restore weeklys to
;			database directories.
;                9-Jul-94 (SLF) - added files=files(sort(files)) 
;		19-Apr-95 (GAL) - added script and alpha switches, and
;					notape
;		 8-May-95 (GAL) - added unload switch and made no-unload
;			the default, and a minor change to the way
;			scripts are made.
;
;-
;	-------------------------------------------------------------

dont_use_this,'rdtap','rdtap2'

	Print,'     ****** rdtap2 ver. 3.1, 8-May-95 *********'
	Print,'READS and EXTRACTS DATA FROM YOHKOH DATA ARCHIVE TAPES'
	Print,' '

;	Error log stuff:
	errflag = 0			;no IO errors yet!!!

;	stop,'**** ck stuff at Rdtape:
	revpar = 0			;init flag for when files=dev

	CASE N_PARAMS() OF 
	  0:  BEGIN
		IF ( KeyWord_set(prefix) or KeyWord_set(fileID) or $
		     KeyWord_set(obs) or KeyWord_set(dir) or $
		     KeyWord_set(software) ) THEN BEGIN
		  dev = Def_tapd()	;get default tape drive		
		ENDIF ELSE BEGIN
		  RdTapeDoc 		;Display help information
		  return
		ENDELSE
	      END
	  1:  BEGIN
		IF (N_ELEMENTS(dev) ne 1) THEN BEGIN	;dev=file
		  revpar = 1			;set flag for dev =files
		  file = dev			;passed files only
	          files = file			;save input list from $#@%^&!!!
		  dev = Def_tapd()	;get default tape drive		
		ENDIF			;otherwise all is normal
	      END
	  2:  BEGIN 		;ok-- check device parameter
		IF (N_ELEMENTS(dev) eq 0) THEN BEGIN	;undefined device
		  dev = Def_tapd()	;get default tape drive		
		  devn= 0				;device number
		ENDIF
	        files = file		;save input list from $#@%^&!!!
	      END		
	  ELSE: BEGIN
		  Print, 'incorrect number of non-keywords parameters!!!'
		  return
		END
	ENDCASE

	notape = Keyword_set(notape)		;binary variable.
	unload = Keyword_set(unload)		;binary variable.
	edit   = Keyword_set(edit)		;binary
	script = Keyword_set(script)		;binary var.
	alpha  = Keyword_set(alpha)		;binary var.

	IF ((!version.OS eq 'vms') AND Keyword_set(script)) THEN BEGIN
	  print, 'script option is not supported under "vms" OS'
	  script = 0 & alpha = 0
	ENDIF
	IF (!version.arch eq 'alpha') THEN script = 1	;default mode for alphas

	IF (n_elements(noexec) eq 0) THEN BEGIN
	  exec = 1			; auto start script
	ENDIF ELSE exec = 0		; let user start script
	IF (alpha) THEN script = 1	; also set script

;       slf, 9-jul-1994, added sort of files 
        if n_elements(files) gt 1 then files=files(sort(files))
		
	cpioPos = 0			;tape position in cpio archive

	IF (SCRIPT) THEN BEGIN
	  IF (!version.OS ne 'IRIX') THEN BEGIN   ;deal with diff machines
            sw = '-f '                    ;switch for sun & ultrix
          ENDIF ELSE BEGIN
            sw = '-t '                    ;switch for SGI
          ENDELSE
	  IF ((!version.arch eq 'alpha') or alpha ) THEN BEGIN	  ;deal with diff machines
	    isalpha = 1
	    cpiostr = 'cpio -iBc -I'+dev+' '    ;base cpio command
	    cpiostr2=''				;null dev string
	  ENDIF ELSE BEGIN
	    isalpha = 0
	    cpiostr = 'cpio -iBc '	       	;base cpio command
	    cpiostr2= ' < '+dev			;dev string
	  ENDELSE
	  cd, current=defdir			;default dir.
	  scr_fn = defdir+'/tape_dump'		;script filename
	  tap_dir= 'tapeDir'			;binary tape dir. file
	  print, 'Using "script" mode: tape commands written to file'
;	  openw, slun, scr_fn, /get_lun ; moved to end 
if (keyword_set(debug)) then begin
	print, 'Debug mode: using the previously copied tap_dir file'
	  rewstr = 'mt '+sw+dev+' rewind'	;rewind string
	  fsfstr = 'mt '+sw+dev+' fsf '         ;skip file forward string
endif else begin
	  rewstr = 'mt '+sw+dev+' rewind'	;rewind string
	  astr = str2arr(rewstr, del=' ')
;	  print,'spawning: ', rewstr
	  spawn, astr, /noshell

	  fsfstr = 'mt '+sw+dev+' fsf '         ;skip file forward string
	  astr = str2arr(fsfstr+'1', del=' ')
;	  print,'spawning: ', fsfstr+'1'
	  spawn, astr, /noshell

	  str = 'cp '+dev+' '+tap_dir	;Dump the binary tape dir. file
	  astr = str2arr(str, del=' ')
;	  print,'spawning: ', str
	  spawn, astr, /noshell
endelse
	
	  Rd_TapDir, tap_dir, hdr, drecs;read tape directory 
	  Print, '	'
	  PRINT, 'Restoring from Data Archive Tape: ', STRING(hdr.tapename)
; NOTE: remove tap_dir file when done reading....  leave for debug

;	  printf, slun, rewstr
	  sarr = rewstr			; initialize output script
;	  printf, slun, fsfstr+'2'
 	  sarr = [sarr, fsfstr+'2']
	ENDIF ELSE BEGIN
	  IF (noTape) THEN BEGIN	;force tape-dir file read fr/disk
	    ff = file_list('$DIR_GEN_XBD', 'xbd*.*', /cd)
	    IF (ff(0) eq '') THEN BEGIN
	      Print, '* No Tape Directory Files FOUND on disk!'
	      ans = 'Y' & input, 'Read tape directory from tape?', ans,ans
	      ans = STRUPCASE(ans)
	      IF (ans eq 'Y') THEN BEGIN
		mess = 'Ready to read tape loaded into drive #'+strtrim(dev,2)+': '
		ans = 'Y' & input, mess, ans, ans
		ans = STRUPCASE(ans)
		IF (ans eq 'Y') THEN BEGIN
		  mt, dev, unit, /open,/mount ;Open tape unit & get unit number
                                        ;added mount for vms only
            	  mt, unit, /rewind           ;rewind tape
            	  mt, unit, skpf=1            ;skip ascii tape dir. file
            	  Rd_TapDir, unit, hdr, drecs ;read tape directory
	          PRINT, '     '
                  PRINT, 'Restoring from Data Archive Tape: ', STRING(hdr.tapename)
            	  mt, unit, skpf=1            ;skip forward to 1st cpio archive
	  	ENDIF ELSE BEGIN
		  Print, 'Exit rdtap without reading tape'
		  return
		ENDELSE
	      ENDIF ELSE BEGIN
		Print, 'Exit rdtap without reading tape'
		return
	      ENDELSE
	    ENDIF ELSE BEGIN		;finally read tape-dir from disk
	      ss = wmenu_sel(ff,/one)	;let user pick the correct one
	      tapdir = ff(ss(0))
	      Rd_TapDir, tapdir, hdr, drecs
	      mt, dev, unit, /open,/mount ;Open tape unit & get unit number
              mt, unit, /rewind           ;rewind tape
              mt, unit, skpf=2            ;skip ascii tape dir. file
	    ENDELSE	      
	  ENDIF ELSE BEGIN		;Default Tape-Dir read
	    mt, dev, unit, /open,/mount	;Open tape unit & get unit number
					;added mount for vms only
	    mt, unit, /rewind		;rewind tape 

	    mt, unit, skpf=1		;skip ascii tape dir. file

	    Rd_TapDir, unit, hdr, drecs	;read tape directory
	    PRINT, '     '
	    PRINT, 'Restoring from Data Archive Tape: ', STRING(hdr.tapename)

	    mt, unit, skpf=1		;skip forward to 1st cpio archive file
	  ENDELSE
	ENDELSE

	IF (KEYWORD_SET(WEEK)) THEN BEGIN	;cause MONS HORSED the OBS file
	Preck = string(hdr.wkprelst)
	IF (Preck ne '') THEN BEGIN	;check for no files	
          wkpre = STR2ARR(string(hdr.wkprelst),delimit=',')      ;get wk-prefix
	  wkpre = wkpre( where(wkpre ne '') )	;delete nulls
          exten  = hdr.wkextlst(where(hdr.wkextlst))              ;get extensions
          sexten = STRING(exten, format='(i2.2)')         ;string-type extensions
	  IF (KEYWORD_SET(week)) THEN BEGIN
	    IF (STRLOWCASE(week(0)) eq 'all') THEN BEGIN
	      wks = wkpre			;restore all
	      sextens = sexten	
	    ENDIF ELSE BEGIN
	      wkidx = intarr(n_elements(week))		;extract only found files
	      for i=0,n_elements(week)-1 do wkidx(i) = where(wkpre eq week(i))
	      wkidx = wkidx(where(wkidx ne -1))		;get rid of -1s
	      ord   = SORT(wkpre(wkidx))		;order for opt. extraction
	      wks   = wkpre(wkidx(ord))
	      sextens = sexten(wkidx(ord))
	    ENDELSE
	    IF (script) THEN BEGIN
              cpioalst = '"'+wks+'*"'
	      cpiolst = arr2str(cpioalst, del=' ')
	      cd, current=olddir
	      CASE 1 OF
	        (Keyword_set(put_ydb)): BEGIN
		  print,'Warning: All weekly files will be dumped to the'
		  print,'default directory.  Then moved to their specific'
		  print,'directories.  You may need as much as 200Mb of '
		  print,'free disk space to do this.'
	          IF (Keyword_set(wpath)) THEN BEGIN
;		    printf, slun, 'cd '+wpath
		    sarr = [sarr, 'cd '+wpath]
		    IF (isalpha) THEN BEGIN
;	              printf, slun, cpiostr+cpiolst
		      sarr = [sarr, cpiostr+cpiolst]
		    ENDIF ELSE sarr = [sarr, cpiostr+cpiolst+cpiostr2]
;				printf, slun, cpiostr+cpiolst+cpiostr2

	          ENDIF ELSE BEGIN
		    IF (isalpha) THEN BEGIN
;	              printf, slun, cpiostr+cpiolst	; default dir dump
		      sarr = [sarr, cpiostr+cpiolst]
		    ENDIF ELSE sarr = [sarr, cpiostr+cpiolst+cpiostr2]
;				printf, slun, cpiostr+cpiolst+cpiostr2
	          ENDELSE

		  move_weekly, sarr, wks	; move weekly files

		  IF (Keyword_set(wpath)) THEN sarr = [sarr, 'cd '+olddir]
;						printf, slun, 'cd '+olddir
	        END
	        (Keyword_set(wpath)): BEGIN
;		  printf, slun, 'cd '+wpath
		  sarr = [sarr, 'cd '+wpath]
		  IF (isalpha) THEN BEGIN
;	            printf, slun, cpiostr+cpiolst
		    sarr = [sarr, cpiostr+cpiolst]
		  ENDIF ELSE sarr = [sarr, cpiostr+cpiolst+cpiostr2]
;				printf, slun, cpiostr+cpiolst+cpiostr2
;		  printf, slun, 'cd '+olddir
		  sarr = [sarr, 'cd '+olddir]
	        END
		ELSE: Begin
		  IF (isalpha) THEN BEGIN
;	            printf, slun, cpiostr+cpiolst	; default dir
		    sarr = [sarr, cpiostr+cpiolst]	; default dir
		  ENDIF ELSE sarr = [sarr, cpiostr+cpiolst+cpiostr2]
;				printf, slun, cpiostr+cpiolst+cpiostr2
	        ENDELSE
	      ENDCASE	
;	      print, 'dumping weekly files: ', cpiostr+cpiolst
;	      printf, slun, fsfstr+'1'
	      sarr = [sarr, fsfstr+'1']
	    ENDIF ELSE BEGIN
	      IF (n_elements(put_ydb) ne 0) THEN BEGIN	;weeklys to $DIR_GEN*
	        a = wrtwkdat( unit, wks, hdr, drecs(0), /put_ydb)	
	      ENDIF ELSE BEGIN			;write to default or wpath
	        a = wrtwkdat( unit, wks, hdr, drecs(0), wpath=wpath)	;create wk files
	      ENDELSE
	      mt, unit, skpf=1		;skip to begin of next file
	      wkfns = wks+STRING(drecs(0).fileID)+'.'+sextens
	      PRINT, 'Creating Weekly files: ', wkfns
	      IF (Keyword_set(wpath)) THEN Print, 'On Directory: ', wpath
	    ENDELSE
	    cpioPos = cpioPos + 1	;tape position in cpio archive
	  ENDIF
	ENDIF ELSE BEGIN		;No weekly files on tape
	  IF (KEYWORD_SET(week)) THEN BEGIN	
	    Print, 'Sorry No Weekly Files were placed on this Tape!'
	  ENDIF
	ENDELSE
	ENDIF 				;end horsing around

	IF (KEYWORD_SET(dir)) THEN BEGIN
	  dirInfo = SIZE(dir)			;check for explicit path
	  IF (dirInfo(1) eq 7) THEN BEGIN	;string 
	    pathdefined = 1			;set flag
	    IF (STRUPCASE(dir) ne 'LOG') THEN BEGIN	;direct both files 
	      IF (!version.OS eq 'vms') THEN BEGIN
	        adirpath = dir
	        bdirpath = dir		
	      ENDIF ELSE BEGIN		;non-vms
		IF (STRMID(dir, strlen(dir)-1, 1) ne '/') THEN dir = dir +'/'
	        adirpath = dir
	        bdirpath = dir
	      ENDELSE
	    ENDIF ELSE BEGIN
	      IF (!version.OS eq 'vms') THEN BEGIN
	        adirpath = 'dir:'	;not clear -assume it is a logical
	        bdirpath = 'dir:'		
	      ENDIF ELSE BEGIN		;non-vms sys
	        adirpath = '$DIR_GEN_XAD/'
	        bdirpath = '$DIR_GEN_XBD/'
	      ENDELSE
	    ENDELSE
	  ENDIF ELSE BEGIN		;no path defined
	    pathdefined = 0			;set flag
	    adirpath = ''
	    bdirpath = ''
	  ENDELSE
	  tDirfn = STRING(hdr.tapeName)         ;default name is tapename
          pos = STRPOS(tDirfn,'-',0)            ;remove reformater ver#
          tDirfn = STRMID(tDirfn,0,pos) 

	  WrtTapDir, hdr, drecs, 'xad'+tDirfn, adirpath	;create ascii dir file
	  PRINT, 'Creating ASCII Tape Directory file: ', 'xad'+tDirfn
          IF (pathdefined) THEN Print, 'On Directory: ', adirpath
	  OPENW, tdunit, bdirpath+'xbd'+tDirfn, /get_lun, /block
	  PRINT, 'Creating Binary Tape Directory file: ', 'xbd'+tDirfn
          IF (pathdefined) THEN Print, 'On Directory: ', bdirpath
	  writeU, tdunit, hdr, drecs
	  Free_lun, tdunit
	ENDIF

	CASE 1 OF 			;create filenames
	  ( KEYWORD_SET(prefix) AND KEYWORD_SET(fileID) ): BEGIN
;		  Print, '**** both prefix and fileID keywords set:'
		  sav_prefix = prefix		;save input
		  prefix = PrefixCk(prefix)	;ck & ensure standard
		  n_prefix = N_ELEMENTS(prefix)
		  IF (n_prefix gt 1) THEN  BEGIN
		    porder = SORT(prefix)	;order the list
		  ENDIF ELSE BEGIN
		    porder = [0]		;list of 1
		  ENDELSE
		  FOR i=0, n_prefix-1 DO BEGIN
		    fn = prefix(porder(i)) + fileID
		    IF (i eq 0) THEN BEGIN
		      files = fn	;save
		    ENDIF ELSE BEGIN
		      files = [files, fn]	;append to list
		    ENDELSE
		  ENDFOR
		  prefix = sav_prefix		;restore input prefix
;		  stop,'**** ck new files list:
	      END
	  ( KEYWORD_SET(prefix) ): BEGIN
		sav_prefix = prefix		;save input
		prefix = PrefixCk(prefix)	;ck & ensure standard
		npf = N_ELEMENTS(prefix)	;number of file prefixes
		nid = N_ELEMENTS(drecs)-1	;number of file ids-obs id
		files = STRARR(npf*nid)
		FOR i=0, npf-1 DO BEGIN
		  files(i*nid) = prefix(i) + STRING(drecs(1:nid).fileid)
		ENDFOR
		prefix = sav_prefix
;	print, 'files from prefix input: ', files
;	stop,'**** ck new files list:
	      END	
	  ( KEYWORD_SET(fileID) ): BEGIN
		prefix = PrefixCk(/noobs)	;get the standard list w/o obs
		npf = N_ELEMENTS(prefix)	
		nid = N_ELEMENTS(fileID)	;number of file ids
		files = STRARR(npf*nid)
		FOR i=0, npf-1 DO BEGIN
		  files(i*nid) = prefix(i) + fileid
		ENDFOR
;		stop,'**** ck new files list:
	      END	
	  ELSE: BEGIN & end		;ok
	ENDCASE
	
	IF (KEYWORD_SET(range)) THEN BEGIN	;expand input into range
	  files = getrng(files, drecs)			
	ENDIF 

	IF (script and Keyword_set(dpath)) THEN BEGIN
	  cd, current=olddir
;	  printf, slun, 'cd '+dpath
	  sarr = [sarr, 'cd '+dpath]
	ENDIF	
	IF (N_elements(files) ne 0) THEN BEGIN	;skip if no files are req.
	good = ck(files, drecs)		;check input file list against
					;tape contents
	IF (good(0) ne -1) THEN BEGIN	;extract data files
;	  stop, '**** ck the good index list to files:'
	  files = files(good)		;do the GOOD list
;	  Given request compute number of files to skip:	
	  IF ( N_ELEMENTS(files) ne 0) THEN BEGIN	;ck valid list of files
	    nfn = N_ELEMENTS(files)
	    len = STRLEN(files(0))	;assume all fileNames are same len
	    ids = STRMID(files,3,len-3)	;get file ids
	    idlist = ids(uniq(ids,sort(ids)))	;unique list of fileIDs
	    nids = N_ELEMENTS(idlist)		;number of unique fileIds
 	    FOR i=0, nids-1 DO BEGIN	;loop thru fileids  
	      idx = where( STRMID(files,3,len-3) eq idlist(i))	
	      list = files(idx)		;list of files with the same ID
	      j = where( string(drecs.fileid) eq idlist(i))	;cpio index
	      j = j(0)				;scalar cpio index
 	      IF (script) THEN BEGIN
;		print, fsfstr+string(j-cpioPos)
;		printf, slun, fsfstr+strtrim((j-cpioPos),2) ;skip to cpio archive
		sarr = [sarr, fsfstr+strtrim((j-cpioPos),2)];skip to cpio arch
		prefixlist = STRMID(list,0,3)
		prefixlist = '"'+prefixlist+'*"'
		fixlist = arr2str(prefixlist, del=' ')
		IF (isalpha) THEN BEGIN
;	          printf, slun, cpiostr+fixlist	; dump the cpio files
		  sarr = [sarr, cpiostr+fixlist]; dump the cpio files
		ENDIF ELSE sarr = [sarr, cpiostr+fixlist+cpiostr2]
;				printf, slun, cpiostr+fixlist+cpiostr2
;		printf, slun, fsfstr+'1'	; skip to next archive
		sarr = [sarr, fsfstr+'1']	; skip to next archive
	      ENDIF ELSE BEGIN
	        mt, unit, skpf=(j-cpioPos)	;skip to cpio archive
	        Print, 'Creating files: ', list
                IF (Keyword_set(dpath)) THEN Print, 'On Directory: ', dpath
	        a = wrtdat3( unit, list, drecs(j), dpath=dpath)  	
	        ainfo = SIZE(a)
	        IF (ainfo(ainfo(0)+1) eq 7) THEN BEGIN	;write errlog
	          IF (errflag eq 0) THEN BEGIN		;no err log yet
		    openw, errunit, 'rdtape.log',/get_lun	;open error mess log
		    openw, errunit2, 'err_recover.dat',/get_lun	;recover file
	          ENDIF
	          Print, a(0), format='(a)'	;print return error message
	          Print, a(1:*)			;display problem files only
	          Print, 'Attempting to Continue...'
	          printf,errunit,a				
		  printf,errunit2,transpose(a(1:*))	;write simple list.
	          errflag = 1
	        ENDIF
	        mt, unit, skpf=1			;move to begin of next file
              ENDELSE
	      cpioPos = cpioPos + 1 + (j-cpioPos)
	    ENDFOR
	  ENDIF				;valid file list ck
	ENDIF ELSE BEGIN		;any files to ck and get
	  Print, 'No Requested Files found on this Tape!!!'
	ENDELSE
	ENDIF				;files are undefined or 0

	IF (script and Keyword_set(dpath)) THEN BEGIN
;	  printf, slun, 'cd '+olddir
	  sarr = [sarr, 'cd '+olddir]
	ENDIF	
	IF (not Keyword_SET(swpos)) THEN swpos = 0
	IF (not Keyword_SET(software)) THEN software = 0
	IF (script and swpos) THEN Begin
	  print, 'The "swpos" switch is not supported in this mode'
	  swpos = 0
	ENDIF
	IF (script and software) THEN Begin
	  print, 'The "software" switch is not supported in this mode'
	  software = 0
	ENDIF
;	Compute Tape Position for decompress Software Tar files:
	IF (KEYWORD_SET(swpos)) THEN BEGIN
	  mt, unit, skpf=(hdr.noLogRec - cpioPos)	;skip forward to tar
	  mt, unit,/close, /dismount
	  return
	ENDIF
	IF (KEYWORD_SET(software)) THEN BEGIN
	  mt, unit, skpf=(hdr.noLogRec - cpioPos)	;skip forward to tar
	  mt, unit,/close, /dismount
	  sinfo = SIZE(software)
	  stype = sinfo(sinfo(0)+1)
	  IF (stype eq 7) THEN BEGIN	;software is string-type
	    cmds = sw_tape(dev,/restore,/all, direct=software)
	  ENDIF ELSE BEGIN		;software is non-string (binary)
	    cmds = sw_tape(dev,/restore,/all)	;restore to default dir.
	  ENDELSE
;	  Print, cmds, format='(a)'
;	  stop ,'**** stop before tar cmds:'
	  cd, STRMID(cmds(0), 3, STRLEN(cmds(0))-3)	;change dir
	  spawn, cmds(1)				;issue restore
	ENDIF
	
	IF (errflag) THEN BEGIN		;valid err log
	  Free_lun, errunit		;error message file
	  Free_lun, errunit2		;error recovery file
	ENDIF 
		
	IF (revpar) THEN BEGIN		;keep file list from being erased
	  dev = file			;return filename list
	  revpar = 0			;reset flag
	ENDIF
	IF (script) THEN BEGIN
	  IF (unload) THEN BEGIN
  	    mtcmd,dev,/offline,cmds=offlstr,/nospawn
	  ENDIF ELSE BEGIN		;Default no-unload
  	    mtcmd,dev,/rewind,cmds=offlstr,/nospawn
	  ENDELSE
;	  printf, slun, offlstr
	  sarr = [sarr, offlstr]

          openw, slun, scr_fn, /get_lun
	  prstr, sarr, slun		; write string-array to file
;	  stop, 'check str-arr: sarr:'
	  free_lun, slun
	  print, 'Commands Saved to "script" file: ', scr_fn

	  vicmd = 'vi '+ scr_fn
	  vicmd = str2arr(vicmd, del=' ')
	  IF (edit) THEN spawn, vicmd, /noshell

	  IF (exec) THEN BEGIN
	    modcmd = 'chmod 777 '+scr_fn
	    modcmd = str2arr(modcmd, del=' ')
	    spawn, modcmd, /noshell

	    execmd = 'nohup '+scr_fn+' &'
;	    execmd = str2arr(execmd, del=' ')
	    spawn, execmd, out
	  ENDIF ELSE BEGIN
	    modcmd = 'chmod 777 '+scr_fn
	    modcmd = str2arr(modcmd, del=' ')
	    spawn, modcmd, /noshell
	  ENDELSE
	  
	ENDIF ELSE BEGIN
	  mt, unit, /close ,/dismount	;release tape dev
	ENDELSE
	END
