#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] = {};
    syslog(LOG_ERR, "WD--file=[%s]--fun=[%s]--line=[%d] type=%s path_type=%s path=%s", __FILE__, __FUNCTION__, __LINE__,type,path_type,path);
    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/")!=NULL){
                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
            {
                syslog(LOG_ERR, "WD--file=[%s]--fun=[%s]--line=[%d]", __FILE__, __FUNCTION__, __LINE__);
                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_sys = NULL;
    xmlNode *restore_install = NULL;
    xmlNode *install_node = NULL;
    xmlNode *sys_node = 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");
        return 1;
    }
    root_element = xmlDocGetRootElement(doc);
    restore_sys = root_element->children;
    restore_install = restore_sys->next;
    sys_node=restore_sys->children;
    install_node=restore_install->children;

    while (restore_sys != NULL)
    {
        if (strcmp((const char *)restore_sys->name, "restore_sys") == 0)
        {
            syslog(LOG_ERR, "WD--file=[%s]--fun=[%s]--line=[%d] (const char *)restore_install->name=%s", __FILE__, __FUNCTION__, __LINE__, (const char *)restore_install->name);
            for (sys_node = restore_install->children; sys_node; sys_node = sys_node->next)
            {
                if (sys_node->type == XML_ELEMENT_NODE && strcmp((const char *)sys_node->name, "etc") == 0)
                {
                    print_origin_path_elements(keep_root_element,sys_node->children, "etc");
                }else if (strcmp((const char *)sys_node->name, "var") == 0)
                {
                    print_origin_path_elements(keep_root_element,sys_node->children, "var");
                }
            }
            break;
        }
        restore_sys = restore_sys->next;
    }

    while (restore_install != NULL)
    {
        if (strcmp((const char *)restore_install->name, "restore_install") == 0)
        {
            for (install_node = install_node->children; install_node; install_node = install_node->next)
            {
                if (install_node->type == XML_ELEMENT_NODE && strcmp((const char *)install_node->name, "etc") == 0)
                {
                    print_origin_path_elements(keep_root_element,install_node->children, "etc");
                }else if (strcmp((const char *)sys_node->name, "var") == 0)
                {
                    print_origin_path_elements(keep_root_element,install_node->children, "var");
                }
            }
            break;
        }
        install_node = install_node->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;
}