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