	PRO mo_prep, mo_node, mo_devin, mo_disk, wks, mo_no, $
		init=init, mount=mount, umount=umount, $
		mkdir=mkdir, chdir=chdir, qdebug=qdebug, old=old
;	--------------------------------------------------------------
;+							10-Feb-94
;	NAME: mo_prep
;	
;	PURPOSE: prepare mo for archiving.
;
;	CALLING SEQUENCE:
;
;	mo_prep, mo_node, /init, /tune, /mount, /umount, /mkdir='002B', /old
;
;	Examples:
;		mo_prep, 'flare15', 0, /mount 	;mount /mo/flare15.0	
;		mo_prep, 'flare15', 1, /umount	;unmount /mo/flare15.1
;		
;	INPUT:	
;	mo_node	name of node with mo device (eg 'flare15' or 'flare13').
;	mo_dev	mo device number (eg. 0 or 1)
;	OPTIONAL INPUT:
;	mount	if set will mount mo
;	umount 	if set will umount mo
;	mkdir	if set will make the directories assoicated with 
;		the SELECTED mo#: The mo# is slected via
;		a) menu of MO of sides (interactive mode).
;		b) At the caller via mkdir ='000B'. 
;		NOTE: The week directories are check for and if they
;		exist they are not re-created (important feature for
;		updating MOs)!
;	qdebug	if set will display spawn commands but will not do
;		actually do anything.
;	old	if set will format MO to low density (1Gb)=448Mb/side
;		otherwise, will format high density (1.2Gb)=577Mb/side
;		
;	THE FOLLOWING OPTIONS HAVE BEEN MOVED TO a NEW routine: MO_INIT
;	init	if set will run mkfs to max. MO space useage
;	tune	if set will reduce the free space threshold to 0%. 
;		(Note: use either tune or init but NOT both!!)
;
;	OUTPUT:
;	mo_disk	The "top" mo directory name or mount point.
;	wks	List of weeks that belong on selected MO.
;	mo_no	Selected indices to the MO disk map.
;
;
;	HISTORY: written 8-apr-93, gal
;		updated 16-apr-93, gal, Use "mocreate" account
;		updated 30-Apr-93, gal, Add debug option 
;		updated 17-May-93, gal, Updated MO mount command for
;			writing to MO and add a tune switch.
;		updated 21-May-93, gal, corrected dir. ck "rsh" cmd
;			and changed the mkdir and chd_mod cmd to work
;			in "rsh" commands so the host does not have
;			to have the mo locally mounted.
;		updated 26-May-93, mdm, changed logic on recognizing when
;			an error occurs on the rsh command
;		updated  1-Feb-94, gls,slf (use momount, reinstate /innit)
;			 2-Feb-94 - if init, dont use rsh command 
;			 rsh mo_node -l mocreate (ultrix) 
;			     mo_nod -l mocreate (others)
;		updated 10-Feb-94, gal, corrected a bug in the rcmd 
;		updated 25-Feb-94, gal, minor update includes document.
;                       24-Jun-94, slf, use is_alive, blank between rsh and node
;			26-Jun-94, slf, make sure mo is REALLY  mounted
;				   (sanity check on disksize and mount name)
;		updated 3-Oct-95, LS, changed path to new mo_mount
;					style /mo/flareN.X
;               updated 18-Oct-95, LS, added /old flag to call.  this will
;                        allow formatting of the lower 1Gb MOs.  otherwise,
;                        formatting will default to the higher density 1.2Gb
;
;
;-
;	--------------------------------------------------------------
	rpre=['/usr/ucb/rsh ','rsh ']
	rcmd=rpre(strlowcase(!version.os) eq 'ultrix') + mo_node + ' -n -l mocreate '

	if keyword_set(init) and getenv('HOME') ne '/' then begin
	   tbeep,3
	   message,/info,'Must be root to initialize MOS, exiting...
	   return
	endif

	account = 'mocreate'		;default account for now...
					;cause it works
	acc_sw  = '-l '+account+' -n '	;account cmd

	ss2 = -1			;initial value of selected MO#
;	Do Node check:
        if not is_alive(mo_node) then begin
          message,/info,"mo node is not available, exiting"
	  return
	ENDIF
		
	if not keyword_set(mo_devin) then mo_dev='0' $
		else mo_dev=strtrim(string(mo_devin),2)

;	mo_disk = '/mo'+mo_dev+'.'+mo_node	;build volume name
	mo_disk = '/mo/'+mo_node+'.'+mo_dev     ;build volume name
        print,'MO disk: ' + mo_disk
	IF KEYWORD_SET(tune) THEN BEGIN
	  cmd = 'tunefs -m 0'		;reduce the free space from 10% to 0%
          cmd = rcmd + cmd	;build full command

	  Print, 'This command will reduce the free space threshold to 0%!'
	  Print, 'Confirm that you have the Correct MO disk in the drive!!'

	  If not Keyword_set(qdebug) THEN begin
	    print, 'cmd; ', cmd
	    spawn, cmd, status		;return status
;	    status = ['','Testing']	;not working yet
	  ENDIF ELSE BEGIN
	    print, 'cmd; ', cmd	
	    status = ['Debug ','Testing']	;not working yet
	  ENDELSE

	  IF status(0) ne '' THEN BEGIN
	    print, 'tunefs was successful!'
	  ENDIF ELSE BEGIN
	    print, 'ERROR: tunefs failed'
	    print, 'Error status: ', status
	    return
	  ENDELSE
	ENDIF

	IF KEYWORD_SET(init) THEN BEGIN
					; if /old, then format low density
	  if (n_elements(old) eq 1 ) then begin
				n_sectors=' 904704 '
				min_mem=400.
			endif else begin	; higher density= more sectors!
				n_sectors=' 1163264 '
				min_mem=550.
			endelse
	  cmd = 'mkfs /dev/rsmo'+mo_dev+n_sectors+' 32 16 8192 1024 16 0 60 32768'
;	  slf - removed rsh for this command - must be done from root
;          cmd = rcmd+cmd	;build full command
	
	  Print, 'This command will erase the disk!'
	  Print, 'Confirm that you have the Correct MO disk in the drive!!'

	  If not Keyword_set(qdebug) THEN begin
	    print, 'cmd; ', cmd
            momount,mo_node,drive=fix(mo_dev),/umount
	    spawn, cmd, status		;return status
;	    status = ['','Testing']	;not working yet
	  ENDIF ELSE BEGIN
	    print, 'cmd; ', cmd	
	    status = ['Debug ','Testing']	;not working yet
	  ENDELSE

	  IF status(0) ne '' THEN BEGIN
	    print, 'Init was successful!'
	    message,/info,'Creating Yohkoh directory...
            momount,mo_node,drive=fix(mo_dev),/write
;           add check to avoid writing to mount point (not mo)
            avail=diskfree(mo_disk,mount=mnt)
            message,/info,"Mount Status..."
            print,"Mount Name: " + mnt + "  Availble (Mbytes): " + strtrim(avail,2)
				; must pass minimum memory spec.
            if mnt eq '/' or avail le min_mem then begin
	       tbeep,3
               message,/info,"Problem with mount, aborting without writing"
	       stop
            endif
	    ysdir=mo_disk + '/yohkoh'
	    cmd='mkdir -p ' + ysdir + '; chmod 777 ' + ysdir
            spawn,cmd,status
            momount,mo_node,drive=fix(mo_dev),/umount
	  ENDIF ELSE BEGIN
	    print, 'ERROR: Init failed'
	    print, 'Error status: ', status
	    return
	  ENDELSE
	ENDIF			;end init

	IF KEYWORD_SET(mount) THEN BEGIN	;mount MO
;	  script=concat_dir('$DIR_SITE_SCRIPT','momount')
	  script='/usr/local/bin/mo_mount'
          cmd= script + ' ' + mo_node + ' /write '
          if mo_dev ne '0' then cmd=cmd + '/' + mo_dev
	  IF not Keyword_set(qdebug) THEN BEGIN
	    print, 'cmd: ', cmd
	    spawn, cmd, status
            avail=diskfree(mo_disk,mount=mnt)
            message,/info,"Mount Status..."
            print,"Mount Name: " + mnt + "  Availble (Mbytes): " + strtrim(avail,2)
            if mnt eq '/' then begin 
	       tbeep,3
               message,/info,"Problem with mount, aborting without writing"
               stop
            endif
	  ENDIF ELSE BEGIN
	    print, 'cmd: ', cmd
	    status = ['Debug ','Testing']		;force ck
	  ENDELSE
	  IF status(0) ne '' THEN BEGIN
	    print, 'Mount was successful!'
	    print, status(n_elements(status)-1)
	  ENDIF ELSE BEGIN
	    print, 'ERROR: Mount failed'
	    print, 'Error status: ', status
	    return
	  ENDELSE
	ENDIF			;end mount

	IF KEYWORD_SET(umount) THEN BEGIN	;umount MO
;	  script=concat_dir('$DIR_SITE_SCRIPT','momount')
	  script='/usr/local/bin/mo_mount'
	  if n_elements(mo_dev) ne 0 then $
	  cmd=script + ' ' + mo_node + ' /' + mo_dev + ' /umount
	  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 status(0) ne '' THEN BEGIN
	    print, 'UMount was successful!'
	    print, status(n_elements(status)-1)
	  ENDIF ELSE BEGIN
	    print, 'ERROR: UMount failed'
	    print, 'Error status: ', status
	    return
	  ENDELSE
	ENDIF			;end mount

	IF KEYWORD_SET(mkdir) THEN BEGIN

	  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)

	  ss = uniq(label_sides)
	  usides = label_sides(ss)
	  umdm = mdm(ss)
	  
	  info_mkdir = SIZE(mkdir)
	  IF info_mkdir(info_mkdir(0)+1) eq 7 THEN BEGIN  ;MO side selected
	    mkdir = strupcase(mkdir)
	    mo_sides = strmid(label,0,4)	;pick-off sides names
	    ss2 = where(mo_sides eq mkdir, ntapes)
	    If (ntapes lt 1) THEN BEGIN		;error in pick
	      Print, 'Error: invalid choice of MO SIDE'
	      return
	    ENDIF
	  ENDIF ELSE BEGIN
	    iside = wmenu_sel(usides, /one)	;PICK MO SIDE 
	    if (iside(0) eq -1) then stop           ;aborted
	    side2do = usides(iside)
	    side2do = side2do(0)            ;turn into scalar

	    ss2 = where(label_sides eq side2do)		;selected MOside
	  ENDELSE
	  wks = string(mdm(ss2).st$week)		;selected weeks 
	  dir1 = concat_dir(mo_disk, 'yohkoh')	
          print,'dir1: ' + dir1
	  cmd  = rcmd +'ls -d '+dir1 ;only check for dirs
	  IF not Keyword_set(qdebug) THEN BEGIN
	    print, 'cmd: ', cmd
	    spawn, cmd, status
	  ENDIF ELSE BEGIN
	    print, 'cmd: ', cmd
	    status = ['','Testing']	;force no dir found for test
	  ENDELSE                      
	  IF status(0) eq '' THEN BEGIN		;dir1 was not found
;	    assume the device is not necessary remote mounted
;		so no "cd" commands
;	    cmd = 'cd ' + mo_disk + '; mkdir yohkoh; chmod 777 yohkoh'
	    mkd_cmd = 'mkdir ' + dir1 
	    chm_cmd = 'chmod 777 '+ dir1
	    cmds = strarr(2)
	    cmds(0) = rcmd+mkd_cmd
	    cmds(1) = rcmd+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 status(0) eq '' THEN BEGIN	;unable to create dirs.		;MDM changed 26-May-93
	      IF status(0) ne '' THEN BEGIN	;unable to create dirs.
	        Print, 'Error: Failure in creating MO directories.'
	        Print, 'Failed cmd: ', cmd
	        Print, 'Error Status: ', status
	        if not Keyword_set(qdebug) then begin
	          stop, 'Error: stop in MO_PREP, unable to create MO dirs!'
	        endif 
	      ENDIF
	    ENDFOR
	  ENDIF

	  FOR i= 0, n_elements(wks)-1 DO BEGIN
	    week = wks(i)		;check this week
	    dir2 = concat_dir(dir1, week) 
	    cmd =  rcmd+'ls -d '+dir2	;ck dirs only
	    IF not Keyword_set(qdebug) THEN BEGIN
	      print, 'cmd: ', cmd
	      spawn, cmd, status
	    ENDIF ELSE BEGIN
	      print, 'cmd: ', cmd
	      status = ['','Testing']		;force no dir2 found
	    ENDELSE                      
	    IF status(0) eq '' THEN BEGIN	;dir2 was not found
;	      assume the device is NOT necessary remote mounted
;		so no 'cd" commands
;	      cmd = 'cd ' + dir1 + '; mkdir ' + week + '; chmod 777 ' + week
	      mkd_cmd = 'mkdir ' + dir2 
	      chm_cmd = 'chmod 777 '+ dir2
	      cmds = strarr(2)
	      cmds(0) = rcmd+mkd_cmd
	      cmds(1) = rcmd+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 status(0) eq '' THEN BEGIN	;unable to create dirs.		;MDM changed 26-May-93
	        IF status(0) ne '' THEN BEGIN	;unable to create dirs.
	          Print, 'Error: Failure in creating MO directories.'
		  Print, 'Failed cmd: ', cmd
	          Print, 'Error Status: ', status
	          if not Keyword_set(qdebug) then begin
	            stop, 'Error: stop in MO_PREP, unable to create MO dirs!'
	          endif 
	        ENDIF
              ENDFOR
	    ENDIF
	  ENDFOR
	  mo_no = ss2			;return indices to MO disk map
; stop, 'chk ipart, ...weeks and mo_disk, ss2'
	ENDIF

	END
