	PRO mo_tap_dump, mo_node, mt_dev, prefix, wks, mag_disk, $
		mt_status, wks_status, qdebug = qdebug	
;	---------------------------------------------------------
;+						21-May-93
;	NAME: mo_tap_dump
;
;	PURPOSE: Dump MO archive file from 8mm Yohkoh archive tape.
;
;	sorry no time for other stuff
;	History; written 21-May-93
;		26-May-93, changed Tape dump dir. to type "m" from
;			type "t". 
;
;-
;	--------------------------------------------------------

	account = 'mocreate'		;account defaults
	acc_sw  = '-l '+account+' -n'	;rsh account cmd

;	Set-up vars. according to the number of input tapes:
	tap_wk = strarr(n_elements(mt_dev))
	mo_wks = strmid(wks,0,5)	;trim the a's, b's and c's
	do_mt  = intarr(n_elements(mt_dev)) & do_mt(*) = 0 ;do none
        tap_wk = strarr(n_elements(mt_dev))

	FOR i = 0, n_elements(mt_dev) -1 DO BEGIN	;do for ea
	  MT, def_tapd(mt_dev(i)), unit, /open
	  MT, unit, /rewind
	  MT, unit, skpf=1
	  
	  rd_tapdir, unit, hdr, drecs	;get tape directory info
          MT, unit, /close		;close the unit

	  tap_wk(i) = strmid(hdr.tapename,0,5)	;name archive week
	
	  wk_ck = where( mo_wks eq tap_wk(i))  	;do we need this one
	  if wk_ck(0) ne -1 then do_mt(i) = 1

	  IF do_mt(i) THEN BEGIN		;valid archive week
	    tap_wk_stat = wk_online( tap_wk(i))	;ck for on-line
	    if tap_wk_stat eq '' then begin	;NOT on-line -- dump
	      dir1 = concat_dir(mag_disk(i), tap_wk(i)+'m')
	      ck_dir1 = strmid(dir1,0,strlen(dir1)-1)	;remove 'm' for ck
;	      exist = file_exist( ck_dir1, /direct) ;db check for dir1
	      dir_exist = wk_online(ck_dir1)	;double ck for generic dir1
	      if (dir_exist(0) eq '') then begin	;not found
	        mkd_cmd = 'mkdir '+ dir1
		chm_cmd = 'chmod 777 '+ dir1
;		rsh_cmd = 'rsh '+mo_node+' '+acc_sw
		cmds = strarr(2)              
		cmds(0) = mkd_cmd
              	cmds(1) = chm_cmd
              	For j=0, 1 do begin       ;do both commands
                    cmd = cmds(j)
                  IF not Keyword_set(qdebug) THEN BEGIN
                    print, 'cmd: ', cmd
                    spawn, cmd, status
                  ENDIF ELSE BEGIN
                    print, 'cmd: ', cmd
                    status = ['Debug ','Testing']         ;force ck
                  ENDELSE
		  if j eq 0 then begin
		   status = file_exist( dir1, /direct)
                    IF not status THEN BEGIN   ;unable to create dirs.
                      Print, 'Error: Failure in creating Tape dump directories.'
                      Print, 'Failed cmd: ', cmd
                      Print, 'Error Status: ', status
                      if not Keyword_set(qdebug) then begin
                       stop, 'Error: stop in MO_tap_dump, unable to create dump tape dirs!'
                      endif
		    ENDIF
		  endif
	        ENDFOR
		fids = string(drecs(1:*).fileid)	;full list of fids

		If not Keyword_set(qdebug) Then begin
		  rdtap, def_tapd(mt_dev(i)), prefix=prefix,fileid=fids, $
			dpath = dir1
		Endif Else Begin
		  Print, "Ready to call 'rdtap': "
		  print, 'rdtap, def_tapd(mt_dev(i)), prefix=prefix,'+ $
			'fileid=fids, dpath = dir1'
		  help, prefix,fids,dir1
		  print, 'MT: ', def_tapd(mt_dev(i))
		  print, 'prefix: ', prefix
		  print, 'dir1: ', dir1
		  print, 'fids: ', fids
		Endelse

	      endif			;end of making and dumping
	    endif else begin		;this week does exist
;		ck for files within this directory
;		ck for valid files 
	        Print, 'This weekly Directory already exists!'
	    endelse
          ENDIF	else begin 		;end dump of valid week
	    Print, 'OPERATOR ERROR: Following tape does NOT match '+ $
		'the weeks on the desired MO:'
	    Print, 'Tape name: ', string(hdr.tapename)
	    Print, 'MO weeks: ', wks
	    Print, 'NO Archive tape data dumped!!!'
          ENDELSE
	ENDFOR

	mt_status = do_mt		;return tape dump status

        wks_status = intarr(n_elements(wks))	;wks to dump to MO
	wks_status(*) = 0			;dump none

	itaps = where(do_mt ne 0, nvtaps)

	IF nvtaps ne 0 then begin	;must have some to ck
	  vtap_wk = tap_wk		
	  vtap_wk = vtap_wk(where(do_mt ne 0))	;only check valid tapes
	
	  FOR i= 0, n_elements(wks)-1 do begin
	    wks_ck = where( mo_wks(i) eq vtap_wk)
	    if wks_ck(0) ne -1 then begin		;no match found
	      wks_status(i) = 1		;set to dump
	    endif  
          ENDFOR
	ENDIF

	END
