/**
 * File:	clients/inst_autoinit.ycp
 * Package:	Auto-installation
 * Summary:	Parses XML Profile for automatic installation
 * Authors:	Anas Nashif <nashif@suse.de>
 *
 * $Id: inst_autoinit.ycp 21742 2005-02-20 21:50:16Z nashif $
 *
 */

{

    textdomain "autoinst";

    import "Installation";
    import "AutoInstall";
    import "AutoinstConfig";
    import "ProfileLocation";
    import "AutoInstallRules";
    import "Progress";
    import "Report";
    import "Language";
    import "Console";
    import "Profile";

    import "Popup";

    include "autoinstall/autoinst_dialogs.ycp";

    Console::Init ();

    string help_text = _("<p>
Please wait while the system is prepared for autoinstallation.</p>
");
    list<string> progress_stages =
	[
	 _("Probe hardware"),
	 _("Retrieve & Read Control File"),
	 _("Process Profiles and Rules"),
	 _("Parse control file"),
	 _("Set up language")
	 ];


    Progress::New(
		  _("Preparing System for Automatic Installation"),
		  "",     // progress_title
		  7 ,	// progress bar length
		  progress_stages,
		  [],
		  help_text );
    Progress::NextStage();
    Progress::Title(_("Probing hardware..."));
    WFM::CallFunction("inst_startup", [] );

    if(UI::PollInput() == `abort)
        if (Popup::ConfirmAbort (`painless))
            return `abort;


    Progress::NextStage();
    // Initialize Rules
    Progress::Title(AutoinstConfig::message);
    AutoInstallRules::Init();
    boolean ret = false;
    Progress::NextStep();
    Progress::Title(_("Processing Profiles and Rules"));
    while (true)
    {
        boolean r = ProfileLocation::Process();
        if (r)
        {
            break;
        }
        else {
            string newURI = ProfileSourceDialog(AutoinstConfig::OriginalURI);
            if ( newURI == "")
            {
                return `abort;
            }
            else
            {
                AutoinstConfig::ParseCmdLine(newURI);
                AutoinstConfig::SetProtocolMessage();
                continue;
            }
        }
    }

    /*
    if (!ProfileLocation::Process())
    {
	y2error("Aborting...");
	return `abort;
    }
    */

    sleep(1000);


    if(UI::PollInput() == `abort)
        if (Popup::ConfirmAbort (`painless))
            return `abort;

    Progress::NextStage();

    //
    // Set reporting behaviour to default, changed later if required
    //
    Report::LogMessages(true);
    Report::LogErrors(true);
    Report::LogWarnings(true);

    if(UI::PollInput() == `abort)
        if (Popup::ConfirmAbort (`painless))
            return `abort;

    Progress::NextStage();
    Progress::Title( _("Parsing control file"));
    if (! Profile::ReadXML( AutoinstConfig::xml_tmpfile ) || Profile::current == $[]  || Profile::current == nil)
    {
	Popup::Error(_("Error while parsing the control file.
Check the log files for more details or fix the
control file and try again.
"));
	return `abort;
    }

    y2debug ("Autoinstall control file %1",  Profile::current );


    Progress::NextStage();
    Progress::Title(_("Initial Configuration"));
    Report::Import(Profile::current["install", "report"]:$[]);

    //
    // Copy the control file for easy access by user to  a pre-defined
    // directory
    //
    SCR::Execute(.target.bash, sformat("cp %1 %2/autoinst.xml",  AutoinstConfig::xml_tmpfile, AutoinstConfig::profile_dir));

    // AutoInstall::ProcessSpecialResources();

    boolean use_utf8 = true;		// utf8 is default

    map displayinfo = UI::GetDisplayInfo();
    if ( ! displayinfo["HasFullUtf8Support"]:true )
    {
	use_utf8 = false;		// fallback to ascii
    }

    string language = Profile::current["install","general","language"]:Language::language;

    //
    // Set it in the Language module.
    //
    Progress::NextStep();
    Progress::Title(_("Configuring language..."));
    Language::Set( language );

    //
    // Set Console font
    //
    Installation::encoding = Console::SelectFont( Language::language );

    if (displayinfo["HasFullUtf8Support"]:true)
    {
        Installation::encoding = "UTF-8";
    }

    UI::SetLanguage (Language::language, Installation::encoding);
    WFM::SetLanguage (Language::language, "UTF-8");

    sleep(1000);
    Progress::Finish();

    return `next;
}
