-- $Id: 800_configure_network.lua,v 1.1 2005/01/28 23:40:52 cpressey Exp $

require "gettext"
require "network"

-- XXX Several of the functions in this script should be in
-- XXX scripts/demo/lib/network_ui.lua (or something) instead.

local dhcp_configure = function(ni)
	local cmds = CmdChain.new()

	ni:dhcp_configure(cmds)
	if cmds:execute() then
		--[[
		if App.wait_for(function() return ni:is_up() end, 30, 2) then
			Dialog.inform(App.c, _(
			   "Interface %s is now ``up'', with IP address %s.",
			   ni:get_name(), ni:get_inet_addr()
			))
			return true
		else
			return false
		end
		]]--
		return true
	else
		return false
	end
end

local static_configure = function(ni)
end

return {
    name = "network",
    title = "Configure your Network",
    action = function(fsm)
	local actions, ifname, ni, result

	if App.state.net_configured then
		return fsm:next()
	end

	if not Dialog.confirm(App.c, _(
	    "You have not yet configured your network settings. "	..
	    "Would you like to do so now? (Having an operational "	..
	    "network connection will enhance the ability of "		..
	    "subsequent tasks, such as installing."
	)) then
		return fsm:next()
	end

	App.state.net_if = NetworkInterface.all()

	actions = {}
	for ifname, ni in App.state.net_if do
		table.insert(actions, {
		    id = ifname,
		    name = ifname
		})
	end
	table.insert(actions, {
	    id = "cancel",
	    name = "Cancel"
	})

	ifname = App.c:present({
	    id = "select_interface",
    	    name =  _("Select Network Interface"),
	    short_desc = _(
	        "Please select the network interface you wish to configure."
	    ),
	    role = "menu",
	    actions = actions
	}).action_id

	if ifname == "cancel" then
		return fsm:next()
	end
	ni = App.state.net_if[ifname]

	if Dialog.confirm(App.c, _(
	    "Would you like to try dynamically configuring this interface? " ..
	    "(This requires that you have a DHCP server operating on " ..
	    "the network that this interface is attached to.)"
	)) then
		dhcp_configure(ni)
	else
		static_configure(ni)
	end

	return fsm:next()
    end
}
