/**
 * File:	modules/AutoInstall.ycp
 * Package:	Auto-installation
 * Summary:	Auto-installation related functions module
 * Author:	Anas Nashif <nashif@suse.de>
 *
 * $Id: AutoInstall.ycp 21739 2005-02-20 21:43:45Z nashif $
 */
{
    module "AutoInstall";
    textdomain "autoinst";

    import "Profile";
    import "Mode";
    import "Stage";
    import "AutoinstConfig";
    import "AutoInstallRules";
    import "Report";
    import "TFTP";

    global boolean autoconf = false;

    /**
     * Read saved data in continue mode
     * @return boolean true on success
     */
    global define boolean Continue()
    {
	//
	// First check if there are some other control files availabe
	// i.e. for post-installation only
	//
        boolean ret = false;
	if ( SCR::Read(.target.size, AutoinstConfig::autoconf_file) != -1 )
	{
	    y2milestone("XML Post installation data found: %1", AutoinstConfig::autoconf_file );
	    ret = Profile::ReadXML (   AutoinstConfig::autoconf_file  );
	    SCR::Execute(.target.bash, sformat("/bin/mv %1 %2",
                        AutoinstConfig::autoconf_file, AutoinstConfig::cache));
	    return (ret);
	}
	else
	{
	    ret = Profile::ReadProfileStructure ( AutoinstConfig::parsedControlFile );
	    if ( Profile::current == $[] || !ret ) {
		y2milestone( "No saved autoinstall data found" );
		return (false);
	    } else {
		y2milestone( "Found and read saved autoinst data");
		SCR::Execute(.target.remove, AutoinstConfig::parsedControlFile);
		return (true);
	    }
	}

        return false;

    }

    /**
     * Constructer
     * @return: void
     */
    global define void AutoInstall()
    {
        if (Stage::cont ())
        {
            boolean ret = Continue();
            if (ret && size ( Profile::current ) > 0) {
                y2milestone("Enabling Auto-Installation mode");
                Mode::SetMode("autoinstallation");
            } else if (Mode::autoinst ()) {
                y2milestone("No autoyast data found, switching back to manual installation");
                Mode::SetMode("installation");
            }
        }
        else if (Stage::initial ())
        {

            if ( SCR::Read(.target.size, AutoinstConfig::xml_tmpfile) != -1
                    && size ( Profile::current ) == 0)
            {
                y2milestone("autoyast: %1 found", AutoinstConfig::xml_tmpfile);
                // Profile is available and it has not been parsed yet.
                Profile::ReadXML(  AutoinstConfig::xml_tmpfile );
            }
        }
        return;
    }


    /**
     * Save configuration
     * @return: boolean true on success
     */
    global define boolean Save()
    {
        if (Mode::autoinst ())
	    return (Profile::SaveProfileStructure( AutoinstConfig::parsedControlFile ));
        else
            return true;
    }

    /**
     * Finish Auto-Installation by saving misc files
     * @param string destdir
     * @return void
     */
    global define void Finish ( string destdir )
    {
        list dircontents = (list)SCR::Read(.target.dir, AutoinstConfig::tmpDir
                                + "/pre-scripts/");
        if (size(dircontents) > 0 )
        {
            SCR::Execute(.target.bash, "/bin/cp " +  AutoinstConfig::tmpDir
                    + "/pre-scripts/* " + destdir + AutoinstConfig::scripts_dir);
            SCR::Execute(.target.bash, "/bin/cp " +  AutoinstConfig::tmpDir
                    + "/pre-scripts/logs/* " + destdir + AutoinstConfig::logs_dir);
        }

        SCR::Execute(.target.bash, sformat("/bin/cp %1 %2%3",
                    AutoinstConfig::xml_tmpfile ,  destdir ,  AutoinstConfig::xml_file) );
        SCR::Execute(.target.bash, sformat("/bin/chmod 700 %1%2",
                    destdir ,  AutoinstConfig::xml_file) );

        return;
    }


    /**
     * Put PXE file on the boot server using tftp
     * @return true on success
     */
    global boolean PXELocalBoot ()
    {
        string tmpdir = (string)SCR::Read(.target.tmpdir);
        string hexfile = sformat("%1/%2", tmpdir, AutoInstallRules::hostid );
        map pxe = Profile::Flat["pxe"]:$[];
        string server = pxe["tftp-server"]:"";
        if (server!="" && pxe["pxe_localboot"]:false)
        {
            y2milestone("putting pxe local boot file on server :%1", server);
            string config = pxe["pxelinux-config"]:"";
            string dir = pxe["pxelinux-dir"]:"pxelinux.cfg";
            if (config == "")
            {
                config = "DEFAULT linux\nLABEL linux\n  localboot 0";
            }

            SCR::Write(.target.string, hexfile,  config);

            return TFTP::Put(server, dir + "/" + AutoInstallRules::hostid,
                hexfile );
        }
        return true;
    }

//EOF
}

