005-set-site-config 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 util = require 'luci.util'
  7. function add_var_to_table(table, var)
  8. if type(var) == "table" and type(table) == "table" then
  9. for name, value in pairs(var) do
  10. if table[name] and type(value) == "table" then
  11. table[name] = add_var_to_table(table[name], value)
  12. else
  13. table[name]=value
  14. end
  15. end
  16. end
  17. return table
  18. end
  19. local default = tools.get_config('/lib/gluon/site-select/default.json')
  20. local groups = tools.get_config('/lib/gluon/site-select/groups.json')
  21. local sites = tools.get_config('/lib/gluon/site-select/sites.json')
  22. local currentsite = uci:get("currentsite", "current", "name")
  23. if site_code ~= currentsite then
  24. local configured = false
  25. for _, site in pairs(sites) do
  26. if site.site_code == currentsite then
  27. if site.site_select and site.site_select.group and groups and groups[site.site_select.group] then
  28. default = add_var_to_table(default, groups[site.site_select.group])
  29. end
  30. default = add_var_to_table(default, site)
  31. local file = '/lib/gluon/site.conf'
  32. local f = io.open(file, "w")
  33. f:write(util.serialize_data(default))
  34. f:close()
  35. configured = true
  36. break
  37. end
  38. end
  39. if configured == false then
  40. tools.force_site_code(site_code)
  41. end
  42. end