#include "mm-config.h"


gboolean mm_init_config_file(const gchar *file_path)
{
    GFileOutputStream *stream;
    GError *error = NULL;

    GFile *file = g_file_new_for_path(file_path);
    if (g_file_query_exists(file, NULL))
    {
      g_print("ostree-ovl.conf has already exists.");
      return TRUE;
    }

    g_print("ostree-ovl.conf not exists, now create it...");
    stream = g_file_create(file, G_FILE_CREATE_NONE, NULL, &error);
    if (error != NULL)
    {
      g_print("ostree-ovl.conf create failed.");
      return FALSE;
    }
    
    const gchar *contents = "[boot]\nmode=normal\n";  
    gsize bytes_written;  

    g_output_stream_write_all(G_OUTPUT_STREAM(stream), contents, strlen(contents), &bytes_written, NULL, &error);  
    if (error != NULL) {  
            g_error_free(error);  
            g_object_unref(stream);  
            g_object_unref(file);  
            return FALSE;  
    }  

    //g_error_free(error);  
    g_object_unref(stream);  
    g_object_unref(file); 

    return TRUE;  
}

/*获取配置文件key-value*/
gchar* mm_get_config_value (const gchar *file_path, gchar *section, gchar *key)
{
    GKeyFile *key_file;
    GError *error = NULL;
    gchar *value;
    //创建一个Gkeyfile对象
    key_file = g_key_file_new();

    //加载配置文件
    if(!g_key_file_load_from_file(key_file, file_path, G_KEY_FILE_NONE, &error))
    {
      	if(error != NULL)
    		g_error_free (error);

      	g_key_file_free(key_file);
      	value = NULL;
     	return value;
    }

    //获取当前文件中key的value
    value = g_key_file_get_string (key_file, section, key, &error);
    if(error != NULL)
    {
		g_error_free (error);
      	value = NULL;
    } 

    return value;
}

/*设置配置文件key-value*/
gboolean mm_set_config_value (const gchar *file_path, gchar *section, gchar *key, gchar *value)
{

    // 配置文件是否存在，不存在初始化创建
    GFile *file;
    GFileInfo *info;
    GKeyFile *key_file;
    GError *error = NULL;

    file = g_file_new_for_path (file_path);
    info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NONE, NULL, &error);
    if (info == NULL)
    {
      const gchar *default_conf = "[boot]\nboot=normal\n";
      if (!g_file_replace_contents(file, default_conf, -1, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, &error))
      {
        g_info("1111.");
        return FALSE;
      }
    }
    g_object_unref(info);
    g_object_unref(file);
    
    //创建一个Gkeyfile对象
    key_file = g_key_file_new();

    //加载配置文件
    if(!g_key_file_load_from_file(key_file, file_path, G_KEY_FILE_NONE, &error))
    {
      if (error->code == G_FILE_ERROR_NOENT)
      {
        /*文件不存在，直接创建文件*/
        g_info("file does exits.");
      }else
      {
        g_info("load conf file error.");
      }
    }
    

    if (!g_key_file_has_group(key_file, section))
    {
      g_key_file_set_comment(key_file, section, key, "new group", &error);
    }
    
    if (g_key_file_has_key(key_file, section, key, NULL))
    {
      g_key_file_set_string (key_file, section, key, value);
    }else
    {
      g_key_file_set_string (key_file, section, key, value);
    }
    
    
    if (!g_key_file_save_to_file (key_file, file_path, &error))
    {
      g_printerr("save err %s \n",error->message);
      g_info("1112.");
      //g_error_free(error);
      return FALSE;
    }

    return TRUE;
}

gboolean mm_clear_config_value (const gchar *file_path, gchar *section, gchar *key)
{
    GKeyFile *key_file;
    GError *error = NULL;

    key_file = g_key_file_new();

    if (!g_key_file_load_from_file(key_file, file_path, G_KEY_FILE_KEEP_COMMENTS, &error)) {
        g_printerr("Error loading file: %s\n", error->message);
		if (error != NULL)
        	g_error_free(error);
        return FALSE;
    }

    g_key_file_remove_key(key_file, section, key, &error);
    if (error != NULL) {
        g_printerr("Error removing entry: %s\n", error->message);
        g_error_free(error);
    } else {
        g_print("Successfully removed %s %s entry.\n", section, key);
    }

    if (!g_key_file_save_to_file(key_file, file_path, &error)) {
        g_printerr("Error saving file: %s\n", error->message);
		if (error != NULL)
        	g_error_free(error);
    } else {
        g_print("Configuration saved successfully.\n");
        g_key_file_free(key_file);
    }
    return TRUE;
}