#include <syslog.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include <unistd.h>
#include <fcntl.h>
// #include <dbus/dbus.h>
// #include <dbus/dbus-glib-lowlevel.h> /* for glib main loop */
#include <libxml/parser.h>
#include <libxml/tree.h>

void restore(char *path, char *type,char* path_type)
{
    char command[255] = {};
    snprintf(command, sizeof(command), "%s %s %s %s", "/usr/ost-ex.d/restore.sh", type, path_type,path);
    if (system(command) == 0)
    {
        syslog(LOG_INFO, "copy success");
    }
}


void print_keep_path_elements(xmlNode *node) {
    xmlNode *cur_node = NULL;
    for (cur_node = node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE && strcmp((const char *)cur_node->name, "path") == 0) {
            printf("Path: %s\n", xmlNodeGetContent(cur_node));
             if (strstr(xmlNodeGetContent(cur_node),"/etc/")){
                printf("Path: %s\n", xmlNodeGetContent(cur_node));
                 restore(xmlNodeGetContent(cur_node), "etc","install");
            }else{
                printf("Path: %s\n", xmlNodeGetContent(cur_node));
                 restore(xmlNodeGetContent(cur_node), "var","install");
            }
           
        }
        print_keep_path_elements(cur_node->children);
    }
}
void print_path_elements(xmlNode *node, char *type)
{
    xmlNode *cur_node = NULL;
    for (cur_node = node; cur_node; cur_node = cur_node->next)
    {
        if (cur_node->type == XML_ELEMENT_NODE && strcmp((const char *)cur_node->name, "path") == 0)
        {
            printf("Path: %s\n", xmlNodeGetContent(cur_node));
            restore(xmlNodeGetContent(cur_node), type, "install");
        }
    }
    print_path_elements(cur_node->children, type);
}
void search_element(xmlNode *node, const char *name,char*type) {
     xmlNode *cur_node = NULL;
    for (cur_node = node; cur_node; cur_node = cur_node->next) {
        if (cur_node->type == XML_ELEMENT_NODE && strcmp((const char *)cur_node->name, "path") == 0) {
           // copy(xmlNodeGetContent(cur_node), type);
           if(strcmp(xmlNodeGetContent(cur_node),name)==0){
               printf("Element %s found with content: %s\n", name, (char *)xmlNodeGetContent(cur_node));
           }else{
                restore(xmlNodeGetContent(cur_node), type,"sys");
           }
        }
        search_element(cur_node->children, name,type);
    }
}
void print_origin_path_elements(xmlNode *keep_node, xmlNode *node, char *type)
{
    xmlNode *cur_node = NULL;
    xmlNode *cur_orig_node = NULL;
    
    for (cur_orig_node = node; cur_orig_node; cur_orig_node = cur_orig_node->next)
    {
        if (cur_orig_node->type == XML_ELEMENT_NODE && strcmp((const char *)cur_orig_node->name, "path") == 0)
        {
            if (keep_node != NULL)
            {
                search_element(keep_node, xmlNodeGetContent(cur_orig_node), type);
            }
            else
            {
                restore(xmlNodeGetContent(cur_node), type,"sys");
            }
        }
        print_origin_path_elements(keep_node,cur_orig_node->children, type);
    }
}
void read_copy_xml(){
    xmlDoc *doc = NULL;
    xmlDoc *keep_doc = NULL;
    xmlNode *root_element = NULL;
    xmlNode *keep_root_element = NULL;
    xmlNode *restore_install_etc = NULL;
    xmlNode *restore_install_var=NULL;

    LIBXML_TEST_VERSION
    const char *filename = "/usr/ost-ex.d/ostree_keep.xml"; // 替换为你要检查的文件名

    if (access(filename, F_OK) != -1)
    {
        syslog(LOG_INFO, "文件 %s 存在\n", filename);
        keep_doc = xmlReadFile("/usr/ost-ex.d/ostree_keep.xml", NULL, 0);
        if (keep_doc == NULL)
        {
            syslog(LOG_ERR,"Error: could not parse file");
        }else{
            keep_root_element =xmlDocGetRootElement(keep_doc);
            if (keep_root_element != NULL)
            {
                print_keep_path_elements(keep_root_element);
            }
        }
    }
    else
    {
        syslog(LOG_INFO, "文件 %s 不存在\n", filename);
    }
    doc = xmlReadFile("/usr/ost-ex.d/restore.xml", NULL, 0);

    if (doc == NULL)
    {
        printf("Error: could not parse file %s", argv[1]);
        return 1;
    }
    root_element = xmlDocGetRootElement(doc);
    restore_sys_etc = root_element->children;
    restore_install_etc = restore_sys_etc->next;
    restore_sys_var=restore_install_etc->next;
    restore_install_var=restore_sys_var->next;

    while (restore_sys_etc != NULL)
    {
        if (strcmp((const char *)restore_sys_etc->name, "restore_sys_etc") == 0)
        {
            print_origin_path_elements(keep_root_element,restore_sys_etc->children,"etc");
            break;
        }
        restore_sys_etc = restore_sys_etc->next;
    }

    while (restore_install_etc != NULL)
    {
        if (strcmp((const char *)restore_install_etc->name, "restore_install_etc") == 0)
        {
            print_path_elements(restore_install_etc->children,"etc");
            break;
        }
        restore_install_etc = restore_install_etc->next;
    }
    while (restore_sys_var != NULL)
    {
        if (strcmp((const char *)restore_sys_var->name, "restore_sys_var") == 0)
        {
            print_origin_path_elements(keep_root_element,restore_sys_var->children,"var");
            break;
        }
        restore_sys_var = restore_sys_var->next;
    }
    while (restore_install_var != NULL)
    {
        if (strcmp((const char *)restore_install_var->name, "restore_install_var") == 0)
        {
            print_path_elements(restore_install_var->children,"var");
            break;
        }
        restore_install_var = restore_install_var->next;
    }

    xmlFreeDoc(doc);
    xmlFreeDoc(keep_doc);
    xmlCleanupParser();

    return 0;
}
int main(int argc, char **argv) {
    const char *filename = "/etc/ostree/ostree_mode"; // 替换为你要检查的文件名
   if (access(filename, F_OK) != -1)
    {
        syslog(LOG_INFO, "文件 %s 存在\n", filename);
        FILE *file = fopen(filename, "r");
        if (file == NULL)
        {
        printf("无法打开文件！");
        exit(1);
        }

        char line[256];
        while (fgets(line, sizeof(line), file))
        {
            if (strncmp(line, "mode=normal", 11) == 0)
            {
               read_copy_xml();
            }
        }
        fclose(file);
    }
    return 0;
}