pro mk_mo_disk2, mo_disk, mag_disk, dev=dev, mo_wk_no=mo_wk_no, $
		batch=batch, qdebug=qdebug, progress=progress
;	-------------------------------------------------------------
;							29-Apr-93
;+
;NAME:
;	mk_mo_disk2
;PURPOSE:
;	This is a program to copy the Yohkoh data from exabyte to
;	MO Disk
;SAMPLE CALLING SEQUENCE:
;	mk_mo_disk2, '/mo/flare15.0'
;	mk_mo_disk2, '/mo/flare15.0', '/yd13/mo_1', dev=1
;	mk_mo_disk2, '/mo/flare15.0', '/yd13/mo_1', dev=1, 
;		mo_wk_no=mo_wk_no, /batch, /qdebug, progress=progress
;INPUT:
;	mo_disk	- The MO directory name
;	mag_disk- The mag disk directory name for staging to MO
;OPTIONAL KEYWORD INPUT:
;	dev	- The device name for the exabyte.  If not passed,
;		  it uses device 0.
;	mo_wk_no- If set, do not prompt user for MO and wk#. use value.
;	batch	- If set, does not prompt user for tape!!! Tapes must
;			already be in the drives!
;	qdebug	- If set, do not execute the spawn commands to 
;		  create the directories and do not call RDTAP
;	progress- Gives the current week count and total to status
;		  the job: (e.g. [1,2] is week 1 of 2, [2,2] is 
;		  week 2 of 2 or last week on side.
;HISTORY:
;	Written 3-Feb-92 by M.Morrison (really 3-Feb-1993, gal)
;	modified, 4-Feb-93, by GAL, corrected usage of tape dev var,
;			and range switch on rd_tap.
;	modified, 5-Feb-93, correct cd command, gal.
;	modified, 9-Apr-93, for automated use.
;	mods,	10-Apr-93, for batch mode
;	mod,	29-Apr-93, to use mocreate account on flare machines
;			for moving files to MO.
;			Also added mail and status features.
;	mod,	3-May-93, added feature to allow the 2nd tape dump 
;		to proceed while the 1st move is occurring.  Since,
;		the MO can not be removed until the last mv is complete
;		the last mv command is NOT executed in the background.
;	mod,	3-Oct-95, LS, changed path to new mo_mount style /mo/flareN.X
;                       changed default machine to flare15
;                       have to parse mo_disk 's new style
;	mod,	1-Nov-95, LS, changed message about how long the process will take.
;                       with the larger capacity MOs, it takes more like 1.5 hrs.
;-
;	--------------------------------------------------------------

	account = 'mocreate		;default account MO making
	acc_sw	= '-l '+account+' -n '	;account cmd

stage = 0			;assume no stageing 

if (n_elements(mo_disk) eq 0) then mo_disk = '/mo/flare15.0'
fpos = strpos(mo_disk, 'flare',0)	;find which machine mo is on
mach = strmid(mo_disk,fpos, strpos(mo_disk,'.',0)-fpos )
spawn, 'ping '+mach, ping_ans		;check machine status
status_m = strpos(ping_ans,'unknown',0)
IF status_m(0) ne -1 then begin
  Print, 'Error: ', strtrim(mach,2), 'with the MO drive is not available'
  return
ENDIF
if (n_elements(dev) eq 0) then begin
  dev = def_tapd(0)
endif else begin
  devsz = size(dev)
  If devsz(devsz(0)+1) ne 7 then dev = def_tapd(dev)	;get the name
endelse
;
infil = concat_dir('$DIR_GEN_MO', 'mdm')
restgen, mdm, file=infil			;MO Disk Map
;
n = n_elements(mdm)
label = strarr(n)
fmt = '(i3.3, a, 2x, a, 3x, a, 6x, a, 2x, a, i5)'
for i=0,n-1 do begin
    mdm0 = mdm(i)
    label(i) = string(mdm0.mo_disk, string(mdm0.st$side), string(mdm0.st$label), string(mdm0.st$week), $
		string(mdm0.st$first_fid), string(mdm0.st$last_fid), mdm0.nfid, format=fmt)
end
label_sides = strmid(label, 0, 18)
;string(mdm.mo_disk, format='(i3.3)') does not do the right thing
;
ss = uniq(label_sides)
usides = label_sides(ss)
umdm = mdm(ss)
;
IF n_elements(mo_wk_no) eq 0 THEN BEGIN	;user has not selected mo and wk
  iside = wmenu_sel(usides, /one)
  if (iside(0) eq -1) then stop		;aborted
  side2do = usides(iside)
  side2do = side2do(0)		;turn into scalar
;
  ss2 = where(label_sides eq side2do)
  if (n_elements(ss2) eq 1) then begin
    ipart = 0
  end else begin
    ipart = wmenu_sel(label(ss2), /one)
    if (ipart(0) eq -1) then stop		;aborted
  end
;
  mdm2do = mdm(ss2(ipart))
ENDIF ELSE BEGIN
  mdm2do = mdm(mo_wk_no)		;use passed value (scaler)
  iside = where( usides eq label_sides(mo_wk_no) )
  if iside(0) eq -1 then stop		;aborted
  iside = iside(0)
ENDELSE
first_fid = string(mdm2do.st$first_fid)
last_fid = string(mdm2do.st$last_fid)
;
tt = string(mdm2do.mo_disk, format='(i3.3)')
if (string(mdm2do.st$side) eq 'A') then begin
    tape_label = tt + '   ' + string(umdm(iside).st$label)  + ' / ' + string(umdm(iside+1).st$label)
end else begin
    tape_label = tt + '   ' + string(umdm(iside-1).st$label)  + ' / ' + string(umdm(iside).st$label)
end
fileID = [first_fid, last_fid]			;gal change var 
prefix = ['ada', 'bda', 'hda', 'sfr', 'spr', 'wda']

print, 'Confirm that the MO disk label is: ' + tape_label
print, 'and that you have side ' + string(mdm2do.st$side) + ' inserted'
print, '  '

;	Look for data files on staging area: compute weekID and compare
IF n_elements(mag_disk) ne 0 THEN BEGIN		;stageing 
  wid = getwid('ada',mag_disk)
  IF wid(0) ne '' then begin
    IF wid(0) eq strmid(string(mdm2do.st$week), 0, 5) THEN BEGIN
      stage = 1			;staging is complete
      Print, 'Staging data for week ', wid(0),' is complete.'
      Print, 'Will proceed with Moving this data to MO now!'
    ENDIF ELSE BEGIN
      stage = -1		;dump to mag disk 1st
      Print, 'Data on Staging area is not from the correct data week!'
    ENDELSE
  ENDIF ELSE BEGIN		;No data found on Staging disk
    stage = -1			;dump to mag disk 1st
    Print, 'No data found on Staging Disk area.'
    Print, 'Will proceed with Dump of data from tape to staging disk.'
    Print, '     '
  ENDELSE
ENDIF ELSE BEGIN		;no staging requested--go directly to MO
  stage = 0
ENDELSE

IF stage ne 1 then BEGIN		;1st dump from tape
  print, 'Insert the Exabyte for week: ' + strmid(string(mdm2do.st$week), 0, 5)
  print, '  '
  IF Keyword_set(batch) THEN BEGIN
;   could add a check on the tapes ...rd tapdir and match!!! later...
    ans = 1			;assume were ready...lets go!
  ENDIF ELSE BEGIN
    yesnox, 'Are you ready to do the copy? ', ans, 'Yes'
  ENDELSE
 
  if (ans eq 1) then begin		;ready to read tape
    dir1 = concat_dir(mo_disk, 'yohkoh')
    exist = file_exist( dir1, /direct)
    if (not exist) then begin
	cmd = 'cd ' + mo_disk + '; mkdir yohkoh; chmod 777 yohkoh'
	print, cmd
	if (not keyword_set(qdebug)) then spawn, cmd
    end

    week = string(mdm2do.st$week)
    dir2 = concat_dir(dir1, week)
    exist = file_exist( dir2, /direct)
    if (not exist) then begin
	cmd = 'cd ' + dir1 + '; mkdir ' + week + '; chmod 777 ' + week
	print, cmd
	if (not keyword_set(qdebug)) then spawn, cmd
    end

    print, 'cd, '+dir2
    if (not keyword_set(qdebug)) then cd, dir2
;stop, 'chk dir2
  end				;end from ready to read tape

  if stage eq -1 THEN BEGIN	;dump tape to stage disk
    cd, mag_disk
  ENDIF
  print, 'FILEID range =  ', fileID
  print, 'PREFIX = ', prefix
  print, 'rdtap, dev, fileID= fileID, prefix=prefix, /range
  if (not keyword_set(qdebug)) then rdtap, dev, fileID=fileID, $
		prefix=prefix, /range

ENDIF 

IF n_elements(progress) ne 0 THEN BEGIN		;progress is defined
  If progress(0) eq progress(1) THEN BEGIN
    message = ['Staging Tape Data to Magnetic Disk is Complete!.', $
		' Tape drives are FREE for other usage!']
    mail, message, subj='Tape Drives are Free!', user='$MAIL_MK_MO', /self
  ENDIF ELSE BEGIN				;not done yet
    message = ['Week '+strtrim(progress(0), 2)+' of '+strtrim(progress(1),2)+ $
	   ' ('+strmid(string(mdm2do.st$week), 0, 4)+') staging is complete.',$
	   ' Proceeding to Move this data to MO.']
    mail, message, subj='Data staged to mag. disk', user='$MAIL_MK_MO', /self
  ENDELSE
ENDIF


IF ABS(stage) eq 1 THEN BEGIN	;Staged files ready to copy to MO
  cd, mag_disk
  dir1 = concat_dir(mo_disk, 'yohkoh')
  week = string(mdm2do.st$week)
  dir2 = concat_dir(dir1, week)
; exist = file_exist( dir2, /direct)	;help mons- how to with rsh...
;	if exist then do....
  Print, 'Moving Files to MO on: ', mach
  Print, 'This takes about 1.5 hours... and bell rings when done.'
  IF n_elements(progress) ne 0 THEN BEGIN	;progress is defined
    If progress(0) eq progress(1) THEN BEGIN	;Last mv (move) commmand
      cmd = 'rsh '+mach+' '+acc_sw+'mv '+mag_disk+'/* '+dir2+'/.'
    ENDIF ELSE BEGIN		;Before last move, do mv and tape dump together
      cmd = 'rsh '+mach+' '+acc_sw+'mv '+mag_disk+'/* '+dir2+'/.&'
    ENDELSE
  END
  Print, 'Spawn CMD: ', cmd
  if (not keyword_set(qdebug)) then spawn, cmd		;move files to MO
  bell, 5
  Print, 'Copy to MO completed'
  bell, 5
  
END

;
end
