pro mo_patch, moname, monode, modev=modev, tapdev=tapdev, $
	mag_stage=mag_stage, nostage=nostage, noconfirm=noconfirm, nospawn=nospawn
;
;+
;   Name: mo_patch
;
;   Purpose: use mo_check bad log to patch an MO disk from exabyte
;
;   Calling Sequence:
;      mo_patch, moname, monode [modev=modev , tapdev=tapdev, mag_stage=mag_stage]
;
;   Calling Example:
;      mo_patch,'038a','flare4'		     ;  tapd0 -> flare4 mod0
;
;      mo_patch,'043a','flare13', mod=1, tapd=1 ; tapd1 -> mod1
;
;   History:
;      7-Jul-1994 (SLF) Written
;      8-Jul-1994 (SLF) Mods - placed online
;      9-Jul-1994 (SLF) upgraded auto-mail message, mt offline in background, 
;			Verify staged (input) valid (or abort),  RECURSE!!
;     12-Jul-1994 (SLF) Mail to executing user only, change output file
;			generation
;     13-Jul-1994 (SLF) use str2cols for names, add directory create/chmod
;     18-sep-1994 (SLF) file delete & chmod protection...
;
;   Side Effects:
;      Data on MO disk may be changed (it should only get better, not worse...)
;
;   Restrictions:
;      Must be run on a machine with a tapedrive (isass0, isass2, flare2)
;-
;


; --------------- verify moname ------------------
if not data_chk(moname,/string) then begin
   message,/info,"Must supply the MO name (ex: 085A), returning..."
   return   
endif

confirm=1-keyword_set(noconfirm)

mon=strupcase(moname)
modb=rd_modb(uniq_side=usides)
which=where(usides eq mon,cnt)
if cnt eq 0 then begin
   message,/info,"Can't locate MO# " + mon + " in data base, returning..."
   return
endif
;
; ------------------ set the stage -------------------
if not keyword_set(mag_stage) then mag_stage=get_logenv('DIR_SITE_MOSTAGE')
if not keyword_set(mag_stage) then begin
   message,/info,"Magnetic Staging Area ($DIR_SITE_MOSTAGE) Undefined"
   return
endif else begin
   if not file_exist(mag_stage) then begin
      message,/info,"Creating magnetic staging area: " + mag_stage
      spawn,'mkdir -p ' + mag_stage
      if not file_exist(mag_stage) then begin
         message,/info,"Could not create, returning..."
      endif else spawn,'chmod 777 ' + mag_stage
   endif
   message,/info,"Restoring files to: " + mag_stage
endelse
; ----------------------------------------------------
; ----------------------------------------------------
;
; ------------ read log file from mo_check --------------
logdir='$DIR_SITE_MOLOGS'
badlog=concat_dir(logdir,'mochk_bad.' + mon)
if not file_exist(badlog) then begin
   message,/info,"Can't find mo_check file: " + badlog + ', returning..."
   return
endif
baddat=rd_tfile(badlog)

; --------------- identify bad files -------------------
badss=where(strpos(baddat,'/mo') eq 0,bcnt)
if bcnt eq 0 then begin
   message,/info,"No bad files listed in: " + badlog + ", returning..."
   return
endif
badfiles=baddat(badss)


breakit=str2cols(badfiles,'/',/trim)		; break into 4 fields
; 'moN.flareXX'	'yohkoh' 'yy_wwN' 'XXyymmdd.hhmm'

bfiles=reform(breakit(3,*))			; file names 
bweeks=reform(breakit(2,*))			; YY_WWn

yohkoh=reform(breakit(1,*))			; constant = 'yohkoh'
weekpat=strmid(bweeks,0,5)			; YY_WW (to ID tapes)
tapes=weekpat(uniq(weekpat))			; for exabyte ID

tmess=$
  ['All required patch data is on tape: ','Required patch data spans tapes: ']
mess=tmess(n_elements(tapes) gt 1) + arr2str(tapes,' , ')
message,/info,mess

resp=''
drive=strtrim(keyword_set(tapdev),2)

; -------------- tape loop - stage from exabyte to magnetic ----------
if not keyword_set(nostage) then begin 

;  Verify host is expected
   host=get_host()
   hostlist=['isass0','isass2','flare2','flare6','flare15']
   if not is_member((str2arr(host,'.'))(0),hostlist,/ignore_case) then begin
      tbeep
      message,/info,"You need to run magnetic staging phase from a machine'
      message,/info,"with an exabyte drive, returning..."
      return
   endif

   for i=0, n_elements(tapes)-1 do begin
   if confirm or i gt 0 then begin	; has to pause for multiple tapes
      tbeep
      print
      print,"--- Load Tape: " + tapes(i) + " into ISASS0 drive" + drive + " ---"
      print
      read,'<CR> when ready, anthing else to QUIT: ',resp
;  
      if resp ne '' then begin
         message,"Aborting on request..."
         return
      endif
   endif else message,/info,"Looking at " + tapes(i) + " in ISASS0 drive " + drive
;  --------- identify bad files which are on the current tape -----------
   whichf=where(weekpat eq tapes(i),fcnt)
   files=bfiles(whichf)
   message,/info,"Restoring " + strtrim(fcnt,2) + " files from tape# " + tapes(i)
;  ------------- restore the files to staging disk via rdtap  ------------

   rdtap,def_tapd(tapdev),files,dpath=mag_stage
;  compare extracted (staged) size to expected size
   mtcmd,tapdev,/offline,/background		; rewind/offline in background
   message,/info,"Checking integrity of staged files...
   yo_file_check,concat_dir(mag_stage,files),badmagfiles,allgood=agood, status=status
   if agood then message,/info,"All requested files staged succesfully..." $
      else begin
          tbeep
          message,/info,"The following staged files are missing or have unexpected sizes..."
          prstr,badmagfiles
          print
          message,/info,"Aborting mo_patch - verify tape drive, rdtap, ..."
          badstage=$
	     concat_dir('$DIR_SITE_MOPATCH','mopatch_' + mon + '.badstage')
          file_append, / new, badstage, $
	    ['mo_patch for MO# ' + mon + ' File extraction/staging problem','']
          outarr= files + string(get_afile_size(files),format='(i12)') + $
		string(file_stat(badmagfiles,/size),format='(i12)')
          file_append, badstage, $
	     ['Filename     Expected-Size  Staged-Size',outarr]
          message,/info,"Staging errors written to: " + badstage
          mail,rd_tfile(badstage),subj="mo_pach MO# " + mon + $
	     " Stageing Problem", user="software@isass0.solar.isas.ac.jp"
          return
      endelse   
   endfor
;  all tapes processed
   message,/info,"All files have been staged to " + mag_stage
endif
; -------------------- end of staging segment ---------------------------
;
if keyword_set(monode) then begin
   message,/info,"Proceding with MO patch.."
;  ----- start an idl job file (will spawn remote job to monode) -----------
   set_logenv,'DIR_SITE_MOPATCH','/ys/site/soft/atest/mo'
   mop=concat_dir('$DIR_SITE_MOPATCH','mopatch_' + mon + '.pro')
   mol=str_replace(mop,'.pro','.log')
   file_delete,[mop,mol]

   file_append,/new,mop, $
      ['; mo_patch for MO# ' + mon + ' run at: ' + !stime + ' by ' + get_user(),'']

;  ------ first command in idl job is a mo mount -------------
   modev=strtrim(keyword_set(modev),2)
   modrive='/mo' + modev + '.' + monode
   file_append,mop,"momount,'" + monode + "',drive=" + modev + ",/write"
;  ---------------------------------------------------------------  
;  ----------- generate source and destination file names... ------
;  infile names are the staged files
   infiles=concat_dir(mag_stage,bfiles)
   outfiles= $
      concat_dir(modrive,(concat_dir(yohkoh,(concat_dir(bweeks,bfiles)))))
;  ------------------------------------------------------------------
;  ------------ append mo verification call to idl job file ---------------
   file_append,mop,['','; now run verification program', $
	"mo_check,'" + mon + "',summary=summpre, badtot=badpre, drive=" + modev]
;  -----------------------------------------------------------------------

;  ---- append directory creation and protection commands to IDL job file --
   udirs=concat_dir(modrive,concat_dir('yohkoh',(bweeks(uniq(bweeks)))))
   for i=0,n_elements(udirs)-1 do begin
      dircmd="rsh,'mkdir -p " + udirs(i) + "'"
      modcmd="rsh,'chmod 777 " + udirs(i) + "'"
      file_append,mop,["","; create subdirectories and set protection", $
	dircmd,modcmd,""]
   endfor
   
;  ------------- append copy commands to idl job file -------------------
   cpcmds="rsh,'" + monode + "','cp -p " + infiles + " " + outfiles + "',user='mocreate',/echo"
   file_append,mop,["","; now issue copy commands...", cpcmds,""]
;
;  ------------ append mo verification call to idl job file ---------------
   file_append,mop,['','; now run verification program', $
	"mo_check,'" + mon + "',summary=summpost,badtot=badpost, drive=" + modev]
;  
;  build final mail message
   file_append,mop,'stats=["Status: OK","Status: PROBLEM"]'
   file_append,mop,'subj="mo_patch MO# ' + mon + ' " + stats(badpost gt 0)'
   file_append,mop,['','; send summary mail', 				$
	'mess=["mo_patch status","", $	',				$
		'"-------- mo_check before patch ----","", summpre,"", $',		$
		'"-------- mo_check after  patch ----","", summpost]']
   file_append,mop,'mail,mess,subj=subj,user="' + get_user() + '@' + get_host() + '"'
;   file_append,mop,'mail,mess,subj=subj,user="software@isass0.solar.isas.ac.jp"'
;  
;  remote job will recurse if better but not perfect (cp timeout problem?)
;   file_append,mop,"   momount,'" + monode + "',mag_stage='" + mag_stage + "',drive=" + modev + ",/eject"
   recurse= $
      "if (badpost lt badpre and (badpost ne 0)) then mo_patch,/noconfirm,/nostage,'" + mon + "','" + monode + "',modev=" + modev + ",mag_stage='" + mag_stage + "'" + " else $"

   file_append,mop,["","; recurse if improved but still problem...",recurse]
   file_append,mop,"   momount,'" + monode + "',drive=" + modev + ",/eject"
   file_append,mop,"end"
;  --- end of remote program -----

;  ----------------------------------------------------------------------
   if confirm then begin
      tbeep
      print
      print,"---- Load (WRITE ENABLE!) Mo# " + mon + " into " + monode + " drive " + modev
      print
      read,'<CR> when ready, anthing else to QUIT: ',resp
      if resp ne '' then begin
         message,"Aborting on request..."
         return
      endif
   endif
;  ---------- now spawn the remote patch job on the monode ------------
   message,/info,"IDL routine generated: " + mop
   if not keyword_set(nospawn) then begin
      message,/info,"Spawning remote copy job on: " + monode 
      message,/info,"Log file will be written to: " + mol 
      rsh,monode,"nohup /ys/gen/script/idl_batch " + $
	   str_replace(mop,'.pro','') + ' ' + mol + " &"
   endif else message,/info,"/NOSPAWN set, remote job not spawned..."
;  --------------------------------------------------------------------------
endif

return
end




