
pro ssw_swpc_cat_makein, sswdb=sswdb, site=site, home=home, image_in_root=image_in_root, _extra=_extra, $
   outstruct=outstruct, no_write=no_write, clobber=clobber
;
;+
;   Name: ssw_swpc_cat_makin
;
;   Purpose: generate the 'swpc_cat.in' config file required by swpc_cat suite; optionally write to $HOME or $SSW/site/setup
;
;   Input Parameters:
;      NONE:
;
;   Keyword Paramters:
;      sswdb - (switch) - if set, use local $SSWDB/packages/swpc_cat/data/ for parent data pointers
;      site - (switch) - if set, write swpc_cat.in to $SSW/site/setup/  (only owner of local $SSW)
;      home - (switch) - if set, write swpc_cat.in to $HOME or $USERPROFILE (default)
;      image_in_root - parent of local swpc_cat data tree (explicit user alternate)
;      _extra - optionally, user explicit keyword=value pairs to overrid template 
;      no_write - (switch) - if set, don't write swpc_config.in ( OUTSTRUCT is still defined)
;      outstruct (output)  - structure version of derived swpc_cat.in contents
;
;   Method:
;      take defaults from ssw supplied template - then add user/local options and write config file
;
;      Individual users should use /HOME
;      $SSW site managers may use /SITE (for output -> $SSW/ste/setup/ setting local default for all users)
;-

template=concat_dir(concat_dir('$SSW_SWPC_CAT','setup'),'swpc_cat_template.in')
if not file_exist(template) then  begin 
    box_message,'SSW SWPC_CAT supplied template swpc_cat.in not found (really cannot happen in valid ssw installation..
    return
endif

ctn=swpc_cat_in2struct(template) ; string->structure

ctnames=tag_names(ctn)

case 1 of 
   keyword_set(image_in_root): ctn.image_in_root=image_in_root ; User supplied
   keyword_set(sswdb): ctn.image_in_root=str_replace(ctn.image_in_root,'$SSWDB',get_logenv('$SSWDB')) ; local $SSWDB
   else: box_message,'Note: no IMAGE_IN_ROOT or /SSWDB - you may need to edit that parameter'
endcase

home=([get_logenv('$HOME'),get_logenv('$USERPROFILE')])(os_family() eq 'Windows')

ctn.export_location_root=str_replace(ctn.export_location_root,'$HOME',home)
ctn.preferences_folder=str_replace(ctn.preferences_folder,'$HOME',home)

if n_tags(_extra) gt 0 then begin 
   etags=tag_names(_extra)
   for t=0,n_tags(_extra)-1 do begin 
      ti=tag_index(ctnames,etags[t])
      if ti ne -1 then ctn.(ti)=_extra.(t)
   endfor
endif
pv=''
for p=0,n_tags(ctn)-1 do pv=[pv,strlowcase(ctnames[p])+'='+strtrim(ctn.(p),2)]
pv=pv[1:*]

outstruct=ctn ; output paramter

outdir=home ; default is $HOME/$USERPREFERENCE
if not keyword_set(no_write) then begin ; default writes a file
   case 1 of
      keyword_set('$HOME'): ; default
      keyword_set(site): begin 
         outdir=get_logenv('SSW_SITE_SETUP')
         if 1-write_dir(outdir) then begin 
            box_message,'You set /SITE but you do not have write permission to $SSW_SITE_SETUP; ask owner of $SSW'
            outdir=home
         endif
      endcase
      keyword_set(current): outdir=curdir()
      else:
   endcase
   outfile=concat_dir(outdir,'swpc_cat.in') ; swpc suite default name
   clobber=keyword_set(clobber) or 1-file_exist(outfile)
   if clobber then file_append,outfile[0],pv,/new else $
      if file_exist(outfile) then box_message,outfile[0] + ' exists - set /CLOBBER to allow overwrite
endif

return
end 










