PRO mt, p1, p2, open=open, close=close, rewind=rewind, 		$
		skpf=skpf, skpr = skpr, rd = rd, mount=mount, dismount=dismount
;	-----------------------------------------------------------------
;+						6-Mar-94
;  NAME:
;    mt
;  PURPOSE:
;    Handle functional control of tape "dev" for both unix and vms.
;  CALLING SEQUENCE:
;    mt,'dev/nrmt0h',unit,/open		; For unix 
;    mt,'mub0',unit,/open,/mount	; For VMS (DCL mount the tape)
;					; Will also do a 
;					;      setlog, 'mub0','mt'+fix(unit)
;    mt,'mub0',unit,/open		; For VMS (Don't mount the tape)
;    mt,unit,/rewind			; rewind
;    mt,unit,rd=buff			; Read a tape record into buff
;    mt,unit,skpf=1			; Skip one record forward
;    mt,unit,skpr=1			; Skip one record backwards (reverse)
;    mt,unit,/close			; unix (no action for vms)
;    mt,unit,/close,/dismount		; vms (DCL dismount/nounload the tape)
;    Note: the /mount and /dismount will have no effect under unix.
;  INPUT: 
;	p1	device (e.g. '/dev/nrmt0h') or
;		unit number (e.g., 0 or 1)
;		NOTE:   DEVICE "dev" is assumed to be defined and string.
;  OUTPUT:
;	p2	unit number (only if /open switch is supplied).
;
;  OPTIONAL KEWORDS:
;	/open	To open either unix or vms
;	/mount	(VMS only)  -- With the /open switch, will also spawn
;				a DCL mount/foreign command.
;	/rewind	Rewinds the tape
;	skpf=n	Skip forward n files (e.g., skpf=1 will skip one file forward)
;	skpr=n	Skip backwards n files.
;	rd=rd	Read a tape record into rd
;	/close  Close the tape unit (unix)
;	/dismount (VMS) If specified with /close, will spawn a DCL 
;			dismount/nounload command. 
;			Note:  Even though the drive is dismounted, since
;			IDL does not deallocate the drive, no other tape
;			can be mounted until the current IDL process has
;			been exited.
; MODIFICATION HISTORY:
;   6-nov-91, G. A. Linford, Written
;  28-mar-92, J. R. Lemen, Added documentation, /mount and /dismount for vms.
;  16-Apr-92, gal, upgraded vms /open /mount logic work for bcs group at isas.
;  18-Jun-92, gal, added I/O trap for problems with openr command on
;		tape device.
;  23-Jun-92, gal, still having problems with j-side devices...added mt rewind
;  19-Oct-93, gal, add a trap on i/o readu errors specifically for DATs???
;  30-Nov-93, gal, minor change to vms mount and info mount status.
;  14-Feb-94, gal, minor change to error trap logic.
;   5-Mar-94, gal, increased wait on retry for problems with ISASS0 running
;		Ultrix V4.3A.
;   6-Mar-94, gal, Yet another patch has been made for hardware problems.
;		This one was for: OPENR: Error opening file: /dev/nrmt*h
;		Interrupted system call.
;		I added an io_error tape after a spawned rewind-status
;		cmd to the system for another openr try.  Trap this error
;		wait 2 seconds and retry the open with sucess!
;   7-Jul-94, slf, suppress annoying 'error' messages, during normal operations
;		   (I think the definition of normal has changed) 
;
;- -------------------------------------------------------------------------

	common glomt, vmsunit			;make unit a global var.

	IF (N_ELEMENTS(vmsunit) eq 0) THEN BEGIN	;unit array is undefined
	  vmsunit = INTARR(10)
	  vmsunit(*) = -1
	ENDIF

	IF (!version.OS ne 'vms') THEN BEGIN	;non-vms mode
	  IF (KEYWORD_SET(open)) THEN BEGIN
	    ON_IOERROR, open_error		;set error trap
	    OPENR, p2, p1, /get_lun		;open dev & get unit
	    GOTO, norm_open
open_error: ON_IOERROR, NULL			;cancel error trap
;	    Print,'Device Problems--Trying AGAIN to OPEN device ', p1
	    wait, 2				;wait a second and 
            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
	    mtcommand = 'mt '+ sw + p1 + ' rewind'	;rewind drive cmd
;	    mtcommand = 'mt '+ sw + p1 + ' status'	;rewind drive cmd
	print, 'spawned cmd: ', mtcommand
	    SPAWN, mtcommand			;do the command
	    ON_IOERROR, open2_error		;set 2nd trap
;	    wait, 4				;maybe tape is really rewinding
	    OPENR, p2, p1, /get_lun		;try again
	    GOTO, norm_open
open2_error:ON_IOERROR, NULL			;cancel 2nd error trap
;	    Print,'Device Problems--Trying once Again to re-open device', p1
	    wait, 2				;wait for error clear?
	    openr, p2, p1, /get_lun		;re-connect to device
norm_open:  ON_IOERROR, NULL			;cancel error trap
	  ENDIF
	  IF (KEYWORD_SET(close)) THEN BEGIN
	    Free_lun, p1
	  ENDIF

	  IF (KEYWORD_SET(rewind)) THEN BEGIN
	    mtCom, p1, /rewind				;rewind unit=p1
	  ENDIF

	  IF (KEYWORD_SET(skpf)) THEN BEGIN
	    mtCom, p1, skpf=skpf
	  ENDIF

	  IF (KEYWORD_SET(skpr)) THEN BEGIN
	    mtCom, p1, skpr=skpr
	  ENDIF

	  IF (KEYWORD_SET(rd)) THEN BEGIN
	    ON_IOERROR, readu_error		;set error trap
	    READU, p1, rd
	    GOTO, norm_read
readu_error:ON_IOERROR, NULL			;cancel error trap
;	    estr = !err_string			;build error message
	    Message, !err_string, /noname, /traceback, /continue
	    rderrflg = 1			;set rd error flag
    ;	print, 'MT: READU i/o error: ', estr	;temp. fix until add common blk
    ;	stop, 'try re-READU, p1,rd & .cont
	    wait, 01			;why wait??? cause its hardware!
	    ON_IOERROR, readu_error		;set error trap
	    READU, p1, rd			;re-read tape record???
norm_read:
	    ;	continue
	  ENDIF

	ENDIF ELSE BEGIN			;vms mode

	  IF (KEYWORD_SET(open)) THEN BEGIN
;	    p2 = fix(strmid(p1,strlen(p1)-1,1))	;get unit # fr physical device name
	    get = where(vmsunit eq -1, count)
	    IF (count eq 0) THEN BEGIN		;ran out of spare units
	      Print, 'All units currently allocated please free up units'
	      stop
	    ENDIF
	    p2 = get(0)		;get the 1st available, set to index val
	    vmsunit(get(0)) = 0			;reserve unit

	    if keyword_set(mount) then begin
	      setlog,'mt'+strtrim(p2,2),p1	; Set up a logical mt#
	      mntcmd ='mount/foreign/nounload mt'+strtrim(p2,2)	; mount/foreign
	      message,/info,"Mounting via: " + mntcmd
              spawn, mntcmd			; do the mount
	    endif
	  ENDIF

	  IF (KEYWORD_SET(rewind)) THEN BEGIN
	    REWIND, p1				;rewind unit=p1
	  ENDIF

	  IF (KEYWORD_SET(skpf)) THEN BEGIN
	    SKIPF,  p1, skpf			;skip forward n files
	  ENDIF

	  IF (KEYWORD_SET(skpr)) THEN BEGIN
	    SKIPF, p1, skpr, 1			;skip forward n records
	  ENDIF

	  IF (KEYWORD_SET(rd)) THEN BEGIN
	    TAPRD, rd, p1			;vms tape read
	  ENDIF

	  IF (KEYWORD_SET(close)) and keyword_set(dismount) THEN BEGIN
	    dismntcmd = 'dismount/nounload mt'+strtrim(p1,2)	; vms dismount
	    message,/info, "Dismounting via: " + dismntcmd
	    spawn, dismntcmd			; do dismount
	    vmsunit(p1) = -1			;free unit	    
	  ENDIF

	ENDELSE

	RETURN
	END
