-- $Id: 100_set_root_password.lua,v 1.2 2005/01/12 05:58:36 cpressey Exp $

require "gettext"

local set_root_password = function()
	local done = false
	local result
	local cmds

	local datasets_list = {
		{ root_passwd_1 = "", root_passwd_2 = "" }
	}

	while not done do
		result = App.c:present({
		    id = "root_passwd",
		    name = _("Set Root Password"),
		    short_desc = _(
			"Here you can set the super-user (root) password."
		    ),
	
		    fields = {
			{
			    id = "root_passwd_1",
			    name = _("Root Password"),
			    short_desc = _("Enter the root password you would like to use"),
			    obscured = "true"
			},
			{
			    id = "root_passwd_2",
			    name = _("Re-type Root Password"),
			    short_desc = _("Enter the same password again to confirm"),
			    obscured = "true"
			}
		    },
		    
		    actions = {
			{
			    id = "ok",
			    name = _("Accept and Set Password")
			},
			{
			    id = "cancel",
			    name = _("Return to Configure Menu")
			}
		    },

		    datasets = datasets_list
		})

		if result.action_id == "ok" then
			datasets_list = result.datasets

			--
			-- Fetch form field values.
			--

			local root_passwd_1 = datasets_list[1].root_passwd_1
			local root_passwd_2 = datasets_list[1].root_passwd_2
			
			--[[
			if (!assert_clean(a->c, _("Root password"), root_passwd_1, PW_NOT_ALLOWED)) {
				done = 0;
			]]--
			
			if root_passwd_1 == root_passwd_2 then
				--
				-- Passwords match, so set the root password.
				--
				cmds = CmdChain.new()
				App.state.target:cmds_set_password(cmds,
				    "root", root_passwd_1)
				if cmds:execute() then
					Dialog.inform(App.c,
					    _("The root password has been changed.")
					)
					done = true
				else
					Dialog.inform(App.c,
					    _("An error occurred when " ..
					      "setting the root password.")
					)
					done = false
				end
			else
				--
				-- Passwords don't match - tell the user, let them try again.
				--
				Dialog.inform(App.c,
				    _("The passwords do not match.")
				)
				done = false
			end
		else
			-- Cancelled
			done = true
		end
	end
end

return {
    name = _("Set Root Password"),
    effect = function()
	set_root_password()
	return Menu.CONTINUE
    end
}
