pro mtcmd, dev, rewind=rewind, fsf=fsf, bsf=bsf, fsr=fsr, bsr=bsr, $
	offline=offline, cmds=cmds, nospawn=nospawn, background=background
;
;+
;   Name: mtcmd
;
;   Purpose: issue OS dependent tape positioning
;
;   Calling Sequence:
;      mtcmd [,drive , /rewind, /offline, fsf=NN, bsf=NN , $
;	      fsr=NN, bsr=NN, /nospawn]
;
;   Calling Examples:
;      mtcmd,/rewind			; rewind drive 0
;      mtcmd,1,/rewind,fsf=5		; rewind, then skip forward 6, drive 1
;
;   Input Parameters:
;      dev - drive number or drive name (default is drive 0, via def_tapd()
;
;   Keyword Parameters:
;      rewind  - if set, rewind is issued
;      offline - if set, offline is issued
;      fsf     - if set, forward skip specified number files 
;      bsf     - if set, backward skip specified number of files
;      fsr     - if set, forward skip specified number records
;      bsr     - if set, backward skip specified number of records
;      nospawn - if set, dont spawn commands
;      background - if set, command is spawned in background
;      cmds    - (output) returns the mt commands as string/string array
;
;   History:
;      30-Jun-1994 (SLF) - having problems with mt.pro and mtcom.pro
;       8-Aug-1994 (SLF) - unload instead of offline for SGI
;      17-Apr-1997 (GLS) - added fsr and bsr commands
;
;   Restrictions:
;      This routine only postions drive (rewind, forward and back skips)
;      UNIX only for today (vms idl routines available=skipf, etc)
;
;-
; ----------- tape name OR drive number are allowed for dev ---------------
; ----------- (tape name system dependence done via def_tap) --------------
if data_chk(dev,/string) then begin
   if (strlen(strtrim(dev,2)))(0) gt 1 then tapd=dev else tapd=def_tapd(dev)
endif else tapd=def_tapd(dev)
; --------------------------------------------------------------------------

; --------------------------------------------------------------------------
if strupcase(!version.os eq 'VMS' ) then begin
   message,/info,"UNIX only for now, use mt.pro
endif else begin
;  ---------------------- system dependent mt qualifiers ------------------
   mtoff=[' offline',' unload']
   mtdevq=[' -f ',' -t ']
   mtost=['IRIX']
   mtcmd='mt' + mtdevq(is_member(!version.os,mtost,/ignore_case)) + tapd
   mtosoff=mtoff(is_member(!version.os,mtost,/ignore_case))
;  -------------------------------------------------------------------------
   cmds=''
;  ---------------- determine requested action(s) ---------------------------   
   if keyword_set(rewind) then  cmds=[cmds,mtcmd + ' rewind']
   if keyword_set(fsf) then     cmds=[cmds,mtcmd + ' fsf ' + strtrim(fsf,2)]
   if keyword_set(bsf) then     cmds=[cmds,mtcmd + ' bsf ' + strtrim(bsf,2)]
   if keyword_set(fsr) then     cmds=[cmds,mtcmd + ' fsr ' + strtrim(fsr,2)]
   if keyword_set(bsr) then     cmds=[cmds,mtcmd + ' bsr ' + strtrim(bsr,2)]
   if keyword_set(offline) then cmds=[cmds,mtcmd + mtosoff]
;  ---------------------------------------------------------------------------
   gcmds=where(cmds ne '',gcnt)
   if gcnt gt 0 then begin
;     
;     add background symbol if requested
      bgs=['',' &']
      cmds=cmds + bgs(keyword_set(background))
      cmds=cmds(gcmds)
      if not keyword_set(nospawn) then begin
;           ---------- Must close OPEN file units if name = tapd --------
	    fs=fstats(name=tapd)			; check all open units
	    if data_chk(fs,/struct) then begin		; open unit found
	       message,/info,"Closing OPEN tape drive" + tapd + " on unit# " $
		  + strcompress(arr2str(string(fs.unit)),/remove)
                for i=0,n_elements(fs)-1 do free_lun,fs.unit
	    endif
;           --------------------------------------------------------------
;           -------------------- now spawn commands ----------------------
            for i=0,n_elements(cmds)-1 do begin
               message,/info, cmds(i)
               spawn,cmds(i),out
            endfor         
;           ---------------------------------------------------------------
        endif
   endif else begin
     message,/info,"No actions specified...
     message,/info,"(Actions are: /rewind, fsf=N, bsf=N, /offline)
   endelse
endelse

return
end
