005-set-site-config 1.4 KB

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