/**
 *
 * File:
 *   details.ycp
 *
 * Module:
 *   Network/YPServer
 *
 * Summary:
 *   YPServer module.
 *
 * Authors:
 *   Dan Vesely <dan@suse.cz>
 *
 * $Id: details.ycp 20039 2004-10-29 07:34:42Z jsuchome $
 *
 * YPServer module part.
 *
 */


{

    textdomain "nis_server";

    import "Label";
    import "Message";
    import "Mode";
    import "NisServer";
    import "Popup";
    import "Wizard";

    include "nis_server/routines.ycp";

    /**
     * Details dialog
     * @return `back, `abort or `next
     */
    define symbol DetailsDialog () ``{
	string srcdir = NisServer::pwd_srcdir;

	// help text 1/3
	string helptext = _("<p>You can change NIS server source directory (usually
<i>'/etc'</i>).</p>");

	// help text 2/3
	helptext = helptext + _("<p>Select if your <i>passwd</i> file should be merged with the <i>shadow</i>
file and if the <i>group</i> file should be merged with the <i>gshadow</i>
file
(only possible if the <i>shadow</i> or <i>gshadow</i> files exist).</p>
");

	// help text 3/3
	helptext = helptext + _("<p>You can also adjust the minimum user and group id.</p>");

	boolean merge_passwd = NisServer::merge_passwd;
	boolean merge_group = NisServer::merge_group;

	term merge_pass_buttons = `VBox ();
	merge_pass_buttons = add (merge_pass_buttons, `VSpacing (0.1));
				// To translators: button label
	merge_pass_buttons = add (merge_pass_buttons, `Left (`RadioButton (`id(`no), _("No"), !merge_passwd)));
	merge_pass_buttons = add (merge_pass_buttons, `VSpacing (0.3));
				// To translators: button label
	merge_pass_buttons = add (merge_pass_buttons, `Left (`RadioButton (`id(`yes), _("Yes"), merge_passwd)));
	merge_pass_buttons = add (merge_pass_buttons, `VSpacing (0.1));
	merge_pass_buttons = `RadioButtonGroup (`id (`pass_rb), merge_pass_buttons);

	term merge_group_buttons = `VBox ();
	merge_group_buttons = add (merge_group_buttons, `VSpacing (0.1));
				// To translators: button label
	merge_group_buttons = add (merge_group_buttons, `Left (`RadioButton (`id(`no), _("No"), !merge_group)));
	merge_group_buttons = add (merge_group_buttons, `VSpacing (0.3));
				// To translators: button label
	merge_group_buttons = add (merge_group_buttons, `Left (`RadioButton (`id(`yes), _("Yes"), merge_group)));
	merge_group_buttons = add (merge_group_buttons, `VSpacing (0.1));
	merge_group_buttons = `RadioButtonGroup (`id (`group_rb), merge_group_buttons);


	term merge_all  = `HBox ();
				// To translators: frame label
	merge_all = add (merge_all, `HWeight (1, `Frame (`id (`pass_frame), _("Merge pa&sswords"), merge_pass_buttons)));
	merge_all = add (merge_all, `HSpacing ());
				// To translators: frame label
	merge_all = add (merge_all, `HWeight (1, `Frame (`id (`group_frame), _("Merge g&roups"), merge_group_buttons)));

	term minimals = `HBox ();

				// To translators: intfield label
	minimals = add (minimals, `IntField (`id (`minuid), _("Minimum &UID"), 0, 50000,
					     NisServer::minuid));
	minimals = add (minimals, `HSpacing ());
				// To translators: intfield label
	minimals = add (minimals, `IntField (`id (`mingid), _("Minimum &GID"), 0, 50000,
					     NisServer::mingid));


	term contents = `VBox ();
				// To translators: textentry label
	contents = add (contents, `TextEntry (`id (`srcdir), `opt(`notify), _("&YP Source directory"),
					      srcdir));
	contents = add (contents, `VSpacing (0.5));
	contents = add (contents, merge_all);
	contents = add (contents, `VSpacing (0.5));
	contents = add (contents, minimals);


	contents = `HVSquash (contents);

				// To translators: dialog label
	Wizard::SetContents (_("NIS Master Server Details Setup"), contents, helptext, true, true);
	Wizard::SetNextButton(`next, Label::OKButton() );

	// If the source directory does not exist, it will be created
	// with empty passwd, group, shadow, gshadow.
	// If it already exists, check whether shadow, gshadow exist
	// and disable some options accordingly.
	// (#15624)
	boolean srcdir_exists = nil;
	boolean shadow_exists = nil;
	boolean gshadow_exists = nil;
	boolean change_enabled = true;

	symbol ui = `again;

	repeat
	{
	    if (change_enabled)
	    {
		// srcdir now has an up-to-date value
		srcdir_exists = SCR::Read (.target.dir, srcdir) != nil;
		shadow_exists  = !srcdir_exists || SCR::Read (.target.size, sformat ("%1/shadow", srcdir)) != -1;
		gshadow_exists = !srcdir_exists || SCR::Read (.target.size, sformat ("%1/gshadow", srcdir)) != -1;
		UI::ChangeWidget (`id (`pass_frame), `Enabled, shadow_exists);
		UI::ChangeWidget (`id (`group_frame), `Enabled, gshadow_exists);
	    }

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

	    change_enabled = (ui == `srcdir);
	    srcdir = (string) UI::QueryWidget (`id (`srcdir), `Value);
	    if (ui == `abort && !Popup::ReallyAbort (true)) ui = `again;
	    if (ui == `next)
	    {
		if (SCR::Read (.target.dir, srcdir) == nil && !Mode::config ())
		{
		    UI::SetFocus (`id (`srcdir));
		    ui = Popup::YesNo (
			Message::DirectoryDoesNotExistCreate (srcdir))
			? `next : `again;
		}
	    }
	}
	until (contains ([`back, `next, `abort], ui));

	NisServer::minuid = (integer) UI::QueryWidget (`id (`minuid), `Value);
	NisServer::mingid = (integer) UI::QueryWidget (`id (`mingid), `Value);
	NisServer::merge_passwd = UI::QueryWidget (`id (`pass_rb), `CurrentButton) == `yes;
	NisServer::merge_group = UI::QueryWidget (`id (`group_rb), `CurrentButton) == `yes;
	NisServer::pwd_srcdir = srcdir;

	Wizard::RestoreNextButton();
	return ui;
    }
}
