/**
 * File:	clients/inst_autosetup.ycp
 * Package:	Auto-installation
 * Summary:	Setup and prepare system for auto-installation
 * Authors:	Anas Nashif <nashif@suse.de>
 *
 * $Id: inst_autosetup.ycp 21742 2005-02-20 21:50:16Z nashif $
 */

{
    textdomain "autoinst";

    import "AutoinstConfig";
    import "AutoInstall";
    import "Installation";
    import "Profile";
    import "Progress";
    import "Report";
    import "AutoinstStorage";
    import "AutoinstScripts";
    import "AutoinstGeneral";
    import "AutoinstSoftware";
    import "Bootloader";
    import "BootCommon";
    import "Popup";
    import "RunlevelEd";
    import "Arch";
    import "AutoinstLVM";
    import "AutoinstRAID";

    include "bootloader/routines/routines.ycp";
    include "bootloader/routines/autoinstall.ycp";

    string help_text = _("<P>Please wait while the system is prepared for autoinstallation.</P>");
    list<string> progress_stages =
        [
    _("Configure General Settings "),
    _("Execute pre-install user scripts"),
    _("Create partition plans"),
    _("Configure Software selections"),
    _("Configure Bootloader"),
    _("Configure runlevel")
        ];

    list<string> progress_descriptions =
        [
    _("Configuring general settings..."),
    _("Executing pre-install user scripts..."),
    _("Creating partition plans..."),
    _("Configuring Software selections..."),
    _("Configuring Bootloader..."),
    _("Configuring runlevel...")
        ];

    Progress::New(
            _("Preparing System for Automated Installation"),
            "",     // progress_title
            size( progress_stages ),	// progress bar length
            progress_stages,
            progress_descriptions,
            help_text );


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


    // configure general settings


    //
    // Set Mouse, timezone and other workflow variables
    //
    AutoinstGeneral::Import(Profile::Flat["general"]:$[]);
    y2milestone("general: %1", Profile::Flat["general"]:$[] );
    AutoinstGeneral::Write();



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

    Progress::NextStage();

    // Pre-Scripts
    AutoinstScripts::Import(Profile::Flat["scripts"]:$[]);
    AutoinstScripts::Write("pre-scripts", false);

    // Reread Profile in case it was modified in pre-script
    // User has to create the new profile in a pre-defined
    // location for easy processing in pre-script.

    if (SCR::Read(.target.size, AutoinstConfig::modified_profile) > 0 )
    {
	if (! Profile::ReadXML( AutoinstConfig::modified_profile ) || Profile::current == $[] )
	{
	    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;
	}
    }

    //
    // Partitioning and Storage
    ////////////////////////////////////////////////////////////////////////

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


    Progress::NextStage();
    AutoinstStorage::Import(Profile::Flat["partitioning"]:[]);
    AutoinstStorage::ImportAdvanced(Profile::Flat["partitioning_advanced"]:$[]);

    if (!AutoinstStorage::Write())
    {
	Report::Error(_("Error while configuring partitions.
Try again.
"));
	y2error("Aborting...");
	return `abort;
    }
    if (AutoinstRAID::Init())
    {
        AutoinstRAID::Write();
    }
    if (AutoinstLVM::Init())
    {
        AutoinstLVM::Write();
    }



    // Software

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

    Progress::NextStage();
    AutoinstSoftware::Import( Profile::Flat["software"]:$[] );

    if (!AutoinstSoftware::Write())
    {
	Report::Error(_("Error while configuring software selections.
Try again.
"));
	y2error("Aborting...");
	return `abort;
    }

    // Bootloader

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

    BootCommon::getLoaderType(true);
    Bootloader::Import(AI2Export (Profile::Flat["bootloader"]:$[]));
    BootCommon::DetectDisks ();
    y2debug ("autoyast: Proposing - fix");
    Bootloader::Propose ();
    y2debug ("autoyast: Proposing done");


    Progress::NextStage();
    string rl = Profile::Flat["runlevel", "default"]:"";
    y2milestone("autoyast - configured runlevel: %1", rl);
    if (rl!="")
    {
        RunlevelEd::default = rl;
    }
    else
    {
        RunlevelEd::default = (Installation::x11_setup_needed ()
                && Arch::x11_setup_needed ()
                && Pkg::IsSelected ("xorg-x11") )? "5": "3";
    }
    y2milestone("autoyast - setting runlevel to: %1", RunlevelEd::default);

    AutoInstall::PXELocalBoot();

    Progress::Finish();

    return `next;
}
