-- hostname model methods
local mymodule = {}

fs = require("acf.fs")
modelfunctions = require("modelfunctions")

mymodule.get = function (fqdn)
	local n
	if fqdn then
		n = modelfunctions.run_executable({"hostname", "-f"})
	end
	if not n or n == "" then
		n = modelfunctions.run_executable({"hostname"})
	end
	if not n or n == "" then
		n = "unknown"
	end

	return cfe({value=n, label="Hostname"})
end


mymodule.read_name = function ()
	return cfe({ type="group", value={hostname=mymodule.get(false)}, label="Hostname" })
end

mymodule.update_name = function(self, name)
	local success = true

	if success then
		fs.write_file("/etc/hostname", name.value.hostname.value)
		modelfunctions.run_executable({"hostname", "-F", "/etc/hostname"})
	else
		name.errtxt = "Failed to set hostname"
	end

	return name
end

return mymodule
