wizard.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local disp = require 'gluon.web.dispatcher'
  2. local fs = require "nixio.fs"
  3. local util = require "gluon.web.util"
  4. local nixio_util = require "nixio.util"
  5. local uci = require("simple-uci").cursor()
  6. local wizard_dir = "/lib/gluon/config-mode/wizard/"
  7. local files = nixio_util.consume(fs.dir(wizard_dir) or function() end)
  8. table.sort(files)
  9. local wizard = {}
  10. for _, entry in ipairs(files) do
  11. if entry:sub(1, 1) ~= '.' then
  12. local f = assert(loadfile(wizard_dir .. entry))
  13. setfenv(f, getfenv())
  14. local w = f()
  15. table.insert(wizard, w)
  16. end
  17. end
  18. local f = Form(translate("Welcome!"))
  19. f.submit = translate('Save & restart')
  20. f.reset = false
  21. local s = f:section(Section)
  22. s.template = "gluon/config-mode/welcome"
  23. local commit = {'gluon-setup-mode'}
  24. for _, w in ipairs(wizard) do
  25. for _, c in ipairs(w(f, uci) or {}) do
  26. if not util.contains(commit, c) then
  27. table.insert(commit, c)
  28. end
  29. end
  30. end
  31. function f:write()
  32. local nixio = require "nixio"
  33. uci:set("gluon-setup-mode", uci:get_first("gluon-setup-mode", "setup_mode"), "configured", true)
  34. for _, c in ipairs(commit) do
  35. uci:commit(c)
  36. end
  37. f.template = "gluon/config-mode/reboot"
  38. f.hidenav = true
  39. if nixio.fork() == 0 then
  40. -- Replace stdout with /dev/null
  41. nixio.dup(nixio.open('/dev/null', 'w'), nixio.stdout)
  42. -- Sleep a little so the browser can fetch everything required to
  43. -- display the reboot page, then reboot the device.
  44. nixio.nanosleep(1)
  45. nixio.execp("reboot")
  46. end
  47. end
  48. return f