005-set-site-config 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/lua
  2. local uci = require('simple-uci').cursor()
  3. local site_code = require('gluon.site').site_code()
  4. local tools = require 'gluon.site_generate'
  5. local groups = tools.get_config('/lib/gluon/site-select/groups.json')
  6. local sites = tools.get_config('/lib/gluon/site-select/sites.json')
  7. local currentsite = uci:get('currentsite', 'current', 'name')
  8. if site_code ~= currentsite then
  9. local configured = false
  10. for _, site in pairs(sites) do
  11. if site.site_code == currentsite then
  12. local config = io.open('/lib/gluon/site-select/template.json'):read('*a')
  13. local subst = {}
  14. subst['%%SN'] = site.site_name
  15. subst['%%SC'] = site.site_code
  16. if site.subst then
  17. config = tools.replace_patterns(config, site.subst)
  18. end
  19. if (site.site_select or {}).group and groups and groups[site.site_select.group] then
  20. group = groups[site.site_select.group]
  21. subst['%%GN'] = site.site_select.group
  22. if group.subst then
  23. config = tools.replace_patterns(config, group.subst)
  24. end
  25. end
  26. config = tools.replace_patterns(config, subst)
  27. local file = '/lib/gluon/site.json'
  28. local f = io.open(file, 'w')
  29. f:write(config)
  30. f:close()
  31. configured = true
  32. break
  33. end
  34. end
  35. if configured == false then
  36. tools.set_site_code(site_code, true)
  37. end
  38. end