	PRO mk_mo2, mo, mo_node, mo_dev, mag_disk, mt_dev=mt_dev, $
		interactive=interactive, start_week = start_week, $
		qdebug = qdebug, wks_status=wks_status
;	-------------------------------------------------------------
;+							11-Aug-1994
;	NAME: mk_mo2
;
;	PURPOSE: Make an MO archive disk from online data or an 
;		Exabyte Archive Tape.
;
;	CALLING SEQUENCE:
;		mk_mo2, mo, mo_node, mo_dev, mag_disk, mt_dev=[0,1]
;	Examples:
;	  from online data:
;		mk_mo2, '058A', 'flare4', 0 	;archive fr "on-line data"
;		mk_mo2, '058B', 'flare4', 1	;to MO # 1 on flare4
;	  from 8mm tape:
;		mk_mo2, '003B', 'flare4', 0, mt_dev=[0,1] ;archive f/tapeS
;	  from staging disk:
;		mk_mo2, '006B', 'flare4', 0, '/yd13' ;archive f/mag_disk
;	
;	INPUT:
;	mo		MO number to make (eg. 000A, 000B, 010A)
;	mo_node		Node name where MO device is located.
;	mo_dev		MO device number...(eg. 0 or 1)
;
;	Optional Input: 
;	mag_disk	Directories for staging tape data to MO
;			Default are: ['/yd19'] 
;			if input undefined-- assumes /yd19...
;		  Also,	if define and mt_dev is undefined: data is
;			copied from this disk to MO.
;	mt_dev		if set will copy data from tape. 
;			Tape device number 0 or 1, and can be a 
;			vector e.g. mt_dev=[0,1]
;	interactive	Normal mode is Batch mode, but if this switch 
;			is set it forces an interactive run.
;	start_week	(NOT WORKING YET!) When more than 4 per 
;			MO side... (eg for mo=' 007a', start_week='91_01')
;
;	LIMITS:
;		Only will dump a maxium of two archive weeks to MO.
;		When more than two tape archive weeks are contained
;		on an MO side must use "start_week" to dump the 3rd
;		and 4th weeks to MO.
;
;	HISTORY:
;		written by gal 9-Apr-93
;		 6-May-93 (GAL) - general mods.
;		15-May-93 (GAL) - More mods to mk_mo2
;		26-May-93 (MDM) - Added WKS_STATUS keyword option
;		31-May-93 (MDM) - Added call to MK_MO_LOG
;		 1-Jun-93 (MDM) - Removed the 26-May-93 modification to
;				  be able to do parts of an MO disk.
;				- Added some print statements
;		 2-Jun-93 (MDM) - Corrected typo introduced 1-Jun-93
;		15-Jun-93 (MDM) - Changed default staging disk from /yd13
;				  to /yd19
;		 8-Jul-93 (MDM) - Modified to use /DIR_DATE option to MK_MO_LOG
;		21-Dec-93 (MDM) - Added statements to create a text log file
;				  of MOs that are made (call to LIST_MO_LOG)
;		 4-Jan-94 (MDM) - Added a MOUNT/UMOUNT of the MO disk from the
;				  local machine so that it can see the MO disk
;				  to do the logging stage.
;	         1-Jul094 (SLF) - replace spawn,'rm' with file_delete.pro
;				  (avoid shell)
;		11-Aug-94 (GAL) - added mo_check call and trap for the
;				  case where bad files are found on the
;				  mo and mo_patch would have to be run.
;				  call to mo_patch is limited to 1st disk
;				  in the list mag_disk.
;-
;	--------------------------------------------------------------

	IF keyword_set(interactive) then begin
	  batch = 0		;set to interactive mode
	ENDIF ELSE BEGIN
	  batch = 1		;default to batch mode
	ENDELSE

;	Define Operation Mode: (online or catch-up: mt or mag_disk)
;	IF (not keyword_set(mt_dev)) then begin		;MDM replace 26-May-93
	IF (n_elements(mt_dev) eq 0) then begin
	  mt = 0		;either doing online or catch-up f/staging
	ENDIF ELSE BEGIN
	  mt = 1		;doing catch-up week from 8mm tape
	ENDELSE

	IF n_elements(mag_disk) eq 0 THEN BEGIN
	  if mt then begin				;using tape
	    ;mag_disk = ['/yd13']			;default staging
	    mag_disk = ['/yd19']			;default staging changed to /yd19 15-Jun-93
	    staging = 1			                ;yes staging
	  endif else staging = 0	;using online, no tape or staging
	ENDIF ELSE BEGIN
          Print, 'Using Staging Disk(s): ', mag_disk
	  staging = 1
	ENDELSE

	IF staging THEN BEGIN		;check for valid directories
	  For i=0, n_elements(mag_disk)-1 Do Begin
	    ckmdisk = finddir(mag_disk(i))	;check for directory
	    Print, 'mag_disk Directory verified: ', ckmdisk
	    if (ckmdisk ne '') then begin
	      mag_disk(i) = ckmdisk		;save full path to disk
	    endif else begin
	      Print, "Error: Invalid 'mag_disk' directory: ", mag_disk(i)
	      Return
	    endelse
	  Endfor
	ENDIF

	momount, mo_node, drive=mo_dev			;MDM added 4-Jan-94
	momount, mo_node, drive=mo_dev, /umount		;MDM added 4-Jan-94

	mo_prep, mo_node, mo_dev, mo_disk, wks, mo_no, /mount, $
			mkdir=mo, qdebug = qdebug
				;Use init switch after J-side gets
				;an mo init. account!
	
	if (n_elements(wks_status) eq 0) then begin
	  wks_status = intarr(n_elements(wks))+1 
	endif else begin
	  wks_status = [wks_status, 0, 0, 0, 0, 0, 0, 0]	;make sure it is as long as "wks"
	endelse

;	MAYBE TO DO: add logic for start_week switch
;	 ----- limit wks to two elements.....
;	SO MO_NO==[12,13,14,15] => [12,13] OR W/START.,.=[14,15]

;	DO ANY ON-LINE DIRECTORIES WITH VALID DATA FILES FIRST:
	FOR i=0, n_elements(wks)-1 DO BEGIN 	;do for each week.

	  print, 'Looking to do week: ', wks(i)	;MDM added 1-Jun-93
          online_wk = strmid(wks(i), 0, 5)	;for each week
	  wk_path = wk_online(online_wk)	;get path to online dir.
	  print, 'On-line data directories found for that week: ', wk_path		;MDM added 1-Jun-93
	  IF wk_path ne '' THEN BEGIN		;if wk_path is defined

	    mo_wk_no = mo_no(i)		;pass the MO week index 
	    mo_fnck = mo_fndirck( wk_path, mo_wk_no)
	    IF mo_fnck(0) THEN BEGIN	;yes the proper files were found!

	      If staging then begin 
	    	if n_elements(mag_disk) eq 1 then begin 
	      	  mdisk = mag_disk
	        endif else begin		;more than 1 dir
	      	  mdisk = mag_disk(i)		
	    	endelse
	      Endif

	      Print, 'Using On-line data directory(s) for MO archive.'
	      Print, 'Copying from '+strtrim(wk_path, 2)+' to MO.'

	      disk2_mo, mo_disk, wk_path, mo_wk_no, $
			progress=[i+1, n_elements(wks)], qdebug = qdebug

	    ENDIF		;end check for proper files within week dir.
	  ENDIF			;end check for on-line week dirs. status

	ENDFOR

;	IF DUMPING DATA FROM ARCHIVE TAPES: DUMP and DO only the New DIR
	If mt THEN BEGIN            	;dump the archive tape(s)
	  prefix = prefixck(/noobs)
	  prefix = [prefix(0:1), prefix(3:*)]
	  ;mo_tap_dump, mo_node, mt_dev, prefix, wks, mag_disk, $
	  ;mo_tap_dump, mo_node, mt_dev, prefix, wks(where(wks_status)), $
	  mo_tap_dump, mo_node, mt_dev, prefix, wks, $		;MDM removed where
	   mag_disk, mt_status, wks_status, qdebug=qdebug     ;MDM 26-May-93 mod

;	  NOTE: This loop uses the "wks_status" from mo_tap_dump to do
;	  only those weeks which were created and filled by tape dumping:

	  FOR i=0, n_elements(wks)-1 DO BEGIN 	;do for each week.

	    If wks_status(i) THEN BEGIN		;do MO copy from tape dump

;	      recheck to see if dir. was created and tape dump successful:
              online_wk = strmid(wks(i), 0, 5)	;for each week
	      wk_path = wk_online(online_wk)	;get path to online dir.
	      IF wk_path ne '' THEN BEGIN		;if wk_path is defined

	        mo_wk_no = mo_no(i)		;pass the MO week index 
	        mo_fnck = mo_fndirck( wk_path, mo_wk_no)
	        IF mo_fnck(0) THEN BEGIN	;yes the proper files were found!

	          If staging then begin 
	    	    if n_elements(mag_disk) eq 1 then begin 
	      	      mdisk = mag_disk
	            endif else begin		;more than 1 dir
	      	      mdisk = mag_disk(i)		
	    	    endelse
	          Endif

	          Print, 'Using New Archive Tape Dump directory(s) for MO archive.'
	          Print, 'Copying from '+strtrim(wk_path, 2)+' to MO.'

	          disk2_mo, mo_disk, wk_path, mo_wk_no, $
			progress=[i+1, n_elements(wks)], qdebug = qdebug

	        ENDIF		;end check for proper files within week dir.
	      ENDIF		;end check for on-line week dirs. status

	    ENDIF		;end check for tape dump directorys

	  ENDFOR

	ENDIF			;end for MT dump and copy

	If (not keyword_set(qdebug)) Then Begin
	  mo_check, mo, mo_node, drive=mo_dev,/readchk,summary=summary
	  result = strpos(summary(n_elements(summary)-1), '0')	; # of bad files
	  if (result eq -1) then begin			; need to patch mo
	    mo_patch, mo, mo_node, /nostage, mag_stage=mag_disk(0)
;	    *** Need to update mo_patch to take a vector of staging disks.
	  endif

	  mk_mo_log, mo_node, mo_dev, /dir_date		;MDM added 31-May-93

	  mo_prep, mo_node, mo_dev, /umount	;unmount MO

	  message = ['MO # '+mo+' is completed and unmounted!', $
		' Ensure that side '+strmid(mo, 3, 1)+' is WRITE-LOCKED'+ $
		' before filing this MO to the MO archive!']
	  mail, message, subj='MK_MO is Done', user='$MAIL_MK_MO', /self

	  outfil2 = getenv('DIR_GEN_MO') + '/mos_made.log'		;MDM added 21-Dec-93
          file_delete,outfil2
	  list_mo_log, outfil=outfil2					;MDM added 21-Dec-93
	Endif Else Begin
	  mo_prep, mo_node, mo_dev, /umount, /qdebug	;unmount MO

	  message = ['MO # '+mo+' is completed and unmounted!', $
		' Ensure that side '+strmid(mo, 3, 1)+' is WRITE-LOCKED'+ $
		' before filing this MO to the MO archive!']
	  mail, message, subj='MO test, This is only a Test', $
		user='$MAIL_MK_MO', /self
	Endelse


	END
	  
