pro ydb_install, startt,  stopt, ydbsets=ydbsets, noexecute=noexecute, $
    default=default
;
; start time = null - copy all files since last local one
; stop time  = null - copy through today

case n_params() of
   0: begin
        startt=''
        read,'Enter start time: ',startt
        stopt=''
        read,'Enter stop time: ',stopt
        if stopt eq '' then stopt = systime()
   endcase
   1: stopt=timegrid(startt,/day)
   else:
endcase
    

strt=fmt_tim(startt)
stpt=fmt_tim(stopt)

;   if pubydb_config not defined, use default one (==isass0)
config = getenv('pubydb_config')
if strlen(config) eq 0 then $
   config=concat_dir('$DIR_GEN_SETUP','pubydb.config')

print,'** Using config file: ',config
if not file_exist(config) then begin
   tbeep
   message,/info,'Need file: ' + config + ', please upgrade and retry...'
   return
endif else ydbdata=rd_tfile(config,delim=';',/nocomment,/auto)

nsets=n_elements(ydbdata(0,*))

if keyword_set(default) then begin
   ydbsets = reform(ydbdata(1,where(reform(ydbdata(3,*)) eq 'yes')))
endif

if n_elements(ydbsets) eq 0 then begin
   chkenv=str2arr(get_logenv('yssw_db_sets'),' ')
   if chkenv(0) eq '' then begin
      message,/info,'Select data base types from menu...'
      ss=wmenu_sel(strupcase(ydbdata(1,*)) + ' : ' + ydbdata(4,*))
      if ss(0) eq -1 then begin
         message,/info,'Nothing selected, quitting...
     return
      endif
      chkenv=ydbdata(1,ss)
   endif
   ydbsets=chkenv
endif 
allsets=reform(ydbdata(1,*))
which=where_arr(allsets,ydbsets,sscnt)

if sscnt eq 0 then begin
   message,/info,"Could not determine ydb sets selected"
   return
endif

ydbdata=ydbdata(*,which)
ydbenv=reform(ydbdata(0,*))
ydbsets=reform(ydbdata(1,*))
remote=reform(ydbdata(2,*))
remss=where(remote eq '',rsscnt)
if rsscnt gt 0 then remote(remss)='/ydb/' + ydbsets(remss)

locals=''
nsets=n_elements(ydbsets)

for sets=0,sscnt-1  do begin
   local=get_logenv(ydbenv(sets),/case_ignor)
   locals=[locals,local]
endfor   
locals=locals(1:*)
   
if strt eq '' then message,'need start time...'

anytim2weeks,strt, stpt,years,weeks
weekstr=string(weeks,format='(i2.2)') + '_' + $
    string(years,format='(i2.2)')

syears=string(years,format='(i2.2)')
if n_elements(syears) gt 1 then syears=syears(uniq(syears))

ydbget=''
;               build ftp file
login_info = 'user ftp ' + get_user() + "@" + get_host()
swhost = get_logenv('yssw_sw_host')
if swhost eq '' then begin
  swhost = 'isass0.solar.isas.ac.jp'
  login_info = 'user guest ##Machida'
endif


print,'** Checking host is alive: ',swhost
if not is_alive(swhost) then begin
   tbeep
   message,/info,"FTP HOST: " + swhost + " is not responding, try again later.."
   return
endif

ftpfile=concat_dir(getenv('HOME'),'ydbget.ftp')
print,'** Writing FTP script: ',ftpfile
file_append,ftpfile,/new, [             $
   'open ' +    swhost,                 $
   login_info,                                            $
;   'user ftp ' + get_user() + "@" + get_host(),            $   
;   'user guest kobuchi',               $ ;*** temporary
   'binary'                     ]

for set=0,nsets-1 do begin
   ydbfiles=ydbsets(set)+weekstr+ '*.*'
;  *** kludge for special cases ***
   if ydbsets(set) eq 'sfc' then ydbfiles=ydbsets(set) + '*.*'
   file_append,ftpfile, [               $
        'lcd ' + locals(set),               $
;   'cd ' + '/pub/ydb/' + remote(set),      $
    'cd ' +   remote(set),              $ ;*** temporary
    'mget ' + ydbfiles              ]   
endfor

file_append,ftpfile,'bye'

if not keyword_set(noexecute) then begin
   for dir=0,n_elements(locals)-1 do begin
      if not file_exist(locals(dir)) then begin
         message,/info,'Creating directory: ' + locals(dir)
         spawn,'mkdir -p ' + locals(dir),reply
         if not file_exist(locals(dir)) then begin
            tbeep
            message,/info,'COULD NOT MAKE DIRECTORY - CHECK PROTECTION ON YDB'
            message,/info,'Returning.... (no ftps done)
            return
         endif
      endif
   endfor
   spawn,'ftp -in < ' + ftpfile, ftpres
   if ftpres(0) ne '' then prstr,['From ftp...',ftpres]
endif

return
end