# include "mm-manager.h"

/***************************************进入运维模式*********************************************************/

/*进入运维模式*/
gboolean mm_enter_maintain_mode()
{
    gboolean ret;
    
    ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_MAINTAIN);  
    if (!ret)
    {
        mm_emit_mode_changed_signal (ENTER_MM_MODE_FAILED);
        g_info("enter maintain mode config failed.");
        return FALSE;
    }
    
    //锁文件
    ret = lock_file_acquire(LOCK_FILE_PATH);
    if (!ret)
    {
        mm_emit_mode_changed_signal (ENTER_MM_MODE_FAILED);
        g_info("enter maintain mode lock file failed.");
        return FALSE;
    }
    //通知
    mm_emit_mode_changed_signal (ENTER_MM_MODE_SUCCESS);

    g_info ("mm:enter maintain mode success.");
    
   
    return TRUE;

}

/*******************************************退出运维模式***********************************************************/
// 获取ovl目录路径
void append_char_to_string(gchar *str, gchar ch)
{
    size_t len = strlen(str);
    str[len] = ch;
    str[len+1] = '\0';
}
static char* mm_get_ovl_dir_path()
{
    FILE *fp;  
    char buffer[1024]; 
    char *hash_buf = malloc(1024);

    strcpy(hash_buf,"/ostree/pkgs/ovl-"); 
    int spaceCount = 0;
  
    // 使用popen执行ls命令，并将输出重定向到fp  
    fp = popen("ostree admin status", "r");  
    if (fp == NULL) {  
        perror("Failed to run command");  
        exit(EXIT_FAILURE);  
    }  
  
    // 读取命令的输出  
    while (fgets(buffer, sizeof(buffer), fp) != NULL) 
    {  
        if (g_strstr_len(buffer, -1, "*") != NULL) 
        {
            for (int i=0; buffer[i] !='\0'; i++)
            {
                if(buffer[i] == ' ')
                {
                    spaceCount++;
                    if(spaceCount == 2)
                    {
                        i++;

                        while(buffer[i] != '\0')
                        { 
                            append_char_to_string (hash_buf, buffer[i++]);
                            //printf("%c", buffer[i++]);
                        }

                        break;
                    }
                }
            }   
        }  
    }  
  
    // 关闭popen创建的管道  
    pclose(fp);   
    return hash_buf;  
}
//获取运维模式期间安装包列表
static void mm_get_pkgs_name_period ()
{
    return; //暂时
}

//获取单个安装包数据
static void mm_get_pkg_data()
{
    //暂时
}

static gboolean mm_delete_tmp_layer_data ()
{
    gchar copy_command[2048];
    gchar *ovl_etc_tmp_upper;
    gchar *ovl_usr_tmp_upper;
    gchar *ovl_var_tmp_upper;

    //获取ovl目录路径
    gchar *ovl_dir_path_tmp = mm_get_ovl_dir_path ();
    if (ovl_dir_path_tmp == NULL)
    {
        g_info("mm: get ovl dir path failed.");
        return FALSE;
    }
    gchar *ovl_dir_path = g_strstrip(ovl_dir_path_tmp); 
    gint16 ret;

    ovl_etc_tmp_upper = g_strconcat (ovl_dir_path, ETC_LAYER_TMP_UPPER, "*", NULL);
    snprintf (copy_command, sizeof(copy_command), "rm -rf %s ", ovl_etc_tmp_upper);
    ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: delete etc pkgs data failed.");
        // 发通知
        // 清除ovl tmp层数据
        // 设置启动模式 normal
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);

        return FALSE;
    }

    ovl_usr_tmp_upper = g_strconcat (ovl_dir_path, USR_LAYER_TMP_UPPER, "*", NULL);
    snprintf (copy_command, sizeof(copy_command), "rm -rf %s ", ovl_usr_tmp_upper);
     ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: mv etc pkgs data failed.");
        // 发通知
        // 清除ovl tmp层数据
        // 设置启动模式 normal
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);

        return FALSE;
    }

    ovl_var_tmp_upper = g_strconcat (ovl_dir_path, VAR_LAYER_TMP_UPPER, "*", NULL);
    snprintf (copy_command, sizeof(copy_command), "rm -rf %s ", ovl_usr_tmp_upper);
    ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: mv etc pkgs data failed.");
        // 发通知
        // 清除ovl tmp层数据
        // 设置启动模式 normal
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);

        return FALSE;
    }

    return TRUE;
} 

//移动安装包数据到指定目录
static gboolean mm_move_pkgs_data_to_layer()
{
    gchar copy_command[2048];

    gchar *ovl_etc_tmp_upper;
    gchar *ovl_etc_lower;

    gchar *ovl_usr_tmp_upper;
    gchar *ovl_usr_lower;

    gchar *ovl_var_tmp_upper;
    gchar *ovl_var_lower;

    //获取ovl目录路径
    gchar *ovl_dir_path_tmp = mm_get_ovl_dir_path ();
    if (ovl_dir_path_tmp == NULL)
    {
        g_info("mm: get ovl dir path failed.");
        return FALSE;
    }
    gchar *ovl_dir_path = g_strstrip(ovl_dir_path_tmp); 
    gint16 ret;

    ovl_etc_tmp_upper = g_strconcat (ovl_dir_path, ETC_LAYER_TMP_UPPER, "*", NULL);
    ovl_etc_lower = g_strconcat (ovl_dir_path, ETC_LAYER_LOWER, NULL);
    g_print("1s=%s 2=%s\n",ovl_etc_tmp_upper,ovl_etc_lower);
    snprintf (copy_command, sizeof(copy_command), "cp -aL -r %s %s", ovl_etc_tmp_upper, ovl_etc_lower);
    ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: mv etc pkgs data failed.");
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);
        return FALSE;
    }

    ovl_usr_tmp_upper = g_strconcat (ovl_dir_path, USR_LAYER_TMP_UPPER, "*", NULL);
    ovl_usr_lower = g_strconcat (ovl_dir_path, USR_LAYER_LOWER, NULL);
    snprintf (copy_command, sizeof(copy_command), "cp -aL -r %s %s", ovl_usr_tmp_upper, ovl_usr_lower);
     ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: mv etc pkgs data failed.");
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);

        return FALSE;
    }

    ovl_var_tmp_upper = g_strconcat (ovl_dir_path, VAR_LAYER_TMP_UPPER, "*", NULL);
    ovl_var_lower = g_strconcat (ovl_dir_path, VAR_LAYER_LOWER, NULL);
    snprintf (copy_command, sizeof(copy_command), "cp -aL -r %s %s", ovl_var_tmp_upper, ovl_var_lower);
    ret = system (copy_command);
    if (ret == -1)
    {
        g_info ("mm: mv etc pkgs data failed.");
        // 发通知
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        // 清除ovl tmp层数据
        mm_delete_tmp_layer_data();
        // 设置为标准模式 normal
        gboolean ret;
        ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE,BOOT_MODE_NORMAL);  
        lock_file_release(LOCK_FILE_PATH);

        return FALSE;
    }

    return TRUE;

}

//判断临时运维层 是否与etc upper层数据文件，删除
static void mm_delete_confilct_data ()
{

}
// 删除临时运维层数据


//判断安装包数据是否已在数据修改层，如果在指定层则在拷贝后删除


/*退出运维模式*/
gboolean mm_exit_maintain_mode()
{
    gboolean ret;
    // 保存所有安装包数据
    ret = mm_move_pkgs_data_to_layer ();
    if (!ret)
    {
        g_info("mm exit faile 1.");
        return FALSE;
    }
   
    // 用户选择部分保存安装包数据 （暂时）

    // 判断是否与etc upper层数据文件一致，（暂时）

    // 删除临时运维层数据
    ret = mm_delete_tmp_layer_data ();
    if (!ret)
    {
        g_info("mm exit faile 2.");
        return FALSE;
    }

    /*设置启动模式标志位*/
    ret = mm_set_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE, BOOT_MODE_NORMAL);
    if (!ret)
    {
        //桌面通知，进入运维模式失败
        mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_FAILED);
        g_info ("mm:enter maintain mode failed.");
        return FALSE;
    }
    // 通知用户退出运维模式
    mm_emit_mode_changed_signal(ENTER_NORMAL_MODE_SUCCESS);
    lock_file_release(LOCK_FILE_PATH);
    // 去除桌面水印 （暂时）
    
    return TRUE;
    
}

/***************************获取当前运维状态********************************/
gchar* mm_get_boot_mode()
{
    gchar *boot_mode;
    boot_mode = mm_get_config_value (MM_CONFIG_FILE_PATH, BOOT_SECTION, BOOT_MODE);  
    if (boot_mode == NULL)
    {
        return NULL;
    }
    
    return boot_mode;   

}