function fix_zdbase, user=user, cds=cds, soho=soho, eis=eis, $
                     original=original, errmsg=errmsg, initialize=initialize, $
                     resolve=resolve
;
;  common to store original value of ZDBASE.  Also, the variable ZDB_USED
;  contains as a text string the database used.
;
common zdbase_def, zdb_initialised, orig_zdbase, zdb_used
;
;  Initialize MESSAGE and RESULT.  These will be updated later.
;
result = 1
message = ''
if keyword_set(initialize) then $
 delvarx,zdb_initialised, orig_zdbase, zdb_used
;
;  Check that at least one keyword is set.
;
N_KEYWORDS = KEYWORD_SET(CDS) + KEYWORD_SET(USER) + KEYWORD_SET(SOHO) + $
  KEYWORD_SET(ORIGINAL) + KEYWORD_SET(RESOLVE) + KEYWORD_SET(EIS)
IF N_KEYWORDS EQ 0 THEN BEGIN
    MESSAGE = 'Use: At least one of /user,/cds,/soho,/eis,/original,' +$
        '/resolve keywords must be set.'
    GOTO, HANDLE_ERROR
ENDIF
;
;  If the RESOLVE keyword was passed, then expand out the path before storing
;  the original definition.
;
if keyword_set(resolve) then begin
   u = get_environ('ZDBASE',/path)
   if strpos(u,'+') ge 0 then begin
      u = find_all_dir(u,/path,/plus_required)
      command = "def_dirlist,'ZDBASE',u"
      status = execute(command)
      if not status then begin
         message = 'Unable to set variable ZDBASE to user value.'
     GOTO, HANDLE_ERROR
      endif
   endif
   goto, finish
endif
;
;  First use (original value undefined or set to zero)?  
;  If so record value of ZDBASE
;
if not keyword_set(zdb_initialised) then begin
   orig_zdbase = get_environ('ZDBASE',/path)
   if orig_zdbase eq '' then begin
    message = 'Warning: no original definition of ZDBASE.'
    if n_elements(errmsg) eq 0 then begin
        message, message, /informational
        message = ''
    endif
   endif else begin
    zdb_used = 'Original'
    zdb_initialised = 1
   endelse
endif 
;
;  Return to original definition, first check initialised flag is defined
;  and not set to zero
;
if keyword_set(original) then begin
   command = "def_dirlist,'ZDBASE',orig_zdbase"
   status = execute(command)
   if not status then begin
      message = 'Unable to reset ZDBASE to original value.'
      GOTO, HANDLE_ERROR
   endif else begin
      zdb_used = 'Original'
      zdb_initialised = 0
      orig_zdbase = ''
      GOTO, FINISH
   endelse
endif
;
;  The remaining keywords can be used in conjunction with each other.
;
zdbase = ''
zdbase_used = ''
if !version.os eq 'vms' then sep = ',' else sep = ':'
;
;  USER option (allow multiple directories if '+' format used)
;
if keyword_set(user) then begin
   u = get_environ('ZDBASE_USER',/path)
   if u ne '' then begin
      u = find_all_dir(u,/path,/plus_required)
      if zdbase eq '' then zdbase = u else zdbase = zdbase + sep + u
      command = "def_dirlist,'ZDBASE',zdbase"
      status = execute(command)
      if not status then begin
         message = 'Unable to set variable ZDBASE to user value.'
     GOTO, HANDLE_ERROR
      endif else begin
         if zdbase_used eq '' then zdbase_used = 'User' else $
           zdbase_used = zdbase_used + ',' + 'User'
      endelse
   endif else begin
      message = 'Variable ZDBASE_USER is not defined.'
      if n_keywords gt 1 then print, message else GOTO, HANDLE_ERROR
   endelse    
endif

;
;  CDS option (gather all subdirectories if '+' format used
;
if keyword_set(cds) then begin
   u = get_environ('ZDBASE_CDS',/path)
   if u ne '' then begin
      u = find_all_dir(u,/path,/plus_required)
      if zdbase eq '' then zdbase = u else zdbase = zdbase + sep + u
      command = "def_dirlist,'ZDBASE',zdbase"
      status = execute(command)
      if not status then begin
         message = 'Unable to set variable ZDBASE to cds value.'
     GOTO, HANDLE_ERROR
      endif else begin
         if zdbase_used eq '' then zdbase_used = 'CDS' else $
           zdbase_used = zdbase_used + ',' + 'CDS'
      endelse   
   endif else begin
      message = 'Variable ZDBASE_CDS is not defined.'
      if n_keywords gt 1 then print, message else GOTO, HANDLE_ERROR
   endelse    
endif


;
;  SOHO option (gather all subdirectories if '+' format used.
;
if keyword_set(soho) then begin
   u = get_environ('ZDBASE_SOHO',/path)
   if u ne '' then begin
      u = find_all_dir(u,/path,/plus_required)
      if zdbase eq '' then zdbase = u else zdbase = zdbase + sep + u
      command = "def_dirlist,'ZDBASE',zdbase"
      status = execute(command)
      if not status then begin
         message = 'Unable to set variable ZDBASE to soho value.'
     GOTO, HANDLE_ERROR
      endif else begin
         if zdbase_used eq '' then zdbase_used = 'SOHO' else $
           zdbase_used = zdbase_used + ',' + 'SOHO'
      endelse   
   endif else begin
      message = 'Variable ZDBASE_SOHO is not defined.'
      if n_keywords gt 1 then print, message else GOTO, HANDLE_ERROR
   endelse    
endif


;
;  EIS option (gather all subdirectories if '+' format used.
;
if keyword_set(eis) then begin
   u = get_environ('ZDBASE_EIS',/path)
   if u ne '' then begin
      u = find_all_dir(u,/path,/plus_required)
      if zdbase eq '' then zdbase = u else zdbase = zdbase + sep + u
      command = "def_dirlist,'ZDBASE',zdbase"
      status = execute(command)
      if not status then begin
         message = 'Unable to set variable ZDBASE to eis value.'
     GOTO, HANDLE_ERROR
      endif else begin
         if zdbase_used eq '' then zdbase_used = 'EIS' else $
           zdbase_used = zdbase_used + ',' + 'EIS'
      endelse   
   endif else begin
      message = 'Variable ZDBASE_EIS is not defined.'
      if n_keywords gt 1 then print, message else GOTO, HANDLE_ERROR
   endelse    
endif


;
;  If ZDBASE is still undefined, then signal an error.
;
if zdbase eq '' then goto, handle_error
message = ''
GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
    RESULT = 0
;
;  Exit point.
;
FINISH:
    IF MESSAGE NE '' THEN BEGIN
        IF N_ELEMENTS(ERRMSG) GT 0 THEN ERRMSG = 'FIX_ZDBASE: ' + $
            MESSAGE ELSE MESSAGE, MESSAGE, /CONTINUE
    ENDIF
;
        MKLOG,'$ZDBASE','ZDBASE'
    RETURN, RESULT
    END