;+
; NAME:
;	UPDATE_FROM_PATH
; PURPOSE:
;	Updates the YS: directories on Yohkoh from ISASS0:: using
;	!path as the pointer as to which directories are to be
;	updated.
; CALLING SEQUENCE:  
;	.run UPDATE_FROM_PATH
; INPUTS:
;	Start time for the "update" in the standard DEC, VMS time format
;	The log file may be inhibited is required.
;	The execution of the copy must explicitly requested - by default 
;	it is inhibited.
; OUTPUTS:
;
; RESTRICTIONS:
;	Designed to work under VMS, updating from ISASS0
;	You must have write access to all the directories on the path
;	A new branch of the tree on ISASS0 will not be seen.
; PROCEDURE:
;	Uses !path to define the directories to be updated. This is
;	into a string array using the "," as seperators.
;	The equivalent directory of each segment of the path is checked
;	for any changes found since the entered "update" date.
;	Any change are copied to the equivalent directory.
;	By default, all changes are logged to the file UPDATE_LOG.LIS
;	Currently updates from the GEN, SXT, HXT and WBS trees.
; MODIFICATION HISTORY:
;	RDB	12-March-92
;	RDB	19-March-91,	included HXT and WBS
;	RDB	03-June-92,	check correct path by number of elements
;	RDB	16-June-92,	log files copied to "control" file
;				input date made more bullet proof
;-
path = !path

;	form the path into a string array
bpath = byte(path)
comma = (byte(','))(0)
loc = where(bpath eq comma, n_seg)
if (n_seg eq 0) then begin
  path_arr = path
endif else begin
  path_arr = strarr(n_seg + 1)
  last = 0
  for i = 0, n_seg-1 do begin
    path_arr(i) = string(bpath(last:loc(i)-1))
    last = loc(i)+1
  endfor
  path_arr(n_seg) = string(bpath(last:*))
endelse

help,path_arr
if n_elements(path_arr) lt 50 then begin
   print,'***********************************************************'
   print,'***       Path is very short for a Yohkoh update        ***'
   print,'*** Have you defined SITE_PATH.PRO without YLIB option? ***'
   print,'***********************************************************'
   yesnox,'Continue',ans
   if not ans then stop
endif

;	define some characters
leftb  = byte(byte('['),0)	;& print,leftb
rightb = byte(byte(']'),0)	;& print,rightb
dot    = byte(byte('.'),0)	;& print,dot
slash  = byte(byte('/'),0)	;& print,slash

host = get_host()
cd,current=home
print,' '
print,'** You are currently in directory:  ',home

print,' '
cdate = systime()
print,'** The current time is:  ',cdate
print,'** Last TAR save sets were created on ISASS0:: on :'
spawn,'dir isass0::"/ys/swmaint/tar/*.tar"/date/wid=(fil=45)'

print,' '
;update = '10-mar-1992'
update=''
Read,'* Enter date from which update should be made: ',update
tarr = timstr2ex(update)	;transform to get into dd-mmm-yy
dd = fmt_tim(tarr)		;before adding required 19
update = strmid(dd,0,7) + '19' + strmid(dd,7,12)
update = '"' + update + '"'

;;if n_elements(log) eq 0 then log = -1
log = -1
ansl = ''
read,'* Log copy instructions to a file? [Def=Y]: ',ansl
ansl = strupcase(strmid(ansl,0,1))
if ansl ne 'N' then ansl = 'Y'
ansc = ''
read,'* Execute spawn of copy instructions [Def=N]: ',ansc
ansc = strupcase(strmid(ansc,0,1))
if ansc ne 'Y' then ansc = 'N'

if ansl eq 'Y' then begin
  log_file = 'update_log.lis'
  print,'** Logging to file: ',log_file
  openw,log,/get_lun,log_file
  printf,log,'** Log of files copied from ISASS0:: at ',cdate
  printf,log,'** Using Update time of ',update
  printf,log,'** Home directory was: ',host + '::' + home
  if ansc ne 'Y' then printf,log,'*** Copying is inhibited ***
  printf,log,' '
  endif

if ansc eq 'Y' then openw,ylog,'yohlib_update_com.dat',/get_lun

jjcopy = 0
;;;n_seg=20
for j = 0,n_seg do begin

;	work through the path, segment by segment
  msg0 = string(j,n_seg,path_arr(j),format='(i5,1h/,i3,6x,a)')
  msg1 = 'Checking segment:  ' + msg0
  print,' '
  print,' '
  print,msg1
;  if log gt 0 then begin
;     printf,log,' '
;     printf,log,msg1
;  endif
;	only update SXT and GEN stuff
  if strpos(path_arr(j),'[SXT.') ge 0 then goto,update
  if strpos(path_arr(j),'[GEN.') ge 0 then goto,update
  if strpos(path_arr(j),'[HXT.') ge 0 then goto,update
  if strpos(path_arr(j),'[WBS.') ge 0 then goto,update
  jcopy = -1
;;  msg = '** Do not update **'
;;  print,msg
;;  if log gt 0 then printf,log,msg
  goto, try_next

update:
;	select next directory
;;  cd,path_arr(j)
;;  spawn,'sho def'

;	form command to see if any updates on ISASS0:
  bseg = byte(path_arr(j))
  qq = where(bseg eq dot)
  bseg(qq) = slash
  qql = where(bseg eq leftb) & qql = qql(0)
  qqr = where(bseg eq rightb) & qqr = qqr(0)
  xseg = string(bseg)
  xdir = 'isass0::"/ys/' + strmid(xseg,qql+1,qqr-qql-1) + '/*.pro"'
  cmd = 'dir ' + xdir + '/out=' + home + 'llx.ll/wid=(fil=75)/since=' + update
  cmd = strlowcase(cmd)
  print,cmd
;	spawn command, and keep files tidy
  spawn,cmd,reply
;  spawn,'type ' + home + 'llx.ll'
  spawn,'purge ' + home + 'llx.ll'
  print,reply

  jcopy = 0
  if strmid(reply(0),0,17) eq "%DIRECT-W-NOFILES" then begin
;;     print,'*** NO new files ***'
     goto, try_next
  endif

  if log gt 0 then begin
;     printf,log,' '
     printf,log,msg1
  endif

;	check file to see if any updates
  str = ''
  openr,lun,/get_lun,home + 'llx.ll'
  while not eof(lun) do begin
     readf,lun,str 
     str = strtrim(str,1)		;removing leading blanks
     fflen = max([50,strlen(strtrim(str,0))]) & fflen = fflen(0)
     str = strmid(str,0,fflen)
     if strpos(str,'/') gt 0 then begin
;        print,str
        qq = where(byte(str) eq slash)
        dflen = strlen(strtrim(str,0))
        destid = strmid(str,max(qq)+1,dflen-max(qq)-2)	;remove end " ???
        cpx = 'copy isass0::' + str + '    ' + path_arr(j) + destid
        print,'>> ',cpx
        if log gt 0 then printf,log,'>> ',cpx,format='(a,a)'
	if ansc eq 'Y' then spawn,cpx
	if ansc eq 'Y' then printf,ylog,path_arr(j)+destid
        jcopy = jcopy + 1
        jjcopy = jjcopy + 1
     endif
  endwhile
  free_lun,lun


try_next:
  case jcopy of
  -1:	msg = '** Do not update **' + msg0
  0:	msg = '** No New Files  **' + msg0
  else:	msg = string('>> Number of files copied =',jcopy,format='(a,i4)')
  endcase
  print,msg
  if log gt 0 then begin
     printf,log,msg
     if jcopy gt 0 then printf,log,' '
  endif
  
endfor 

msg = string('>> Total number of files copied is: ',jjcopy,format='(a,i5)')
print,' '
print,msg
if log gt 0 then begin
  printf,log,' '
  printf,log,msg
  if ansc ne 'Y' then printf,log,'*** Copying is inhibited ***
  free_lun,log
endif

if ansc eq 'Y' then free_lun,ylog

;;print,'** Returning to start directory'
;;cd,home
;;spawn,'sho def'

print,' '
print,' '
print,'*** Possible changes by Mons to BCS stuff?? ***'
spawn,'dir isass0::"/ys/bcs/soft/*/*/*"/date/wid=(fil=55)/since=' +update
spawn,'dir isass0::"/ys/bcs/soft/*/*/*/*"/date/wid=(fil=55)/since=' +update

print,'**************************************************************************
print,'** Any changes should be copied to the [SOLAR_A.BCS.SOFT..] directories **
print,'**************************************************************************

end
