/**************
FILE          : X11Version.ycp
***************
PROJECT       : YaST2
              :
AUTHOR        : Marcus Schäfer <ms@suse.de>
              :
BELONGS TO    : YaST2
              : (X11 integration part using SaX2/ISaX)
              :
DESCRIPTION   : Provides a function to determine the _used_ XFree-version
              : in a running system. Provide information about the
              : package selection status which may told us:
              : there is no X11 installed
              :
              :
STATUS        : Development
 *
 * $Id: X11Version.ycp 21246 2005-02-06 00:29:58Z nashif $
 */

{

module "X11Version";
textdomain "installation";

import "Directory";
import "Installation";
import "Package";
import "Mode";

//=======================================
// System Global Variables
//---------------------------------------
global string  version  = "";
global string  versionLink = "";

//=======================================
// Global Functions
//---------------------------------------
//---[ GetVersion ]----//
global define string GetVersion() ``{
	// ...
	// Set the global variable version to:
	// ""  -   No X11 found
	// "3" -   XFree86 Version 3.x
	// "4" -   XFree86 Version 4.x
	// ---
	// NOTE: This is highly dependent on the X11-infrastructure
	// and must be accommodated to any changes there.
	// ---
	version = "";	// init

	// ...
	// Take a look into the system....
	// ask the libhd for the configuration stuff to this card
	// if there is only one entry pointing to XFree86 version 3
	// XFree86 3 has to be used for this card
	// ---
	list<map> gfxcards = (list<map>) SCR::Read(.probe.display);
	/* more cards -> ver=4 */
	if(size(gfxcards) > 1) {
	    version = "4";
	}
	/* one cards -> inspect drivers */
	else if(size(gfxcards) == 1) {
	    foreach(map gfxcard, gfxcards, {
		list<map> drivers = gfxcard["x11"]:[];
		/* do we have any 4 driver? */
		foreach(map driver, drivers, {
		    if(version == "") {
			if(driver["version"]:"" == "4") version = "4";
		    }
		});
		/* do we have any 3 driver? */
		foreach(map driver, drivers, {
		    if(version == "") {
			if(driver["version"]:"" == "3") version = "3";
		    }
		});
	    });
	}
	/* not sure about default */
	if(version == "") version = "4";

	y2milestone( "xfree_version: <%1>", version );
	return( version );
}

//---[ X11Version ]----//
global define void X11Version() ``{
	// ...
	// The module constructor. Sets some proprietary module data defined
	// for public access This is done only once (and automatically)
	// when the module is loaded for the first time
	// ---
	GetVersion();
	return;
}

//---[ GetX11Link ]----//
global define string GetX11Link() ``{

    string ret = "4";

    integer count = 0;
    string file = Installation::destdir + "/X"; // "/usr/X11R6/bin/X";

    while(count < 10) {
	y2debug("Inspecting: %1 (%2)", file, count);
	map stat = (map) SCR::Read(.target.lstat, file);
	boolean islink = stat["islink"]:false;
	y2debug("islink=%1 (%2)", islink, stat);
	if(islink == nil || islink == false) break;
	file = (string) SCR::Read(.target.symlink, file);
	if(file == nil) break;
	count = count + 1;
    }

    if(file != nil && find(file, "XFree86") == -1) ret = "3";
    y2milestone("X link: %1", ret);
    return ret;
}

//---[ have_x11 ]----//
global define boolean have_x11 () ``{
	// ...
	// check if the required packages are installed
	// ---
	boolean ret = true;
	list<string> pacs = ["xorg-x11", "yast2-x11","sax2"];
        // Dont ask for installing packages, just return in autoinst mode
        if ( Mode::autoinst ())
        {
            ret = Package::InstalledAll(pacs);
        }
        else {
            if (!Package::InstallAllMsg (pacs, 
                        // notification 1/2
                        _("<p>To access the X11 system, the <b>%1</b> package must be installed.</p>") +
                        // notification 2/2
                        _("<p>Do you want to install it now?</p>"))
               ) {
                ret = false;
            }
        }
	y2milestone ("have_x11 = %1", ret);
	return ret;
}

}
