/**
 * File:
 *	modules/AutoinstClass.ycp
 *
 * Module:
 *	AutoinstClass
 *
 * Summary:
 *	This module handles the configuration for auto-installation
 *
 * Authors:
 *	Anas Nashif <nashif@suse.de>
 *
 * $Id: AutoinstClass.ycp 21131 2005-01-31 16:10:26Z nashif $
 */

{
    module "AutoinstClass";

    import "AutoinstConfig";
    import "XML";
    import "Summary";
    include "autoinstall/xml.ycp";




    global string classDir = AutoinstConfig::classDir;

    global string ClassConf = "/etc/autoinstall";

    global list<map> profile_conf = [];

    global list Profiles = [];

    global list<map> Classes = [];

    global list<map> confs = [];

    string class_file = "classes.xml";

    string classPath = sformat("%1/%2", ClassConf, class_file);



    /**
     * find a profile path
     * @param string profile name
     * @return string profile Path
     *
     */
    global define string findPath(string name, string class)
	``{
	map result = (map) filter(map c,  confs,
				   ``(c["name"]:"" == name && c["class"]:"" == class))[0]:$[];

	string profile_path = sformat("%1/%2/%3", AutoinstConfig::classDir, result["class"]:"", result["name"]:"default");
	return profile_path;
    }


    /**
     * Read classes
     */
    global define void Read()
	``{
	if (SCR::Read(.target.size, classPath) != -1)
	{
	    // TODO: use XML module
	    map tmp = (map)SCR::Read(.xml, classPath);
	    Classes = tmp["classes"]:[];
	}
	return;
    }


    
    /*
     * Constructor
     * @return void
     */

    global define void AutoinstClass()
	``{
	classSetup();
	Read();
	return;
    }


    /**
     * Merge Classes
     **/

    global define map MergeClasses(map configuration,
			       string base_profile,
			       string resultFileName
			       )
	``{

	string dontmerge_str = "";
	integer i = 1;
	foreach (string dm,  AutoinstConfig::dontmerge, ``{
	    dontmerge_str = dontmerge_str + sformat(" --param dontmerge%1 \"'%2'\" ", i,  dm);
	    i = i + 1;
	});
	string tmpdir = AutoinstConfig::tmpDir;
	string MergeCommand = sformat("/usr/bin/xsltproc --novalid --param replace \"'false'\" %1 --param with ",dontmerge_str ) ;

	MergeCommand = MergeCommand + "\"'" + findPath(configuration["name"]:"", configuration["class"]:"") + "'\"  ";
	MergeCommand = MergeCommand + "--output " +  tmpdir + "/" + resultFileName + " ";
	MergeCommand = MergeCommand + " /usr/share/autoinstall/xslt/merge.xslt ";
	MergeCommand = MergeCommand +   base_profile + " ";


	y2milestone("Merge command: %1", MergeCommand);

	map out = (map)SCR::Execute(.target.bash_output, MergeCommand, $[]);
	y2milestone("Merge stdout: %1, stderr: %2", out["stdout"]:"", out["stderr"]:"");
	return (out);
    }





    /**
     * Read files from class directories
     * @return void
     */
    global define void Files()
	``{
	confs = [];
	foreach(map class, Classes,
		``{
	    list<string> files = (list<string>) SCR::Read(.target.dir, classDir + "/" + class["name"]:"xxx");
	    if (files != nil ) {
		y2milestone("Files in class %1: %2", class["name"]:"xxx", files);
		list<map> tmp_confs = maplist(string file , files , ``{
		    map conf = $[];
		    conf["class"] =  class["name"]:"xxx";
		    conf["name"]  = file;
		    return (conf);
		});
		y2milestone("Configurations: %1", tmp_confs);
		confs = (list<map>)union(confs, tmp_confs);
	    }
	});
	y2milestone("Configurations: %1", confs);
	return;
    }



    /**
     * Save Class definitions
     */

    global define boolean Save()
	``{
	map tmp = $[ "classes":Classes];
	y2debug("saving classes: %1", classPath);
	return (XML::YCPToXMLFile(`class, tmp, classPath));
    }



    /**
     * Import configuration
     */

    global define boolean Import(list<map> settings)
	``{
	profile_conf = settings;
	return true;
    }

    /**
     * Export configuration
     */

    global define list<map> Export()
	``{
	return profile_conf;
    }

    /**
     * Configuration Summary
     */

    global define string Summary() ``{
	string summary = "";

	foreach(map c, profile_conf, ``{
	    summary = Summary::AddHeader(summary, c["class_name"]:"None");
	    summary = Summary::AddLine(summary,  c["configuration"]:"None");
	});
	if (size(summary) == 0 ) {
	    return Summary::NotConfigured();
	}
	return summary;
    }


}
