/**
 *
 * File:
 *   maps.ycp
 *
 * Module:
 *   Network/YPServer
 *
 * Summary:
 *   YPServer module.
 *
 * Authors:
 *   Dan Vesely <dan@suse.cz>
 *   Martin Vidner <mvidner@suse.cz>
 *
 * $Id: maps.ycp 20792 2005-01-18 10:20:58Z jsuchome $
 *
 * YPServer module part.
 *
 */


{

    textdomain "nis_server";

    import "Wizard";
    import "Popup";

    import "NisServer";
    include "nis_server/routines.ycp";

    /**
     * Maps dialog
     * @return `back, `abort or `next
     */
    define symbol MapsDialog () ``{

	// help text 1/1
	// Translators: the text was truncated because now there's
	// a MultiSelectionBox instead of two SelectionBoxes that
	// were a pain to use.
	string helptext = _("<p>With this dialog, adjust which maps will be available.</p>");

	// Let's call a yp map a table, not to get too confused.

	// All tables that ypserv Makefile knows about
	list<string> all = NisServer::GetAllMaps ();

	// Assign a source file to all tables.
	// Some tables are built of more files while we only check for one.
	// Still it's better than no checking at all.
	list<string> passwd_tables = ["passwd", "group", "passwd.adjunct", "shadow"];
	string passwd_dir = NisServer::pwd_srcdir;
	map files = listmap (string table, all,
			     {return $[table : sformat ("/etc/%1",table)]; });
	foreach (string table, passwd_tables, ``{
	    files[table] = sformat ("%1/%2", passwd_dir, table);
	});
	// irregularly named tables
	files["netgrp"] = "/etc/netgroup";
	files["mail"] = "/etc/mail/aliases";

	// We want to construct a list of items:
	// `item (`id (table), table, true|false)
	map<string,boolean> enabled = listmap (string table, all, {return $[table : false];});
	// filter out tables that are merged or their sources don't exist
	if (NisServer::merge_passwd)
	{
	    enabled = remove (enabled, "shadow");
	}
	enabled = filter (string table, boolean dummy, enabled, ``(
			      SCR::Read (.target.size, files[table]:"/") >= 0
			      ));
	list<string> current = NisServer::maps;
	foreach (string table, current, ``{
	    enabled[table] = true;
	});
	list items = maplist (string table, boolean e, enabled,
			      ``( `item (`id (table), table, e) ));

	term contents = `MultiSelectionBox (
	    // multilesection box label
	    `id (`current), _("&Maps"), items
	    );

	// To translators: dialog label
	Wizard::SetContents (_("NIS Server Maps Setup"), contents, helptext, true, true);

	symbol ui = nil;

	repeat
	{
	    ui = (symbol) UI::UserInput ();
	    if (ui == `cancel)
	    {
		ui = `abort;
	    }

	    if (ui == `abort && !Popup::ReallyAbort (true)) ui = `again;
	}
	until (contains ([`back, `next, `abort], ui));

	current = (list<string>) UI::QueryWidget (`id (`current), `SelectedItems);
	NisServer::maps = current;
	return ui;
    }
}
