-- $Id: 200_add_user.lua,v 1.2 2005/01/27 23:00:52 cpressey Exp $

require "gettext"

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

	local form = {
	    id = "add_user",
	    name = _("Add user"),
	    short_desc = _("Here you can add a user to an installed system.\n\n" ..
		"You can leave the Home Directory, User ID, and Login Group "	..
		"fields empty if you want these items to be automatically "	..
		"allocated by the system."),
	    fields = {
		{
		    id = "username",
		    name = _("Username"),
		    short_desc = _("Enter the username the user will log in as")
		},
		{
		    id = "gecos",
		    name = _("Real Name"),
		    short_desc = _("Enter the real name (or GECOS field) of this user")
		},
		{
		    id = "passwd_1",
		    name = _("Password"),
		    short_desc = _("Enter the user's password (will not be displayed)"),
		    obscured = "true"
		},
		{
		    id = "passwd_2",
		    name = _("Password (Again)"),
		    short_desc = _("Re-enter the user's password to confirm"),
		    obscured = "true"
		},
		{
		    id = "shell",
		    name = _("Shell"),
		    short_desc = _("Enter the full path to the user's shell program")
		},
		{
		    id = "home",
		    name = _("Home Directory"),
		    short_desc = _("Enter the full path to the user's home directory, or leave blank")
		},
		{
		    id = "uid",
		    name = _("User ID"),
		    short_desc = _("Enter this account's numeric user id, or leave blank")
		},
		{
		    id = "group",
		    name = _("Login Group"),
		    short_desc = _("Enter the primary group for this account, or leave blank")
		},
		{
		    id = "groups",
		    name = _("Other Group Memberships"),
		    short_desc = _(
		        "Enter a comma-separated list of other groups "	..
			"that this user should belong to"
		    )
		}
	    },
	    actions = {
		{
		    id = "ok",
		    name = _("Accept and Add User")
		},
		{
		    id = "cancel",
		    name = _("Return to Configure Menu")
		}
	    },
	    datasets = {
		{
		    username = "",
		    gecos = "",
		    passwd_1 = "",
		    passwd_2 = "",
		    shell = "/bin/tcsh",
		    home = "",
		    uid = "",
		    group = "",
		    groups = ""
		}
	    }
	}

	while not done do
		result = App.c:present(form)
		if result.action_id == "ok" then
			form.datasets = result.datasets

			--
			-- Fetch form field values.
			--
			local username	= result.datasets[1].username
			local gecos	= result.datasets[1].gecos
			local passwd_1	= result.datasets[1].passwd_1
			local passwd_2	= result.datasets[1].passwd_2
			local shell	= result.datasets[1].shell
			local home	= result.datasets[1].home
			local uid	= result.datasets[1].uid
			local group	= result.datasets[1].group
			local groups	= result.datasets[1].groups

			--
			-- Valid field values.
			--

			if string.len(username) == 0 then
				Dialog.inform(App.c, _(
				    "You must enter a username."
				))
			elseif passwd_1 ~= passwd_2 then
				Dialog.inform(App.c, _(
				    "The passwords do not match."
				))
			--[[
			elseif (!assert_clean(a->c, _("Username"), username, PW_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Real Name"), gecos, GECOS_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Password"), passwd_1, PW_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Shell"), shell, FILENAME_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Home Directory"), home, FILENAME_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("User ID"), uid, PW_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Login Group"), group, PW_NOT_ALLOWED) ||
		    !assert_clean(a->c, _("Group Memberships"), groups, MEMBERSHIPS_NOT_ALLOWED)) {
			done = 0;
			]]--
			elseif not FileSystem.is_program(App.dir.root .. shell) and
			       shell ~= "/nonexistent" then
				Dialog.inform(App.c, _(
				    "The selected shell does not exist on the system."
				))
			else
				local cmds = CmdChain.new()

				App.state.target:cmds_add_user(cmds, {
				    username = username,
				    gecos = gecos,
				    shell = shell,
				    uid = uid,
				    group = group,
				    home = home,
				    groups = groups,
				    password = passwd_1
				})
				if cmds:execute() then
					Dialog.inform(App.c, _(
					    "User `%s' was added.",
					    username
					))
					done = true
				else
					Dialog.inform(App.c, _(
					    "User was not successfully added."
					))
				end
			end
		else
			-- Cancelled.
			done = true
		end
	end
end

return {
    name = _("Add User"),
    effect = function()
	add_user()
	return Menu.CONTINUE
    end
}
