/**
 * File:
 *	Console.ycp
 *
 * Module:
 *	Console
 *
 * Depends:
 *	Language
 *
 * Summary:
 *	provide console specific stuff (esp. font and encoding)
 *<BR>
 * sysconfig /etc/sysconfig/console:<BR>
 *<UL>
 *<LI>	CONSOLE_FONT		string	console font</LI>
 *<LI>	CONSOLE_SCREENMAP	string	console screenmap</LI>
 *<LI>	CONSOLE_UNICODEMAP	string	console unicode map</LI>
 *<LI>	CONSOLE_MAGIC		string	console magic control sequence</LI>
 *<LI>	CONSOLE_ENCODING	string	console encoding</LI>
 *</UL>
 *
 * $Id: Console.ycp 21198 2005-02-03 16:13:14Z jsuchome $
 *
 * Author:
 *	Klaus Kaempf <kkaempf@suse.de>
 *
 */

{
    module "Console";

    import "Mode";
    import "Language";
    import "Linuxrc";
    import "Encoding";
    import "Stage";

    // current base language, used in Check
    string language	= "en_US";

    string font		= "lat1-16.psfu";
    string unicodeMap	= "";
    string screenMap	= "none";
    string magic	= "(B";

    // non-empty if serial console (written /etc/inittab)
    // -> S0:1235:respawn:/sbin/agetty -L 9600<n8> ttyS0
    // something like "ttyS0,9600" from /etc/install.inf
    string serial	= "";

    /**
     * activate a language specific console font
     *
     * @param	string	language	ISO code of language
     * @return	string	encoding	encoding for console i/o
     */

    global define string SelectFont (string lang)
    ``{

	list consolefont = [];

	map consolefonts = (map)WFM::Read (.local.yast2, "consolefonts.ycp");

	string fqlanguage = Language::GetLocaleString(lang);
	consolefont = consolefonts[fqlanguage]:[];

	if( size(consolefont)==0 )
	    {
	    consolefont = consolefonts[lang]:[];
	    }

	if ((size (consolefont) == 0) && (size (lang) > 2))
	{
	    consolefont = consolefonts[substring(lang, 0, 2)]:[];
	}

	if (size (consolefont) > 0)
	{
	    language	= lang;

	    font	= consolefont[0]:"";
	    unicodeMap  = consolefont[1]:"";
	    screenMap	= consolefont[2]:"";
	    magic	= consolefont[3]:"";

	    string currentLanguage = WFM::GetLanguage ();

	    // Eventually must switch languages to get correct encoding
	    if (currentLanguage != language)
	    {
		string currentEncoding = WFM::GetEncoding();	// save encoding

		Encoding::console = WFM::SetLanguage (language);// switch lang, get proposed encoding

		WFM::SetLanguage (currentLanguage, currentEncoding);// reset as it was before
	    }

	    if (Linuxrc::braille ())
	    {
		SCR::Execute (.target.bash, "/usr/bin/setfont");
	    }
	    else
	    {
		//UI::SetConsoleFont (magic, font, screenMap, unicodeMap, encoding);
		UI::SetConsoleFont (magic, font, screenMap, unicodeMap, language);	
	    }
	}

	y2milestone ("Language %1 -> Console encoding %2", language,
	    Encoding::console);
	return Encoding::console;
    };

    /**
     * save data to system (rc.config agent)
     */

    global define void Save ()
    ``{
	SCR::Write (.sysconfig.console.CONSOLE_FONT,	   font);
	SCR::Write (.sysconfig.console.CONSOLE_SCREENMAP,  screenMap);
	SCR::Write (.sysconfig.console.CONSOLE_UNICODEMAP, unicodeMap);
	SCR::Write (.sysconfig.console.CONSOLE_MAGIC,	   magic);

	SCR::Write (.sysconfig.console.CONSOLE_ENCODING, WFM::GetEncoding() );
	SCR::Write (.sysconfig.console.CONSOLE_ENCODING.comment, "\n# Encoding used for output of non-ascii characters.\n#\n");
	SCR::Write (.sysconfig.console, nil);

	if (serial != "")
	{
	    list splitserial = splitstring (serial, ",");
	    string tty = splitserial[0]:"";
	    string baud = splitserial[1]:"9600";
	    string line = substring (tty, 0, 3) == "tty" ? substring (tty, 3) : tty;

	    if( size(line)>4 )
		{
		line = substring( line, 0, 4 );
		}

	    if ((tty != "") && (baud != ""))
	    {
		string initline = sformat ("%1:1235:respawn:/sbin/agetty -L %2 %3", line, baud, tty);
		if ( tty == "hvc0" )
		{
			initline = sformat ("%1:1235:respawn:/sbin/agetty -L %2 %3 vt320", line, baud, tty);
		}
		else if ( substring( tty, 0, 4 ) == "hvsi" )
		{
			initline = sformat ("%1:1235:respawn:/sbin/agetty -L %2 %3 vt102", line, baud, tty);
		}
		//SCR::Write (.etc.inittab.S0, initline);
		SCR::Execute (.target.bash, sformat ("/bin/grep -v \" %1\"  /etc/inittab >/tmp/inittab", line));
		SCR::Execute (.target.bash, "/bin/echo '" + initline + "' >>/tmp/inittab");
		SCR::Execute (.target.bash, "/bin/mv /tmp/inittab /etc/inittab");
		SCR::Execute (.target.bash, "/bin/echo " + tty + " >>/etc/securetty");
	    }
	}
    }

    /**
     * restore data to system (rc.config agent)
     * returns encoding
     */
    global define string Restore ()
    ``{
	font	   = (string)SCR::Read (.sysconfig.console.CONSOLE_FONT);
	screenMap  = (string)SCR::Read (.sysconfig.console.CONSOLE_SCREENMAP);
	unicodeMap = (string)SCR::Read (.sysconfig.console.CONSOLE_UNICODEMAP);
	magic	   = (string)SCR::Read (.sysconfig.console.CONSOLE_MAGIC);
	language   = (string)SCR::Read (.sysconfig.language.RC_LANG);
	y2milestone( "encoding %1", Encoding::console );
	return Encoding::console;
    }

    /**
     * initialize console settings
     */
    global define void Init () {

	if (Linuxrc::braille ())
	{
	    SCR::Execute (.target.bash, "/usr/bin/setfont");
	}
	else
	{
	    UI::SetConsoleFont (magic, font, screenMap, unicodeMap, language);
	}
    }

    /**
     * Check current configuration
     * This function should be called to check consistency with
     * other modules (mentioned as Depends in the header)
     * @return	0	if no change
     * 		1	change due to dependency with other module
     *		2	inconsistency detected
     *
     */

    global define boolean Check ()
    ``{
	return true;
    }

    /**
     * constructor
     * does nothing in initial mode
     * restores console configuration from /etc/sysconfig
     * in normal mode
     */

    global define void Console ()
    ``{
	if (Stage::initial ())
	{
	    serial = (string)SCR::Read (.etc.install_inf.Console);
	    if (serial == nil) serial = "";
	}
	else
	{
	    Restore ();
	}

	return;
    }
}
