/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Pieringer Johannes <jpieringer@desktopbsd.net>
 *         (C) 2006
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#ifndef __GRUBCONFIGURATION_H
#define __GRUBCONFIGURATION_H

#define  GRUB_MENU_FILE  "/boot/grub/menu.lst"
#define  GRUB_CONFIG_FILE  "/boot/grub/grub.config"

#define	GRUB_INSTALL_EXECUTABLE	"/usr/local/sbin/grub-install"
#define  GRUB_AUTOMATIC_GENERATION_SCRIPT "/usr/local/sbin/dbsd-grubgen"
#define  GRUB_MENU_GEN_FILE_TEST "/tmp/test_autogenmenu.XXXXX"

#define GRUB_FOLDER "/boot/grub"

#include <qstring.h>
#include <qptrlist.h>
#include <qptrdict.h>

#include "grubbootentry.h"
#include "grubcommand.h"

/** @brief Reads the GRUB-menu.lst File and writes the GRUB afterwards.
  */
class GrubConfiguration
{
protected:
	enum KnownGRUBCommands {Default, Timeout, Color, Title, Root, Kernel, Makeactive, Chainloader, Savedefault, Initrd, Splashimage, Unknown};

	void init();

	static const char *KnownGRUBCommandsNames[12];
	static const char *GRUBBackgroundColorNames[8];
	static const char *GRUBForegroundColorNames[16];

	GrubCommand<int> defaultTitle;	//default of -1 is "saved"
	GrubCommand<int> timeout;
	GrubCommand<QString>* color;
	GrubCommand<QString>* splashimage;
	QPtrList<GrubBootentry> titles;
	QPtrList<GrubCommand<QString> > unknownCommands;
	QString lastComment;

	KnownGRUBCommands getCommand(QString& line);
	void processComment(QString& line, QString &comment, QString& commentAftercommand);
	QString processColor(uint position);

public:
	enum GRUBBackgroundColor
	{
	   BGBlack, BGBlue, BGGreen, BGCyan, BGRed, BGMagenta, BGBrown, BGLightGray
	};
	enum GRUBForegroundColor
	{
	   FGBlack, FGBlue, FGGreen, FGCyan, FGRed, FGMagenta, FGBrown, FGLightGray, FGDarkGray, FGLightBlue, FGLightGreen, FGLightCyan, FGLightRed, 	FGLightMagneta, FGYellow, FGWhite
	};

	GrubConfiguration();
	bool readConfiguration(const QString& path = GRUB_MENU_FILE);
	bool writeConfiguration();
	QString toString();

	void addBootmenuEntry(GrubBootentry* entry);
	void removeBootmenuEntry(int id);
	bool generateBootmenu();

	void setTimeout(int seconds);
	void setDefaultTitle(int title);
	void setBootmenuEntries(QPtrList<GrubBootentry> entries);
	bool setSplashimagePath(QString *splashimagePath);
	void setColor(GRUBForegroundColor* foreNormal, GRUBBackgroundColor* backNormal, bool *blinkNormal, GRUBForegroundColor* foreHigh, GRUBBackgroundColor* backHigh, bool *blinkHigh);
	bool *getNormalBlink();
	GRUBForegroundColor *getColorNormalForeground();
	GRUBBackgroundColor *getColorNormalBackground();
	bool *getHighlightBlink();
	GRUBForegroundColor *getColorHighlightForeground();
	GRUBBackgroundColor *getColorHighlightBackground();
	int getTimeout();
	int getDefaultTitle();
	QString* getSplashimagePath();
	QPtrList<GrubBootentry> getBootmenuEntries();

	static bool installGrub();
	static bool isGrubInstalled();
	static bool isGrubSupported();
	static bool isGrubAvailable();

	friend class GrubBootentry;
};

#endif /* __GRUBCONFIGURATION_H */
