/*
 *
 *
 * $Id: support.ycp 20786 2005-01-17 17:39:59Z jsuchome $
 */

{
    textdomain "support";

    integer current_stage = -1;

    import "Wizard";
    import "Popup";

    include "support/supportio.ycp";
    include "support/support_askkey.ycp";
    include "support/support_question.ycp";
    include "support/support_send.ycp";

    /*
     * exit when running for non-root
     */

    map res = (map) SCR::Execute(.target.bash_output, "/usr/bin/whoami", $[]);
    list l_res = splitstring(res["stdout"]:"", "\n");

    if( size(l_res) > 0 &&  l_res[0]:"" != "root")
    {
        // Popup label (user is not root)
        Popup::Message(_("Log in as a root to run SuSE support."));
        return;
    }

    // global - not necessary to read data again
    string wrapper_data = "";
    string tmpdir = (string) SCR::Read(.target.tmpdir);

    // keydata is global !
    // map keydata = read_supportkey ();

    Wizard::CreateDialog();
    Wizard::SetDesktopIcon("support");
    Wizard::HideAbortButton();

    // if no valid supportkey data present, ask !

    any retval = nil;

    string check_message = check_supportkey (keydata);
    y2milestone ("check_message '%1'", check_message);
    boolean ask_key = (check_message != "");

    boolean go_on = true;

    while (go_on)
    {

	while (ask_key)
	{
		retval = support_askkey();	    
	    if (retval != `next)
	    {
		go_on = false;
		break;
	    }
	    else
	    {
		string check_message = check_supportkey (keydata);
		y2milestone ("check_message '%1'", check_message);
		ask_key = (check_message != "");
		if (ask_key == true)
		{
		    Popup::Error (check_message);
		}
		else if (keydata["changed"]:false)
		{
		    // popup when saving data
		    Popup::Message (_("Saving new supportkey data"));
		    if (!write_supportkey (keydata))
		    {
			// popup when saving failed
			Popup::Error (_("Saving of supportkey data failed !"));
		    }
		    break;
		}
		else
		{
		    break;
		}
	    }
	} // while (ask_key)

	if (go_on == false)
	    break;
	retval = support_question ();
	if (retval == `ask_key)
	{
	    ask_key = true;
	}
	else if (retval == `back)
	{
	    retval = `abort;
	}
	else if (retval == `next)
	{
	    retval = `first;
	    // support_send might return `again, loop here
        // but do not need to generate & read info again - only for first time
	    while (retval == `again || retval == `first)
	    {
		retval = support_send (retval);
	    }
	    if (retval == `next)
	    {
		// just a final text to give user a 'good' feeling
		Popup::Message (_("Thank you for using SuSE support"));
		go_on = false;
	    }
	}

	if (retval == `abort)
	{
	    go_on = false;
	}
    }


    // --------------------------------------------------------------

    Wizard::CloseDialog();

/* EOF */
}
